답안 #781863

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
781863 2023-07-13T12:07:36 Z TeaTime Two Dishes (JOI19_dishes) C++17
100 / 100
5521 ms 432332 KB
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <cassert>

using namespace std;

typedef long long ll;
typedef long double ld;

#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

const ll SZ = 1'000'100, INF = 2'000'000'000'000'000'000;

#define int ll

ll n, m;

struct el {
    ll a, s, p;
};

vector<el> vec1, vec2;
vector<ll> pref1, pref2;

struct point {
    ll x, y, val;
};

vector<point> blue, red;

ll seg[SZ * 8], psh[SZ * 8];

void push(int v) {
    seg[v * 2 + 1] += psh[v];
    seg[v * 2 + 2] += psh[v];

    psh[v * 2 + 1] += psh[v];
    psh[v * 2 + 2] += psh[v];

    psh[v] = 0;
}

void upd(int v, int l, int r, int askl, int askr, ll val) {
    push(v);
    if (l >= askr || r <= askl) return;

    if (askl <= l && r <= askr) {
        seg[v] += val;
        psh[v] += val;
        return;
    }

    int mid = (l + r) / 2;
    upd(v * 2 + 1, l, mid, askl, askr, val);
    upd(v * 2 + 2, mid, r, askl, askr, val);
    seg[v] = max(seg[v * 2 + 1], seg[v * 2 + 2]);
}

void upd(int v, int l, int r, int pos, ll val) {
    push(v);
    if (l == r - 1) {
        seg[v] = val;
    } else {
        int mid = (l + r) / 2;

        if (pos < mid) {
            upd(v * 2 + 1, l, mid, pos, val);
        } else {
            upd(v * 2 + 2, mid, r, pos, val);
        }

        seg[v] = max(seg[v * 2 + 1], seg[v * 2 + 2]);
    }
}

ll ask(int v, int l, int r, int askl, int askr) {
    push(v);
    if (l >= askr || r <= askl) return -INF;

    if (askl <= l && r <= askr) return seg[v];

    int mid = (l + r) / 2;
    return max(ask(v * 2 + 1, l, mid, askl, askr), ask(v * 2 + 2, mid, r, askl, askr));
}

vector<point> points[SZ];

bool cmp(point a, point b) {
    return a.y < b.y;
}

