답안 #709709

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
709709 2023-03-14T08:22:44 Z PixelCat Two Dishes (JOI19_dishes) C++14
100 / 100
4280 ms 258388 KB
#include <bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; i++)
#define Forr(i, a, b) for(int i = a; i >= b; i--)
#define F first
#define S second
#define sz(x) ((int)x.size())
#define all(x) x.begin(), x.end()
#define eb emplace_back
#define int LL
using namespace std;
using LL = long long;
using pii = pair<int, int>;
 
const int MAXN = 1000010;
const int INF = 8e18;
 
// range add, range max
#define L(id) ((id) * 2 + 1)
#define R(id) ((id) * 2 + 2)
struct SegTree {
    struct Node {
        int mx, add;
    } a[MAXN << 2];
    void init(int val) {
        For(i, 0, (MAXN << 2) - 1) {
            a[i].mx = val;
            a[i].add = 0;
        }
    }
    void _add(int id, int x) {
        a[id].add += x;
        a[id].mx += x;
    }
    void pull(int id) {
        a[id].mx = max(a[L(id)].mx, a[R(id)].mx);
    }
    void push(int id) {
        if(a[id].add != 0) {
            _add(L(id), a[id].add);
            _add(R(id), a[id].add);
            a[id].add = 0;
        }
    }
    void add(int id, int l, int r, int L, int R, int x) {
        // if(l > R || r < L) return;
        if(l >= L && r <= R) {
            _add(id, x);
            return;
        }
        push(id);
        int m = (l + r) / 2;
        if(L <= m) add(L(id), l, m, L, R, x);
        if(R > m)  add(R(id), m + 1, r, L, R, x);
        pull(id);
    }
    void set(int id, int l, int r, int i, int x) {
        if(l == r) {
            a[id].mx = max(a[id].mx, x);
            a[id].add = 0;
            return;
        }
        push(id);
        int m = (l + r) / 2;
        if(i <= m) set(L(id), l, m, i, x);
        else       set(R(id), m + 1, r, i, x);
        pull(id);
    }
    int ask(int id, int l, int r, int L, int R) {
        if(l >= L && r <= R) return a[id].mx;
        push(id);
        int m = (l + r) / 2;
        int res = -INF;
        if(L <= m) res = max(res, ask(L(id), l, m, L, R));
        if(R > m)  res = max(res, ask(R(id), m + 1, r, L, R));
        return res;
    }
} seg, seg2, seg3;
 
int a1[MAXN];
int s1[MAXN];
int p1[MAXN];
int pr1[MAXN];
int t1[MAXN];
 
int a2[MAXN];
int s2[MAXN];
int p2[MAXN];
int pr2[MAXN];
int t2[MAXN];
 
// int su[MAXN][MAXN];
int dp[MAXN];
 
int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    // NYA =^-w-^=
    int n, m;
// #define QWQ
#ifndef QWQ
    cin >> n >> m;
    assert(n <= MAXN);
    For(i, 1, n) {
        cin >> a1[i] >> s1[i] >> p1[i];
        pr1[i] = pr1[i - 1] + a1[i];
    }
    For(i, 1, m) {
        cin >> a2[i] >> s2[i] >> p2[i];
        pr2[i] = pr2[i - 1] + a2[i];
    }
#else
    mt19937 rng(48763);
    n = m = 5;
    For(i, 1, n) {
        a1[i] = rng() % 10 + 1;
        s1[i] = rng() % 100 + 1;
        p1[i] = 1;
        pr1[i] = pr1[i - 1] + a1[i];
    }
    For(i, 1, m) {
        a2[i] = rng() % 10 + 1;
        s2[i] = rng() % 100 + 1;
        p2[i] = 1;
        pr2[i] = pr2[i - 1] + a2[i];
    }
