QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#1525#876257#9737. Let's Go! New AdventurecooluoMax_s_xaMFailed.2025-02-06 17:02:172025-02-06 17:02:18

Details

Extra Test:

Invalid Input

input:

1
15 15 7379813967
1546918924 6199042587 4201373264 3444199571 9984434463 8594430007 5450070433 9983489213 2612388588 3325141094 2627583417 955203411 6291670195 8527792554 6218938963
1823353825 1963172212 2984884327 4612281154 5555573952 6129635346 6350357593 7343784671 7420820324 7543275026 7824183...

output:


result:

FAIL Expected int32, but "7379813967" found (test case 1, stdin, line 2)

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#876257#9737. Let's Go! New AdventureMax_s_xaMAC ✓1066ms20844kbC++174.1kb2025-01-30 19:07:092025-01-30 19:07:11

answer

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <random>
#include <ctime>
#include <chrono>
#include <numeric>
#include <iomanip>
#include <cassert>

typedef long long ll;
typedef double lf;
typedef unsigned long long ull;

// #define DEBUG 1
struct IO
{
    #define MAXSIZE (1 << 20)
    #define isdigit(x) (x >= '0' && x <= '9')
    char buf[MAXSIZE], *p1, *p2;
    char pbuf[MAXSIZE], *pp;
    #if DEBUG
    #else
    IO() : p1(buf), p2(buf), pp(pbuf) {}
    ~IO() {fwrite(pbuf, 1, pp - pbuf, stdout);}
    #endif
    #define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) ? ' ' : *p1++)
    #define blank(x) (x == ' ' || x == '\n' || x == '\r' || x == '\t')

    template <typename T>
    void Read(T &x)
    {
        #if DEBUG
        std::cin >> x;
        #else
        bool sign = 0; char ch = gc(); x = 0;
        for (; !isdigit(ch); ch = gc())
            if (ch == '-') sign = 1;
        for (; isdigit(ch); ch = gc()) x = x * 10 + (ch ^ 48);
        if (sign) x = -x;
        #endif
    }
    void Read(char *s)
    {
        #if DEBUG
        std::cin >> s;
        #else
        char ch = gc();
        for (; blank(ch); ch = gc());
        for (; !blank(ch); ch = gc()) *s++ = ch;
        *s = 0;
        #endif
    }
    void Read(char &c) {for (c = gc(); blank(c); c = gc());}

    void Push(const char &c)
    {
        #if DEBUG
        putchar(c);
        #else
        if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
        *pp++ = c;
        #endif
    }
    template <typename T>
    void Write(T x)
    {
        if (x < 0) x = -x, Push('-');
        static T sta[35];
        int top = 0;
        do sta[top++] = x % 10, x /= 10; while (x);
        while (top) Push(sta[--top] ^ 48);
    }
    template <typename T>
    void Write(T x, char lst) {Write(x), Push(lst);}
} IO;
#define Read(x) IO.Read(x)
#define Write(x, y) IO.Write(x, y)
#define Put(x) IO.Push(x)

using namespace std;

const int MAXN = 5e5 + 10;

int n, m, c;
ll a[MAXN], b[MAXN], f[MAXN];

struct Line
{
    int l, r, x;
}st[MAXN];
int head, tail;

inline ll Calc(ll x) { return upper_bound(b + 1, b + m + 1, x) - b - 1; }
struct Frac
{
    ll k, a, b;
    bool operator < (const Frac &u) const
    {
        if (k ^ u.k) return k < u.k;
        return (__int128)a * u.b < (__int128)u.a * b;
    }
};
inline Frac F(ll x)
{
    int p = upper_bound(b + 1, b + m + 1, x) - b - 1;
    if (p == m) return Frac{p, 0, 1};
    return Frac{p, x - b[p], p == m ? 1 : b[p + 1] - b[p]};
}
inline bool cmp(int x, int y, int z)
{
    Frac u = F(a[z] - a[x]), v = F(a[z] - a[y]);
    u.k += f[x], v.k += f[y];
    return u < v;
}

int main()
{
    #ifndef DEBUG
    ios::sync_with_stdio(0), cin.tie(0);
    #endif
    int T;
    Read(T);
    while (T--)
    {
        Read(n), Read(m), Read(c);
        for (int i = 1; i <= n; i++) Read(a[i]), a[i] += a[i - 1];
        for (int i = 1; i <= m; i++) Read(b[i]), b[i] += b[i - 1];
        f[0] = 0, st[head = tail = 1] = Line{1, n, 0};
        for (int i = 1; i <= n; i++)
        {
            f[i] = f[st[head].x] + Calc(a[i] - a[st[head].x]) - c;
            if (head <= tail && st[head].l <= i) st[head].l = i + 1;
            if (head <= tail && st[head].l > st[head].r) head++;
            while (head <= tail && cmp(st[tail].x, i, st[tail].l)) tail--;
            if (head > tail) st[++tail] = Line{i + 1, n, i};
            else
            {
                int l = st[tail].l + 1, r = st[tail].r, mid, p = r + 1;
                while (l <= r)
                {
                    mid = l + r >> 1;
                    if (cmp(st[tail].x, i, mid)) p = mid, r = mid - 1;
                    else l = mid + 1;
                }
                st[tail].r = p - 1;
                if (p <= n) st[++tail] = Line{p, n, i};
            }
        }
        cout << f[n] << '\n';
    }
    return 0;
}