Submission #708187

# Submission time Handle Problem Language Result Execution time Memory
708187 2023-03-11T09:18:46 Z LittleCube Two Dishes (JOI19_dishes) C++14
74 / 100
4198 ms 269100 KB
#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
#define ll long long
using namespace std;

const ll inf = 1'000'000'000'000'000'000;
const ll mxN = 2'000'006;
int N, M;
ll A[mxN], B[mxN], S[mxN], T[mxN], P[mxN], Q[mxN], dp[mxN], seg[4 * mxN], lazy[4 * mxN], bit[mxN], rcost[mxN], ucost[mxN], tmp[mxN][2];
int rangeA[mxN], rangeB[mxN], nrange[mxN];
vector<int> ins[mxN];

void Bmodify(int pos, ll val)
{
    for (int i = pos; i < mxN; i += (i & -i))
        bit[i] += val;
}

ll Bquery(int pos)
{
    ll ans = 0;
    for (int i = pos; i > 0; i -= (i & -i))
        ans += bit[i];
    return ans;
}

void init(int i = 1, int L = 0, int R = N)
{
    if (L == R)
        seg[i] = -inf, lazy[i] = 0;
    if (L != R)
    {
        int mid = (L + R) / 2;
        init(i << 1, L, mid);
        init(i << 1 | 1, mid + 1, R);
    }
}

void modify(int mL, int mR, ll val, int i = 1, int L = 0, int R = N)
{
    if (mL <= L && R <= mR)
        seg[i] += val, lazy[i] += val;
    else if (R < mL || mR < L)
        return;
    else
    {
        int mid = (L + R) / 2;
        seg[i << 1] += lazy[i], seg[i << 1 | 1] += lazy[i];
        lazy[i << 1] += lazy[i], lazy[i << 1 | 1] += lazy[i];
        lazy[i] = 0;
        modify(mL, mR, val, i << 1, L, mid);
        modify(mL, mR, val, i << 1 | 1, mid + 1, R);
        seg[i] = max(seg[i << 1], seg[i << 1 | 1]);
    }
}