#endif
 
    vector<pair<pii, int>> v;
    For(i, 1, n) {
        auto it = upper_bound(pr2, pr2 + m + 1, s1[i] - pr1[i]);
        t1[i] = it - pr2 - 1;
        if(t1[i] >= 0) v.eb(pii(t1[i], i), 1);
    }
    
    For(i, 1, m) {
        auto it = upper_bound(pr1, pr1 + n + 1, s2[i] - pr2[i]);
        t2[i] = it - pr1 - 1;
        if(p2[i] < 0 && t2[i] < n && t2[i] >= 0) v.eb(pii(i - 1, t2[i] + 1), 0);
    }
 
    v.eb(pii(m, n + 1), 0);
    sort(all(v));
    reverse(all(v));
    
    seg.init(-INF);
    seg2.init(0);
    for(auto &i:v) if(i.S) {
        seg2.add(0, 0, n + 1, i.F.S, n + 1, p1[i.F.S]);
    }
    seg.set(0, 0, n + 1, 0, 0);
 
    For(it, 0, m) {
        if(it && (t2[it] >= 0)) {
            seg.add(0, 0, n + 1, 0, t2[it], p2[it]);
        }
        int last = 0;
        while(sz(v) && v.back().F.F == it) {
            int i = v.back().F.S;
            int owo = v.back().S;
            // cout << v.back().F.F << " " << i << " " << owo << "\n" << flush;
            v.pop_back();
 
            if(owo) {
                seg2.add(0, 0, n + 1, i, n + 1, -p1[i]);
                seg.add(0, 0, n + 1, i, n + 1, p1[i]);
            }
            
            int val = seg.ask(0, 0, n + 1, last, i - 1) + seg2.ask(0, 0, n + 1, i, i);
            if(owo) val += p1[i];
            seg.set(0, 0, n + 1, i, val - seg2.ask(0, 0, n + 1, i, i));
            last = i;
        }
    }
    cout << seg.ask(0, 0, n + 1, n + 1, n + 1) << "\n";
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 404 ms 146608 KB Output is correct
2 Correct 452 ms 146780 KB Output is correct
3 Correct 449 ms 147628 KB Output is correct
4 Correct 422 ms 145724 KB Output is correct
5 Correct 51 ms 125520 KB Output is correct
6 Correct 476 ms 146984 KB Output is correct
7 Correct 149 ms 135340 KB Output is correct
8 Correct 333 ms 140100 KB Output is correct
9 Correct 462 ms 147780 KB Output is correct
10 Correct 437 ms 147968 KB Output is correct
11 Correct 416 ms 148156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 125568 KB Output is correct
2 Correct 52 ms 125628 KB Output is correct
3 Correct 50 ms 125552 KB Output is correct
4 Correct 53 ms 125524 KB Output is correct
5 Correct 55 ms 125516 KB Output is correct
6 Correct 51 ms 125592 KB Output is correct
7 Correct 55 ms 125624 KB Output is correct
8 Correct 56 ms 125628 KB Output is correct
9 Correct 54 ms 125632 KB Output is correct
10 Correct 54 ms 125628 KB Output is correct
11 Correct 52 ms 125520 KB Output is correct
12 Correct 68 ms 125596 KB Output is correct
13 Correct 59 ms 125516 KB Output is correct
14 Correct 56 ms 125548 KB Output is correct
15 Correct 55 ms 125532 KB Output is correct
16 Correct 54 ms 125516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 125568 KB Output is correct
2 Correct 52 ms 125628 KB Output is correct
3 Correct 50 ms 125552 KB Output is correct
4 Correct 53 ms 125524 KB Output is correct
5 Correct 55 ms 125516 KB Output is correct
6 Correct 51 ms 125592 KB Output is correct
7 Correct 55 ms 125624 KB Output is correct
8 Correct 56 ms 125628 KB Output is correct
9 Correct 54 ms 125632 KB Output is correct
10 Correct 54 ms 125628 KB Output is correct
11 Correct 52 ms 125520 KB Output is correct
12 Correct 68 ms 125596 KB Output is correct
13 Correct 59 ms 125516 KB Output is correct
14 Correct 56 ms 125548 KB Output is correct
15 Correct 55 ms 125532 KB Output is correct
16 Correct 54 ms 125516 KB Output is correct
17 Correct 60 ms 125884 KB Output is correct
18 Correct 57 ms 125908 KB Output is correct
19 Correct 75 ms 125960 KB Output is correct
20 Correct 61 ms 125860 KB Output is correct
21 Correct 64 ms 125900 KB Output is correct
22 Correct 56 ms 125888 KB Output is correct
23 Correct 73 ms 125812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 125568 KB Output is correct
2 Correct 52 ms 125628 KB Output is correct
3 Correct 50 ms 125552 KB Output is correct
4 Correct 53 ms 125524 KB Output is correct
5 Correct 55 ms 125516 KB Output is correct
6 Correct 51 ms 125592 KB Output is correct
7 Correct 55 ms 125624 KB Output is correct
8 Correct 56 ms 125628 KB Output is correct
9 Correct 54 ms 125632 KB Output is correct
10 Correct 54 ms 125628 KB Output is correct
11 Correct 52 ms 125520 KB Output is correct
12 Correct 68 ms 125596 KB Output is correct
13 Correct 59 ms 125516 KB Output is correct
14 Correct 56 ms 125548 KB Output is correct
15 Correct 55 ms 125532 KB Output is correct
16 Correct 54 ms 125516 KB Output is correct
17 Correct 60 ms 125884 KB Output is correct
18 Correct 57 ms 125908 KB Output is correct
19 Correct 75 ms 125960 KB Output is correct
20 Correct 61 ms 125860 KB Output is correct
21 Correct 64 ms 125900 KB Output is correct
22 Correct 56 ms 125888 KB Output is correct
23 Correct 73 ms 125812 KB Output is correct
24 Correct 392 ms 147888 KB Output is correct
25 Correct 294 ms 145616 KB Output is correct
26 Correct 390 ms 147876 KB Output is correct
27 Correct 312 ms 145548 KB Output is correct
28 Correct 467 ms 147368 KB Output is correct
29 Correct 434 ms 147816 KB Output is correct
30 Correct 639 ms 147932 KB Output is correct
31 Correct 127 ms 134776 KB Output is correct
32 Correct 294 ms 139672 KB Output is correct
33 Correct 427 ms 145416 KB Output is correct
34 Correct 621 ms 147596 KB Output is correct
35 Correct 634 ms 147416 KB Output is correct
36 Correct 795 ms 147372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 125568 KB Output is correct
2 Correct 52 ms 125628 KB Output is correct
3 Correct 50 ms 125552 KB Output is correct
4 Correct 53 ms 125524 KB Output is correct
5 Correct 55 ms 125516 KB Output is correct
6 Correct 51 ms 125592 KB Output is correct
7 Correct 55 ms 125624 KB Output is correct
8 Correct 56 ms 125628 KB Output is correct
9 Correct 54 ms 125632 KB Output is correct
10 Correct 54 ms 125628 KB Output is correct
11 Correct 52 ms 125520 KB Output is correct
12 Correct 68 ms 125596 KB Output is correct
13 Correct 59 ms 125516 KB Output is correct
14 Correct 56 ms 125548 KB Output is correct
15 Correct 55 ms 125532 KB Output is correct
16 Correct 54 ms 125516 KB Output is correct
17 Correct 60 ms 125884 KB Output is correct
18 Correct 57 ms 125908 KB Output is correct
19 Correct 75 ms 125960 KB Output is correct
20 Correct 61 ms 125860 KB Output is correct
21 Correct 64 ms 125900 KB Output is correct
22 Correct 56 ms 125888 KB Output is correct
23 Correct 73 ms 125812 KB Output is correct
24 Correct 392 ms 147888 KB Output is correct
25 Correct 294 ms 145616 KB Output is correct
26 Correct 390 ms 147876 KB Output is correct
27 Correct 312 ms 145548 KB Output is correct
28 Correct 467 ms 147368 KB Output is correct
29 Correct 434 ms 147816 KB Output is correct
30 Correct 639 ms 147932 KB Output is correct
31 Correct 127 ms 134776 KB Output is correct
32 Correct 294 ms 139672 KB Output is correct
33 Correct 427 ms 145416 KB Output is correct
34 Correct 621 ms 147596 KB Output is correct
35 Correct 634 ms 147416 KB Output is correct
36 Correct 795 ms 147372 KB Output is correct
37 Correct 464 ms 147260 KB Output is correct
38 Correct 344 ms 145060 KB Output is correct
39 Correct 424 ms 147452 KB Output is correct
40 Correct 445 ms 147412 KB Output is correct
41 Correct 55 ms 125608 KB Output is correct
42 Correct 674 ms 147172 KB Output is correct
43 Correct 441 ms 144616 KB Output is correct
44 Correct 672 ms 147012 KB Output is correct
45 Correct 651 ms 148252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 125568 KB Output is correct
2 Correct 52 ms 125628 KB Output is correct
3 Correct 50 ms 125552 KB Output is correct
4 Correct 53 ms 125524 KB Output is correct
5 Correct 55 ms 125516 KB Output is correct
6 Correct 51 ms 125592 KB Output is correct
7 Correct 55 ms 125624 KB Output is correct
8 Correct 56 ms 125628 KB Output is correct
9 Correct 54 ms 125632 KB Output is correct
10 Correct 54 ms 125628 KB Output is correct
11 Correct 52 ms 125520 KB Output is correct
12 Correct 68 ms 125596 KB Output is correct
13 Correct 59 ms 125516 KB Output is correct
14 Correct 56 ms 125548 KB Output is correct
15 Correct 55 ms 125532 KB Output is correct
16 Correct 54 ms 125516 KB Output is correct
17 Correct 60 ms 125884 KB Output is correct
18 Correct 57 ms 125908 KB Output is correct
19 Correct 75 ms 125960 KB Output is correct
20 Correct 61 ms 125860 KB Output is correct
21 Correct 64 ms 125900 KB Output is correct
22 Correct 56 ms 125888 KB Output is correct
23 Correct 73 ms 125812 KB Output is correct
24 Correct 392 ms 147888 KB Output is correct
25 Correct 294 ms 145616 KB Output is correct
26 Correct 390 ms 147876 KB Output is correct
27 Correct 312 ms 145548 KB Output is correct
28 Correct 467 ms 147368 KB Output is correct
29 Correct 434 ms 147816 KB Output is correct
30 Correct 639 ms 147932 KB Output is correct
31 Correct 127 ms 134776 KB Output is correct
32 Correct 294 ms 139672 KB Output is correct
33 Correct 427 ms 145416 KB Output is correct
34 Correct 621 ms 147596 KB Output is correct
35 Correct 634 ms 147416 KB Output is correct
36 Correct 795 ms 147372 KB Output is correct
37 Correct 464 ms 147260 KB Output is correct
38 Correct 344 ms 145060 KB Output is correct
39 Correct 424 ms 147452 KB Output is correct
40 Correct 445 ms 147412 KB Output is correct
41 Correct 55 ms 125608 KB Output is correct
42 Correct 674 ms 147172 KB Output is correct
43 Correct 441 ms 144616 KB Output is correct
44 Correct 672 ms 147012 KB Output is correct
45 Correct 651 ms 148252 KB Output is correct
46 Correct 2218 ms 230232 KB Output is correct
47 Correct 1553 ms 217044 KB Output is correct
48 Correct 2031 ms 228256 KB Output is correct
49 Correct 2036 ms 228592 KB Output is correct
50 Correct 4081 ms 228784 KB Output is correct
51 Correct 2243 ms 213624 KB Output is correct
52 Correct 3231 ms 223924 KB Output is correct
53 Correct 3720 ms 227716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 404 ms 146608 KB Output is correct
2 Correct 452 ms 146780 KB Output is correct
3 Correct 449 ms 147628 KB Output is correct
4 Correct 422 ms 145724 KB Output is correct
5 Correct 51 ms 125520 KB Output is correct
6 Correct 476 ms 146984 KB Output is correct
7 Correct 149 ms 135340 KB Output is correct
8 Correct 333 ms 140100 KB Output is correct
9 Correct 462 ms 147780 KB Output is correct
10 Correct 437 ms 147968 KB Output is correct
11 Correct 416 ms 148156 KB Output is correct
12 Correct 55 ms 125568 KB Output is correct
13 Correct 52 ms 125628 KB Output is correct
14 Correct 50 ms 125552 KB Output is correct
15 Correct 53 ms 125524 KB Output is correct
16 Correct 55 ms 125516 KB Output is correct
17 Correct 51 ms 125592 KB Output is correct
18 Correct 55 ms 125624 KB Output is correct
19 Correct 56 ms 125628 KB Output is correct
20 Correct 54 ms 125632 KB Output is correct
21 Correct 54 ms 125628 KB Output is correct
22 Correct 52 ms 125520 KB Output is correct
23 Correct 68 ms 125596 KB Output is correct
24 Correct 59 ms 125516 KB Output is correct
25 Correct 56 ms 125548 KB Output is correct
26 Correct 55 ms 125532 KB Output is correct
27 Correct 54 ms 125516 KB Output is correct
28 Correct 60 ms 125884 KB Output is correct
29 Correct 57 ms 125908 KB Output is correct
30 Correct 75 ms 125960 KB Output is correct
31 Correct 61 ms 125860 KB Output is correct
32 Correct 64 ms 125900 KB Output is correct
33 Correct 56 ms 125888 KB Output is correct
34 Correct 73 ms 125812 KB Output is correct
35 Correct 392 ms 147888 KB Output is correct
36 Correct 294 ms 145616 KB Output is correct
37 Correct 390 ms 147876 KB Output is correct
38 Correct 312 ms 145548 KB Output is correct
39 Correct 467 ms 147368 KB Output is correct
40 Correct 434 ms 147816 KB Output is correct
41 Correct 639 ms 147932 KB Output is correct
42 Correct 127 ms 134776 KB Output is correct
43 Correct 294 ms 139672 KB Output is correct
44 Correct 427 ms 145416 KB Output is correct
45 Correct 621 ms 147596 KB Output is correct
46 Correct 634 ms 147416 KB Output is correct
47 Correct 795 ms 147372 KB Output is correct
48 Correct 464 ms 147260 KB Output is correct
49 Correct 344 ms 145060 KB Output is correct
50 Correct 424 ms 147452 KB Output is correct
51 Correct 445 ms 147412 KB Output is correct
52 Correct 55 ms 125608 KB Output is correct
53 Correct 674 ms 147172 KB Output is correct
54 Correct 441 ms 144616 KB Output is correct
55 Correct 672 ms 147012 KB Output is correct
56 Correct 651 ms 148252 KB Output is correct
57 Correct 407 ms 146512 KB Output is correct
58 Correct 445 ms 148980 KB Output is correct
59 Correct 550 ms 151212 KB Output is correct
60 Correct 466 ms 146544 KB Output is correct
61 Correct 601 ms 146620 KB Output is correct
62 Correct 51 ms 125564 KB Output is correct
63 Correct 738 ms 147884 KB Output is correct
64 Correct 479 ms 144440 KB Output is correct
65 Correct 650 ms 147368 KB Output is correct
66 Correct 696 ms 148012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 404 ms 146608 KB Output is correct
2 Correct 452 ms 146780 KB Output is correct
3 Correct 449 ms 147628 KB Output is correct
4 Correct 422 ms 145724 KB Output is correct
5 Correct 51 ms 125520 KB Output is correct
6 Correct 476 ms 146984 KB Output is correct
7 Correct 149 ms 135340 KB Output is correct
8 Correct 333 ms 140100 KB Output is correct
9 Correct 462 ms 147780 KB Output is correct
10 Correct 437 ms 147968 KB Output is correct
11 Correct 416 ms 148156 KB Output is correct
12 Correct 55 ms 125568 KB Output is correct
13 Correct 52 ms 125628 KB Output is correct
14 Correct 50 ms 125552 KB Output is correct
15 Correct 53 ms 125524 KB Output is correct
16 Correct 55 ms 125516 KB Output is correct
17 Correct 51 ms 125592 KB Output is correct
18 Correct 55 ms 125624 KB Output is correct
19 Correct 56 ms 125628 KB Output is correct
20 Correct 54 ms 125632 KB Output is correct
21 Correct 54 ms 125628 KB Output is correct
22 Correct 52 ms 125520 KB Output is correct
23 Correct 68 ms 125596 KB Output is correct
24 Correct 59 ms 125516 KB Output is correct
25 Correct 56 ms 125548 KB Output is correct
26 Correct 55 ms 125532 KB Output is correct
27 Correct 54 ms 125516 KB Output is correct
28 Correct 60 ms 125884 KB Output is correct
29 Correct 57 ms 125908 KB Output is correct
30 Correct 75 ms 125960 KB Output is correct
31 Correct 61 ms 125860 KB Output is correct
32 Correct 64 ms 125900 KB Output is correct
33 Correct 56 ms 125888 KB Output is correct
34 Correct 73 ms 125812 KB Output is correct
35 Correct 392 ms 147888 KB Output is correct
36 Correct 294 ms 145616 KB Output is correct
37 Correct 390 ms 147876 KB Output is correct
38 Correct 312 ms 145548 KB Output is correct
39 Correct 467 ms 147368 KB Output is correct
40 Correct 434 ms 147816 KB Output is correct
41 Correct 639 ms 147932 KB Output is correct
42 Correct 127 ms 134776 KB Output is correct
43 Correct 294 ms 139672 KB Output is correct
44 Correct 427 ms 145416 KB Output is correct
45 Correct 621 ms 147596 KB Output is correct
46 Correct 634 ms 147416 KB Output is correct
47 Correct 795 ms 147372 KB Output is correct
48 Correct 464 ms 147260 KB Output is correct
49 Correct 344 ms 145060 KB Output is correct
50 Correct 424 ms 147452 KB Output is correct
51 Correct 445 ms 147412 KB Output is correct
52 Correct 55 ms 125608 KB Output is correct
53 Correct 674 ms 147172 KB Output is correct
54 Correct 441 ms 144616 KB Output is correct
55 Correct 672 ms 147012 KB Output is correct
56 Correct 651 ms 148252 KB Output is correct
57 Correct 2218 ms 230232 KB Output is correct
58 Correct 1553 ms 217044 KB Output is correct
59 Correct 2031 ms 228256 KB Output is correct
60 Correct 2036 ms 228592 KB Output is correct
61 Correct 4081 ms 228784 KB Output is correct
62 Correct 2243 ms 213624 KB Output is correct
63 Correct 3231 ms 223924 KB Output is correct
64 Correct 3720 ms 227716 KB Output is correct
65 Correct 407 ms 146512 KB Output is correct
66 Correct 445 ms 148980 KB Output is correct
67 Correct 550 ms 151212 KB Output is correct
68 Correct 466 ms 146544 KB Output is correct
69 Correct 601 ms 146620 KB Output is correct
70 Correct 51 ms 125564 KB Output is correct
71 Correct 738 ms 147884 KB Output is correct
72 Correct 479 ms 144440 KB Output is correct
73 Correct 650 ms 147368 KB Output is correct
74 Correct 696 ms 148012 KB Output is correct
75 Correct 1955 ms 236524 KB Output is correct
76 Correct 2041 ms 248332 KB Output is correct
77 Correct 2576 ms 258388 KB Output is correct
78 Correct 2080 ms 234868 KB Output is correct
79 Correct 4280 ms 242920 KB Output is correct
80 Correct 2463 ms 220708 KB Output is correct
81 Correct 3392 ms 230616 KB Output is correct
82 Correct 4013 ms 234952 KB Output is correct
83 Correct 4088 ms 234284 KB Output is correct