signed main() {
    fastInp;

    cin >> n >> m;

    vec1.resize(n);
    vec2.resize(m);

    pref1.push_back(0);
    for (auto &c : vec1) {
        cin >> c.a >> c.s >> c.p;
        pref1.push_back(pref1.back() + c.a);
    }

    pref2.push_back(0);
    for (auto &c : vec2) {
        cin >> c.a >> c.s >> c.p;
        pref2.push_back(pref2.back() + c.a);
    }

    for (int i = 0; i < SZ; i++) {
        seg[i] = -INF;
    }

    upd(0, 0, SZ, 0, 0);

    // x is vec1
    // y is vec2
    ll ans = 0, prefsum = 0;
    for (int i = 0; i < n; i++) {
        prefsum += vec1[i].a;

        ll left = vec1[i].s - prefsum;
        if (left < 0) continue;

        int ind = upper_bound(pref2.begin(), pref2.end(), left) - pref2.begin() - 1;
        assert(ind >= 0);

        blue.push_back({i + 1, ind, vec1[i].p}); // above or on the lattice path
    }

    prefsum = 0;

    for (int i = 0; i < m; i++) {
        prefsum += vec2[i].a;

        ll left = vec2[i].s - prefsum;
        if (left < 0) continue;

        int ind = upper_bound(pref1.begin(), pref1.end(), left) - pref1.begin() - 1;
        assert(ind >= 0);

        red.push_back({ind, i + 1, vec2[i].p}); // under or on the lattice path
        blue.push_back({ind + 1, i, -vec2[i].p}); // above or on the lattice path
        ans += vec2[i].p;
    }

    for (auto c : blue) {
        points[c.x].push_back(c);
        if (c.x != 0) points[c.x - 1].push_back({c.x - 1, c.y + 1, 0});
    }

    points[n].push_back({n, m, 0});

    for (int i = 0; i < SZ; i++) {
        sort(points[i].begin(), points[i].end(), cmp);

        vector<point> cur;
        for (auto c : points[i]) {
            if (!cur.empty() && cur.back().y == c.y) {
                cur.back().val += c.val;
            } else {
                cur.push_back(c);
            }
        }

        points[i] = cur;

        for (auto c : points[i]) {
            upd(0, 0, SZ, 0, c.y + 1, c.val);
            ll q = ask(0, 0, SZ, 0, c.y + 1);
            upd(0, 0, SZ, c.y, q);
        }
    }

    cout << ans + ask(0, 0, SZ, m, m + 1);

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 382 ms 92060 KB Output is correct
2 Correct 447 ms 101272 KB Output is correct
3 Correct 639 ms 108852 KB Output is correct
4 Correct 246 ms 77652 KB Output is correct
5 Correct 19 ms 31736 KB Output is correct
6 Correct 467 ms 99696 KB Output is correct
7 Correct 303 ms 81612 KB Output is correct
8 Correct 320 ms 59176 KB Output is correct
9 Correct 623 ms 108456 KB Output is correct
10 Correct 280 ms 95496 KB Output is correct
11 Correct 584 ms 108612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 31632 KB Output is correct
2 Correct 18 ms 31712 KB Output is correct
3 Correct 18 ms 31740 KB Output is correct
4 Correct 17 ms 31656 KB Output is correct
5 Correct 19 ms 31740 KB Output is correct
6 Correct 21 ms 31740 KB Output is correct
7 Correct 18 ms 31740 KB Output is correct
8 Correct 18 ms 31700 KB Output is correct
9 Correct 18 ms 31688 KB Output is correct
10 Correct 23 ms 31740 KB Output is correct
11 Correct 20 ms 31648 KB Output is correct
12 Correct 18 ms 31716 KB Output is correct
13 Correct 22 ms 31744 KB Output is correct
14 Correct 18 ms 31700 KB Output is correct
15 Correct 18 ms 31700 KB Output is correct
16 Correct 18 ms 31708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 31632 KB Output is correct
2 Correct 18 ms 31712 KB Output is correct
3 Correct 18 ms 31740 KB Output is correct
4 Correct 17 ms 31656 KB Output is correct
5 Correct 19 ms 31740 KB Output is correct
6 Correct 21 ms 31740 KB Output is correct
7 Correct 18 ms 31740 KB Output is correct
8 Correct 18 ms 31700 KB Output is correct
9 Correct 18 ms 31688 KB Output is correct
10 Correct 23 ms 31740 KB Output is correct
11 Correct 20 ms 31648 KB Output is correct
12 Correct 18 ms 31716 KB Output is correct
13 Correct 22 ms 31744 KB Output is correct
14 Correct 18 ms 31700 KB Output is correct
15 Correct 18 ms 31700 KB Output is correct
16 Correct 18 ms 31708 KB Output is correct
17 Correct 23 ms 32276 KB Output is correct
18 Correct 23 ms 32472 KB Output is correct
19 Correct 24 ms 32444 KB Output is correct
20 Correct 22 ms 32204 KB Output is correct
21 Correct 25 ms 32520 KB Output is correct
22 Correct 24 ms 32456 KB Output is correct
23 Correct 23 ms 32468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 31632 KB Output is correct
2 Correct 18 ms 31712 KB Output is correct
3 Correct 18 ms 31740 KB Output is correct
4 Correct 17 ms 31656 KB Output is correct
5 Correct 19 ms 31740 KB Output is correct
6 Correct 21 ms 31740 KB Output is correct
7 Correct 18 ms 31740 KB Output is correct
8 Correct 18 ms 31700 KB Output is correct
9 Correct 18 ms 31688 KB Output is correct
10 Correct 23 ms 31740 KB Output is correct
11 Correct 20 ms 31648 KB Output is correct
12 Correct 18 ms 31716 KB Output is correct
13 Correct 22 ms 31744 KB Output is correct
14 Correct 18 ms 31700 KB Output is correct
15 Correct 18 ms 31700 KB Output is correct
16 Correct 18 ms 31708 KB Output is correct
17 Correct 23 ms 32276 KB Output is correct
18 Correct 23 ms 32472 KB Output is correct
19 Correct 24 ms 32444 KB Output is correct
20 Correct 22 ms 32204 KB Output is correct
21 Correct 25 ms 32520 KB Output is correct
22 Correct 24 ms 32456 KB Output is correct
23 Correct 23 ms 32468 KB Output is correct
24 Correct 520 ms 92068 KB Output is correct
25 Correct 507 ms 99724 KB Output is correct
26 Correct 529 ms 92048 KB Output is correct
27 Correct 506 ms 91204 KB Output is correct
28 Correct 612 ms 100264 KB Output is correct
29 Correct 617 ms 108008 KB Output is correct
30 Correct 854 ms 102572 KB Output is correct
31 Correct 293 ms 85100 KB Output is correct
32 Correct 279 ms 58452 KB Output is correct
33 Correct 497 ms 80004 KB Output is correct
34 Correct 790 ms 100840 KB Output is correct
35 Correct 893 ms 103196 KB Output is correct
36 Correct 833 ms 102408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 31632 KB Output is correct
2 Correct 18 ms 31712 KB Output is correct
3 Correct 18 ms 31740 KB Output is correct
4 Correct 17 ms 31656 KB Output is correct
5 Correct 19 ms 31740 KB Output is correct
6 Correct 21 ms 31740 KB Output is correct
7 Correct 18 ms 31740 KB Output is correct
8 Correct 18 ms 31700 KB Output is correct
9 Correct 18 ms 31688 KB Output is correct
10 Correct 23 ms 31740 KB Output is correct
11 Correct 20 ms 31648 KB Output is correct
12 Correct 18 ms 31716 KB Output is correct
13 Correct 22 ms 31744 KB Output is correct
14 Correct 18 ms 31700 KB Output is correct
15 Correct 18 ms 31700 KB Output is correct
16 Correct 18 ms 31708 KB Output is correct
17 Correct 23 ms 32276 KB Output is correct
18 Correct 23 ms 32472 KB Output is correct
19 Correct 24 ms 32444 KB Output is correct
20 Correct 22 ms 32204 KB Output is correct
21 Correct 25 ms 32520 KB Output is correct
22 Correct 24 ms 32456 KB Output is correct
23 Correct 23 ms 32468 KB Output is correct
24 Correct 520 ms 92068 KB Output is correct
25 Correct 507 ms 99724 KB Output is correct
26 Correct 529 ms 92048 KB Output is correct
27 Correct 506 ms 91204 KB Output is correct
28 Correct 612 ms 100264 KB Output is correct
29 Correct 617 ms 108008 KB Output is correct
30 Correct 854 ms 102572 KB Output is correct
31 Correct 293 ms 85100 KB Output is correct
32 Correct 279 ms 58452 KB Output is correct
33 Correct 497 ms 80004 KB Output is correct
34 Correct 790 ms 100840 KB Output is correct
35 Correct 893 ms 103196 KB Output is correct
36 Correct 833 ms 102408 KB Output is correct
37 Correct 426 ms 91816 KB Output is correct
38 Correct 426 ms 90972 KB Output is correct
39 Correct 296 ms 95056 KB Output is correct
40 Correct 306 ms 94896 KB Output is correct
41 Correct 19 ms 31632 KB Output is correct
42 Correct 1002 ms 102448 KB Output is correct
43 Correct 523 ms 79724 KB Output is correct
44 Correct 876 ms 100224 KB Output is correct
45 Correct 922 ms 103188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 31632 KB Output is correct
2 Correct 18 ms 31712 KB Output is correct
3 Correct 18 ms 31740 KB Output is correct
4 Correct 17 ms 31656 KB Output is correct
5 Correct 19 ms 31740 KB Output is correct
6 Correct 21 ms 31740 KB Output is correct
7 Correct 18 ms 31740 KB Output is correct
8 Correct 18 ms 31700 KB Output is correct
9 Correct 18 ms 31688 KB Output is correct
10 Correct 23 ms 31740 KB Output is correct
11 Correct 20 ms 31648 KB Output is correct
12 Correct 18 ms 31716 KB Output is correct
13 Correct 22 ms 31744 KB Output is correct
14 Correct 18 ms 31700 KB Output is correct
15 Correct 18 ms 31700 KB Output is correct
16 Correct 18 ms 31708 KB Output is correct
17 Correct 23 ms 32276 KB Output is correct
18 Correct 23 ms 32472 KB Output is correct
19 Correct 24 ms 32444 KB Output is correct
20 Correct 22 ms 32204 KB Output is correct
21 Correct 25 ms 32520 KB Output is correct
22 Correct 24 ms 32456 KB Output is correct
23 Correct 23 ms 32468 KB Output is correct
24 Correct 520 ms 92068 KB Output is correct
25 Correct 507 ms 99724 KB Output is correct
26 Correct 529 ms 92048 KB Output is correct
27 Correct 506 ms 91204 KB Output is correct
28 Correct 612 ms 100264 KB Output is correct
29 Correct 617 ms 108008 KB Output is correct
30 Correct 854 ms 102572 KB Output is correct
31 Correct 293 ms 85100 KB Output is correct
32 Correct 279 ms 58452 KB Output is correct
33 Correct 497 ms 80004 KB Output is correct
34 Correct 790 ms 100840 KB Output is correct
35 Correct 893 ms 103196 KB Output is correct
36 Correct 833 ms 102408 KB Output is correct
37 Correct 426 ms 91816 KB Output is correct
38 Correct 426 ms 90972 KB Output is correct
39 Correct 296 ms 95056 KB Output is correct
40 Correct 306 ms 94896 KB Output is correct
41 Correct 19 ms 31632 KB Output is correct
42 Correct 1002 ms 102448 KB Output is correct
43 Correct 523 ms 79724 KB Output is correct
44 Correct 876 ms 100224 KB Output is correct
45 Correct 922 ms 103188 KB Output is correct
46 Correct 2033 ms 314052 KB Output is correct
47 Correct 2075 ms 314264 KB Output is correct
48 Correct 1407 ms 334328 KB Output is correct
49 Correct 1414 ms 334276 KB Output is correct
50 Correct 5071 ms 373820 KB Output is correct
51 Correct 2935 ms 255928 KB Output is correct
52 Correct 4255 ms 350760 KB Output is correct
53 Correct 4815 ms 369472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 382 ms 92060 KB Output is correct
2 Correct 447 ms 101272 KB Output is correct
3 Correct 639 ms 108852 KB Output is correct
4 Correct 246 ms 77652 KB Output is correct
5 Correct 19 ms 31736 KB Output is correct
6 Correct 467 ms 99696 KB Output is correct
7 Correct 303 ms 81612 KB Output is correct
8 Correct 320 ms 59176 KB Output is correct
9 Correct 623 ms 108456 KB Output is correct
10 Correct 280 ms 95496 KB Output is correct
11 Correct 584 ms 108612 KB Output is correct
12 Correct 21 ms 31632 KB Output is correct
13 Correct 18 ms 31712 KB Output is correct
14 Correct 18 ms 31740 KB Output is correct
15 Correct 17 ms 31656 KB Output is correct
16 Correct 19 ms 31740 KB Output is correct
17 Correct 21 ms 31740 KB Output is correct
18 Correct 18 ms 31740 KB Output is correct
19 Correct 18 ms 31700 KB Output is correct
20 Correct 18 ms 31688 KB Output is correct
21 Correct 23 ms 31740 KB Output is correct
22 Correct 20 ms 31648 KB Output is correct
23 Correct 18 ms 31716 KB Output is correct
24 Correct 22 ms 31744 KB Output is correct
25 Correct 18 ms 31700 KB Output is correct
26 Correct 18 ms 31700 KB Output is correct
27 Correct 18 ms 31708 KB Output is correct
28 Correct 23 ms 32276 KB Output is correct
29 Correct 23 ms 32472 KB Output is correct
30 Correct 24 ms 32444 KB Output is correct
31 Correct 22 ms 32204 KB Output is correct
32 Correct 25 ms 32520 KB Output is correct
33 Correct 24 ms 32456 KB Output is correct
34 Correct 23 ms 32468 KB Output is correct
35 Correct 520 ms 92068 KB Output is correct
36 Correct 507 ms 99724 KB Output is correct
37 Correct 529 ms 92048 KB Output is correct
38 Correct 506 ms 91204 KB Output is correct
39 Correct 612 ms 100264 KB Output is correct
40 Correct 617 ms 108008 KB Output is correct
41 Correct 854 ms 102572 KB Output is correct
42 Correct 293 ms 85100 KB Output is correct
43 Correct 279 ms 58452 KB Output is correct
44 Correct 497 ms 80004 KB Output is correct
45 Correct 790 ms 100840 KB Output is correct
46 Correct 893 ms 103196 KB Output is correct
47 Correct 833 ms 102408 KB Output is correct
48 Correct 426 ms 91816 KB Output is correct
49 Correct 426 ms 90972 KB Output is correct
50 Correct 296 ms 95056 KB Output is correct
51 Correct 306 ms 94896 KB Output is correct
52 Correct 19 ms 31632 KB Output is correct
53 Correct 1002 ms 102448 KB Output is correct
54 Correct 523 ms 79724 KB Output is correct
55 Correct 876 ms 100224 KB Output is correct
56 Correct 922 ms 103188 KB Output is correct
57 Correct 516 ms 89268 KB Output is correct
58 Correct 511 ms 102148 KB Output is correct
59 Correct 298 ms 103816 KB Output is correct
60 Correct 300 ms 103716 KB Output is correct
61 Correct 897 ms 111080 KB Output is correct
62 Correct 18 ms 31700 KB Output is correct
63 Correct 874 ms 113396 KB Output is correct
64 Correct 500 ms 89480 KB Output is correct
65 Correct 778 ms 112508 KB Output is correct
66 Correct 882 ms 106628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 382 ms 92060 KB Output is correct
2 Correct 447 ms 101272 KB Output is correct
3 Correct 639 ms 108852 KB Output is correct
4 Correct 246 ms 77652 KB Output is correct
5 Correct 19 ms 31736 KB Output is correct
6 Correct 467 ms 99696 KB Output is correct
7 Correct 303 ms 81612 KB Output is correct
8 Correct 320 ms 59176 KB Output is correct
9 Correct 623 ms 108456 KB Output is correct
10 Correct 280 ms 95496 KB Output is correct
11 Correct 584 ms 108612 KB Output is correct
12 Correct 21 ms 31632 KB Output is correct
13 Correct 18 ms 31712 KB Output is correct
14 Correct 18 ms 31740 KB Output is correct
15 Correct 17 ms 31656 KB Output is correct
16 Correct 19 ms 31740 KB Output is correct
17 Correct 21 ms 31740 KB Output is correct
18 Correct 18 ms 31740 KB Output is correct
19 Correct 18 ms 31700 KB Output is correct
20 Correct 18 ms 31688 KB Output is correct
21 Correct 23 ms 31740 KB Output is correct
22 Correct 20 ms 31648 KB Output is correct
23 Correct 18 ms 31716 KB Output is correct
24 Correct 22 ms 31744 KB Output is correct
25 Correct 18 ms 31700 KB Output is correct
26 Correct 18 ms 31700 KB Output is correct
27 Correct 18 ms 31708 KB Output is correct
28 Correct 23 ms 32276 KB Output is correct
29 Correct 23 ms 32472 KB Output is correct
30 Correct 24 ms 32444 KB Output is correct
31 Correct 22 ms 32204 KB Output is correct
32 Correct 25 ms 32520 KB Output is correct
33 Correct 24 ms 32456 KB Output is correct
34 Correct 23 ms 32468 KB Output is correct
35 Correct 520 ms 92068 KB Output is correct
36 Correct 507 ms 99724 KB Output is correct
37 Correct 529 ms 92048 KB Output is correct
38 Correct 506 ms 91204 KB Output is correct
39 Correct 612 ms 100264 KB Output is correct
40 Correct 617 ms 108008 KB Output is correct
41 Correct 854 ms 102572 KB Output is correct
42 Correct 293 ms 85100 KB Output is correct
43 Correct 279 ms 58452 KB Output is correct
44 Correct 497 ms 80004 KB Output is correct
45 Correct 790 ms 100840 KB Output is correct
46 Correct 893 ms 103196 KB Output is correct
47 Correct 833 ms 102408 KB Output is correct
48 Correct 426 ms 91816 KB Output is correct
49 Correct 426 ms 90972 KB Output is correct
50 Correct 296 ms 95056 KB Output is correct
51 Correct 306 ms 94896 KB Output is correct
52 Correct 19 ms 31632 KB Output is correct
53 Correct 1002 ms 102448 KB Output is correct
54 Correct 523 ms 79724 KB Output is correct
55 Correct 876 ms 100224 KB Output is correct
56 Correct 922 ms 103188 KB Output is correct
57 Correct 2033 ms 314052 KB Output is correct
58 Correct 2075 ms 314264 KB Output is correct
59 Correct 1407 ms 334328 KB Output is correct
60 Correct 1414 ms 334276 KB Output is correct
61 Correct 5071 ms 373820 KB Output is correct
62 Correct 2935 ms 255928 KB Output is correct
63 Correct 4255 ms 350760 KB Output is correct
64 Correct 4815 ms 369472 KB Output is correct
65 Correct 516 ms 89268 KB Output is correct
66 Correct 511 ms 102148 KB Output is correct
67 Correct 298 ms 103816 KB Output is correct
68 Correct 300 ms 103716 KB Output is correct
69 Correct 897 ms 111080 KB Output is correct
70 Correct 18 ms 31700 KB Output is correct
71 Correct 874 ms 113396 KB Output is correct
72 Correct 500 ms 89480 KB Output is correct
73 Correct 778 ms 112508 KB Output is correct
74 Correct 882 ms 106628 KB Output is correct
75 Correct 2557 ms 385072 KB Output is correct
76 Correct 2839 ms 384496 KB Output is correct
77 Correct 1480 ms 390348 KB Output is correct
78 Correct 1481 ms 386360 KB Output is correct
79 Correct 5521 ms 432332 KB Output is correct
80 Correct 2962 ms 316184 KB Output is correct
81 Correct 4486 ms 419536 KB Output is correct
82 Correct 5323 ms 407136 KB Output is correct
83 Correct 5182 ms 418064 KB Output is correct