ll query(int mL, int mR, int i = 1, int L = 0, int R = N)
{
    if (mL <= L && R <= mR)
        return seg[i];
    else if (R < mL || mR < L)
        return -inf;
    else
    {
        int mid = (L + R) / 2;
        seg[i << 1] += lazy[i], seg[i << 1 | 1] += lazy[i];
        lazy[i << 1] += lazy[i], lazy[i << 1 | 1] += lazy[i];
        lazy[i] = 0;
        return max(query(mL, mR, i << 1, L, mid),
                   query(mL, mR, i << 1 | 1, mid + 1, R));
    }
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> N >> M;
    for (int i = 1; i <= N; i++)
    {
        cin >> A[i] >> S[i] >> P[i];
        A[i] += A[i - 1];
    }
    for (int i = 1; i <= M; i++)
    {
        cin >> B[i] >> T[i] >> Q[i];
        B[i] += B[i - 1];
    }
    for (int i = 1; i <= N; i++)
    {
        rangeA[i] = upper_bound(B, B + 1 + M, S[i] - A[i]) - B - 1;
        if (rangeA[i] == -1)
            rangeA[i] = 0, P[i] = 0;
    }
    for (int i = 1; i <= M; i++)
    {
        rangeB[i] = upper_bound(A, A + 1 + N, T[i] - B[i]) - A - 1;
        if (rangeB[i] == -1)
            rangeB[i] = 0, Q[i] = 0;
        else if (rangeA[rangeB[i] + 1] >= i)
        {
            // cerr << "build insert " << i << " -> " << rangeB[i] + 1 << '\n';
            ins[rangeB[i] + 1].emplace_back(i - 1);
        }
    }
    rangeA[++N] = M;
    int nN = 0;
    for (int i = 1; i <= N; i++)
        tmp[i][0] = P[i], tmp[i][1] = rangeA[i];
    for (int i = 1; i <= N; i++)
    {
        sort(ins[i].begin(), ins[i].end(), greater<>());
        nrange[i] = ++nN;
        P[nN] = tmp[i][0];
        rangeA[nN] = tmp[i][1];
        for (int j : ins[i])
        {
            ++nN;
            rangeA[nN] = j;
            P[nN] = 0;
        }
    }
    for (int i = 1; i <= M; i++)
        rangeB[i] = nrange[rangeB[i]];
    N = nN;
    vector<int> v;

    for (int i = 1; i <= N; i++)
        v.emplace_back(i);
    sort(v.begin(), v.end(), [&](int i, int j)
         { return make_pair(rangeA[i], i) < make_pair(rangeA[j], j); });

    ll sumR = 0;
    for (int i = N; i >= 1; i--)
    {
        rcost[i] = sumR - Bquery(rangeA[i]);
        sumR += P[i];
        Bmodify(rangeA[i] + 1, P[i]);
    }
    for (int i = 0; i < mxN; i++)
        bit[i] = 0;

    int last = 1;
    ll sumU = 0;

    init();
    modify(0, 0, inf);
    for (int i = 1; i <= N; i++)
        modify(0, i, P[i]);
    for (int i : v)
    {
        while (last <= rangeA[i])
        {
            sumU += Q[last];
            Bmodify(rangeB[last] + 1, Q[last]);
            modify(0, rangeB[last], Q[last]);
            last++;
        }
        ucost[i] = sumU - Bquery(i);
        dp[i] = query(0, i - 1) - rcost[i];
        // cerr << ' ' << query(0, 0) << '\n';
        // cerr << i << ' ' << query(0, i - 1) << '-' << rcost[i] << '\n';
        modify(0, i, -P[i]);
        modify(i, i, inf + dp[i] - ucost[i]);
    }
    cout << dp[N] << '\n';
}

/*
1 1
2 3 1
1 2 -1
*/
# Verdict Execution time Memory Grader output
1 Correct 357 ms 92140 KB Output is correct
2 Correct 362 ms 91620 KB Output is correct
3 Correct 341 ms 92240 KB Output is correct
4 Correct 355 ms 91688 KB Output is correct
5 Correct 33 ms 63088 KB Output is correct
6 Correct 361 ms 91300 KB Output is correct
7 Correct 113 ms 68768 KB Output is correct
8 Correct 250 ms 86548 KB Output is correct
9 Correct 349 ms 91852 KB Output is correct
10 Correct 327 ms 91976 KB Output is correct
11 Correct 300 ms 91844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 62980 KB Output is correct
2 Correct 32 ms 63060 KB Output is correct
3 Correct 35 ms 62984 KB Output is correct
4 Correct 32 ms 63056 KB Output is correct
5 Correct 33 ms 63020 KB Output is correct
6 Correct 33 ms 63024 KB Output is correct
7 Correct 33 ms 62980 KB Output is correct
8 Correct 32 ms 63008 KB Output is correct
9 Correct 32 ms 63060 KB Output is correct
10 Correct 35 ms 63056 KB Output is correct
11 Correct 32 ms 63028 KB Output is correct
12 Correct 32 ms 62968 KB Output is correct
13 Correct 33 ms 62996 KB Output is correct
14 Correct 33 ms 62996 KB Output is correct
15 Correct 31 ms 62924 KB Output is correct
16 Correct 31 ms 63012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 62980 KB Output is correct
2 Correct 32 ms 63060 KB Output is correct
3 Correct 35 ms 62984 KB Output is correct
4 Correct 32 ms 63056 KB Output is correct
5 Correct 33 ms 63020 KB Output is correct
6 Correct 33 ms 63024 KB Output is correct
7 Correct 33 ms 62980 KB Output is correct
8 Correct 32 ms 63008 KB Output is correct
9 Correct 32 ms 63060 KB Output is correct
10 Correct 35 ms 63056 KB Output is correct
11 Correct 32 ms 63028 KB Output is correct
12 Correct 32 ms 62968 KB Output is correct
13 Correct 33 ms 62996 KB Output is correct
14 Correct 33 ms 62996 KB Output is correct
15 Correct 31 ms 62924 KB Output is correct
16 Correct 31 ms 63012 KB Output is correct
17 Correct 33 ms 63308 KB Output is correct
18 Correct 35 ms 63560 KB Output is correct
19 Correct 40 ms 63580 KB Output is correct
20 Correct 36 ms 63516 KB Output is correct
21 Correct 37 ms 63568 KB Output is correct
22 Correct 37 ms 63436 KB Output is correct
23 Correct 36 ms 63408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 62980 KB Output is correct
2 Correct 32 ms 63060 KB Output is correct
3 Correct 35 ms 62984 KB Output is correct
4 Correct 32 ms 63056 KB Output is correct
5 Correct 33 ms 63020 KB Output is correct
6 Correct 33 ms 63024 KB Output is correct
7 Correct 33 ms 62980 KB Output is correct
8 Correct 32 ms 63008 KB Output is correct
9 Correct 32 ms 63060 KB Output is correct
10 Correct 35 ms 63056 KB Output is correct
11 Correct 32 ms 63028 KB Output is correct
12 Correct 32 ms 62968 KB Output is correct
13 Correct 33 ms 62996 KB Output is correct
14 Correct 33 ms 62996 KB Output is correct
15 Correct 31 ms 62924 KB Output is correct
16 Correct 31 ms 63012 KB Output is correct
17 Correct 33 ms 63308 KB Output is correct
18 Correct 35 ms 63560 KB Output is correct
19 Correct 40 ms 63580 KB Output is correct
20 Correct 36 ms 63516 KB Output is correct
21 Correct 37 ms 63568 KB Output is correct
22 Correct 37 ms 63436 KB Output is correct
23 Correct 36 ms 63408 KB Output is correct
24 Correct 350 ms 100428 KB Output is correct
25 Correct 363 ms 100352 KB Output is correct
26 Correct 351 ms 100376 KB Output is correct
27 Correct 448 ms 115576 KB Output is correct
28 Correct 464 ms 102800 KB Output is correct
29 Correct 352 ms 100148 KB Output is correct
30 Correct 697 ms 114204 KB Output is correct
31 Correct 143 ms 75028 KB Output is correct
32 Correct 259 ms 91388 KB Output is correct
33 Correct 466 ms 101144 KB Output is correct
34 Correct 673 ms 115044 KB Output is correct
35 Correct 633 ms 110396 KB Output is correct
36 Correct 626 ms 110336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 62980 KB Output is correct
2 Correct 32 ms 63060 KB Output is correct
3 Correct 35 ms 62984 KB Output is correct
4 Correct 32 ms 63056 KB Output is correct
5 Correct 33 ms 63020 KB Output is correct
6 Correct 33 ms 63024 KB Output is correct
7 Correct 33 ms 62980 KB Output is correct
8 Correct 32 ms 63008 KB Output is correct
9 Correct 32 ms 63060 KB Output is correct
10 Correct 35 ms 63056 KB Output is correct
11 Correct 32 ms 63028 KB Output is correct
12 Correct 32 ms 62968 KB Output is correct
13 Correct 33 ms 62996 KB Output is correct
14 Correct 33 ms 62996 KB Output is correct
15 Correct 31 ms 62924 KB Output is correct
16 Correct 31 ms 63012 KB Output is correct
17 Correct 33 ms 63308 KB Output is correct
18 Correct 35 ms 63560 KB Output is correct
19 Correct 40 ms 63580 KB Output is correct
20 Correct 36 ms 63516 KB Output is correct
21 Correct 37 ms 63568 KB Output is correct
22 Correct 37 ms 63436 KB Output is correct
23 Correct 36 ms 63408 KB Output is correct
24 Correct 350 ms 100428 KB Output is correct
25 Correct 363 ms 100352 KB Output is correct
26 Correct 351 ms 100376 KB Output is correct
27 Correct 448 ms 115576 KB Output is correct
28 Correct 464 ms 102800 KB Output is correct
29 Correct 352 ms 100148 KB Output is correct
30 Correct 697 ms 114204 KB Output is correct
31 Correct 143 ms 75028 KB Output is correct
32 Correct 259 ms 91388 KB Output is correct
33 Correct 466 ms 101144 KB Output is correct
34 Correct 673 ms 115044 KB Output is correct
35 Correct 633 ms 110396 KB Output is correct
36 Correct 626 ms 110336 KB Output is correct
37 Correct 393 ms 99544 KB Output is correct
38 Correct 473 ms 114660 KB Output is correct
39 Correct 379 ms 99612 KB Output is correct
40 Correct 355 ms 99220 KB Output is correct
41 Correct 32 ms 63048 KB Output is correct
42 Correct 692 ms 113332 KB Output is correct
43 Correct 510 ms 100308 KB Output is correct
44 Correct 685 ms 114360 KB Output is correct
45 Correct 685 ms 113276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 62980 KB Output is correct
2 Correct 32 ms 63060 KB Output is correct
3 Correct 35 ms 62984 KB Output is correct
4 Correct 32 ms 63056 KB Output is correct
5 Correct 33 ms 63020 KB Output is correct
6 Correct 33 ms 63024 KB Output is correct
7 Correct 33 ms 62980 KB Output is correct
8 Correct 32 ms 63008 KB Output is correct
9 Correct 32 ms 63060 KB Output is correct
10 Correct 35 ms 63056 KB Output is correct
11 Correct 32 ms 63028 KB Output is correct
12 Correct 32 ms 62968 KB Output is correct
13 Correct 33 ms 62996 KB Output is correct
14 Correct 33 ms 62996 KB Output is correct
15 Correct 31 ms 62924 KB Output is correct
16 Correct 31 ms 63012 KB Output is correct
17 Correct 33 ms 63308 KB Output is correct
18 Correct 35 ms 63560 KB Output is correct
19 Correct 40 ms 63580 KB Output is correct
20 Correct 36 ms 63516 KB Output is correct
21 Correct 37 ms 63568 KB Output is correct
22 Correct 37 ms 63436 KB Output is correct
23 Correct 36 ms 63408 KB Output is correct
24 Correct 350 ms 100428 KB Output is correct
25 Correct 363 ms 100352 KB Output is correct
26 Correct 351 ms 100376 KB Output is correct
27 Correct 448 ms 115576 KB Output is correct
28 Correct 464 ms 102800 KB Output is correct
29 Correct 352 ms 100148 KB Output is correct
30 Correct 697 ms 114204 KB Output is correct
31 Correct 143 ms 75028 KB Output is correct
32 Correct 259 ms 91388 KB Output is correct
33 Correct 466 ms 101144 KB Output is correct
34 Correct 673 ms 115044 KB Output is correct
35 Correct 633 ms 110396 KB Output is correct
36 Correct 626 ms 110336 KB Output is correct
37 Correct 393 ms 99544 KB Output is correct
38 Correct 473 ms 114660 KB Output is correct
39 Correct 379 ms 99612 KB Output is correct
40 Correct 355 ms 99220 KB Output is correct
41 Correct 32 ms 63048 KB Output is correct
42 Correct 692 ms 113332 KB Output is correct
43 Correct 510 ms 100308 KB Output is correct
44 Correct 685 ms 114360 KB Output is correct
45 Correct 685 ms 113276 KB Output is correct
46 Correct 1918 ms 206148 KB Output is correct
47 Correct 2377 ms 267700 KB Output is correct
48 Correct 1723 ms 199488 KB Output is correct
49 Correct 1709 ms 199496 KB Output is correct
50 Correct 4198 ms 263500 KB Output is correct
51 Correct 2717 ms 238160 KB Output is correct
52 Correct 3874 ms 269100 KB Output is correct
53 Correct 3856 ms 264996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 357 ms 92140 KB Output is correct
2 Correct 362 ms 91620 KB Output is correct
3 Correct 341 ms 92240 KB Output is correct
4 Correct 355 ms 91688 KB Output is correct
5 Correct 33 ms 63088 KB Output is correct
6 Correct 361 ms 91300 KB Output is correct
7 Correct 113 ms 68768 KB Output is correct
8 Correct 250 ms 86548 KB Output is correct
9 Correct 349 ms 91852 KB Output is correct
10 Correct 327 ms 91976 KB Output is correct
11 Correct 300 ms 91844 KB Output is correct
12 Correct 32 ms 62980 KB Output is correct
13 Correct 32 ms 63060 KB Output is correct
14 Correct 35 ms 62984 KB Output is correct
15 Correct 32 ms 63056 KB Output is correct
16 Correct 33 ms 63020 KB Output is correct
17 Correct 33 ms 63024 KB Output is correct
18 Correct 33 ms 62980 KB Output is correct
19 Correct 32 ms 63008 KB Output is correct
20 Correct 32 ms 63060 KB Output is correct
21 Correct 35 ms 63056 KB Output is correct
22 Correct 32 ms 63028 KB Output is correct
23 Correct 32 ms 62968 KB Output is correct
24 Correct 33 ms 62996 KB Output is correct
25 Correct 33 ms 62996 KB Output is correct
26 Correct 31 ms 62924 KB Output is correct
27 Correct 31 ms 63012 KB Output is correct
28 Correct 33 ms 63308 KB Output is correct
29 Correct 35 ms 63560 KB Output is correct
30 Correct 40 ms 63580 KB Output is correct
31 Correct 36 ms 63516 KB Output is correct
32 Correct 37 ms 63568 KB Output is correct
33 Correct 37 ms 63436 KB Output is correct
34 Correct 36 ms 63408 KB Output is correct
35 Correct 350 ms 100428 KB Output is correct
36 Correct 363 ms 100352 KB Output is correct
37 Correct 351 ms 100376 KB Output is correct
38 Correct 448 ms 115576 KB Output is correct
39 Correct 464 ms 102800 KB Output is correct
40 Correct 352 ms 100148 KB Output is correct
41 Correct 697 ms 114204 KB Output is correct
42 Correct 143 ms 75028 KB Output is correct
43 Correct 259 ms 91388 KB Output is correct
44 Correct 466 ms 101144 KB Output is correct
45 Correct 673 ms 115044 KB Output is correct
46 Correct 633 ms 110396 KB Output is correct
47 Correct 626 ms 110336 KB Output is correct
48 Correct 393 ms 99544 KB Output is correct
49 Correct 473 ms 114660 KB Output is correct
50 Correct 379 ms 99612 KB Output is correct
51 Correct 355 ms 99220 KB Output is correct
52 Correct 32 ms 63048 KB Output is correct
53 Correct 692 ms 113332 KB Output is correct
54 Correct 510 ms 100308 KB Output is correct
55 Correct 685 ms 114360 KB Output is correct
56 Correct 685 ms 113276 KB Output is correct
57 Correct 379 ms 96344 KB Output is correct
58 Correct 522 ms 121008 KB Output is correct
59 Correct 374 ms 103732 KB Output is correct
60 Correct 369 ms 103672 KB Output is correct
61 Correct 688 ms 116828 KB Output is correct
62 Correct 34 ms 63028 KB Output is correct
63 Incorrect 736 ms 119836 KB Output isn't correct
64 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 357 ms 92140 KB Output is correct
2 Correct 362 ms 91620 KB Output is correct
3 Correct 341 ms 92240 KB Output is correct
4 Correct 355 ms 91688 KB Output is correct
5 Correct 33 ms 63088 KB Output is correct
6 Correct 361 ms 91300 KB Output is correct
7 Correct 113 ms 68768 KB Output is correct
8 Correct 250 ms 86548 KB Output is correct
9 Correct 349 ms 91852 KB Output is correct
10 Correct 327 ms 91976 KB Output is correct
11 Correct 300 ms 91844 KB Output is correct
12 Correct 32 ms 62980 KB Output is correct
13 Correct 32 ms 63060 KB Output is correct
14 Correct 35 ms 62984 KB Output is correct
15 Correct 32 ms 63056 KB Output is correct
16 Correct 33 ms 63020 KB Output is correct
17 Correct 33 ms 63024 KB Output is correct
18 Correct 33 ms 62980 KB Output is correct
19 Correct 32 ms 63008 KB Output is correct
20 Correct 32 ms 63060 KB Output is correct
21 Correct 35 ms 63056 KB Output is correct
22 Correct 32 ms 63028 KB Output is correct
23 Correct 32 ms 62968 KB Output is correct
24 Correct 33 ms 62996 KB Output is correct
25 Correct 33 ms 62996 KB Output is correct
26 Correct 31 ms 62924 KB Output is correct
27 Correct 31 ms 63012 KB Output is correct
28 Correct 33 ms 63308 KB Output is correct
29 Correct 35 ms 63560 KB Output is correct
30 Correct 40 ms 63580 KB Output is correct
31 Correct 36 ms 63516 KB Output is correct
32 Correct 37 ms 63568 KB Output is correct
33 Correct 37 ms 63436 KB Output is correct
34 Correct 36 ms 63408 KB Output is correct
35 Correct 350 ms 100428 KB Output is correct
36 Correct 363 ms 100352 KB Output is correct
37 Correct 351 ms 100376 KB Output is correct
38 Correct 448 ms 115576 KB Output is correct
39 Correct 464 ms 102800 KB Output is correct
40 Correct 352 ms 100148 KB Output is correct
41 Correct 697 ms 114204 KB Output is correct
42 Correct 143 ms 75028 KB Output is correct
43 Correct 259 ms 91388 KB Output is correct
44 Correct 466 ms 101144 KB Output is correct
45 Correct 673 ms 115044 KB Output is correct
46 Correct 633 ms 110396 KB Output is correct
47 Correct 626 ms 110336 KB Output is correct
48 Correct 393 ms 99544 KB Output is correct
49 Correct 473 ms 114660 KB Output is correct
50 Correct 379 ms 99612 KB Output is correct
51 Correct 355 ms 99220 KB Output is correct
52 Correct 32 ms 63048 KB Output is correct
53 Correct 692 ms 113332 KB Output is correct
54 Correct 510 ms 100308 KB Output is correct
55 Correct 685 ms 114360 KB Output is correct
56 Correct 685 ms 113276 KB Output is correct
57 Correct 1918 ms 206148 KB Output is correct
58 Correct 2377 ms 267700 KB Output is correct
59 Correct 1723 ms 199488 KB Output is correct
60 Correct 1709 ms 199496 KB Output is correct
61 Correct 4198 ms 263500 KB Output is correct
62 Correct 2717 ms 238160 KB Output is correct
63 Correct 3874 ms 269100 KB Output is correct
64 Correct 3856 ms 264996 KB Output is correct
65 Correct 379 ms 96344 KB Output is correct
66 Correct 522 ms 121008 KB Output is correct
67 Correct 374 ms 103732 KB Output is correct
68 Correct 369 ms 103672 KB Output is correct
69 Correct 688 ms 116828 KB Output is correct
70 Correct 34 ms 63028 KB Output is correct
71 Incorrect 736 ms 119836 KB Output isn't correct
72 Halted 0 ms 0 KB -