Submission #974545

# Submission time Handle Problem Language Result Execution time Memory
974545 2024-05-03T12:46:25 Z DAleksa Two Dishes (JOI19_dishes) C++17
100 / 100
4116 ms 332408 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long

struct node {
    long long mx;
    long long add;
    long long set;
    bool both;
    node() {
        mx = 0;
        add = 0;
        set = 0;
        both = false;
    }
};

struct pt {
    int y;
    long long val;
};

const int N = 1e6 + 10;
const long long inf = 1e18;
int n, m;
long long a[N], p[N], b[N], q[N];
long long s[N], t[N];
int x[N], y[N];
vector<pt> all[N];
node st[4 * N];

void propagate(int index) {
    if(2 * index + 1 < 4 * N) {
        if(st[index].both) {
            long long val = st[index].set + st[index].add;
            st[2 * index].mx = val;
            st[2 * index].set = val;
            st[2 * index].add = 0;
            st[2 * index].both = true;
            st[2 * index + 1].mx = val;
            st[2 * index + 1].set = val;
            st[2 * index + 1].add = 0;
            st[2 * index + 1].both = true;
            st[index].add = 0;
            st[index].set = 0;
            st[index].both = false;
        } else {
            st[2 * index].mx += st[index].add;
            st[2 * index].add += st[index].add;
            st[2 * index + 1].mx += st[index].add;
            st[2 * index + 1].add += st[index].add;
            st[index].add = 0;
        }
    }
}

void update(int index, int l, int r, int L, int R, long long val) {
    if(l > r || r < L || R < l) return;
    if(L <= l && r <= R) {
        st[index].mx += val;
        st[index].add += val;
        return;
    }
    propagate(index);
    int mid = (l + r) / 2;
    update(2 * index, l, mid, L, R, val);
    update(2 * index + 1, mid + 1, r, L, R, val);
    st[index].mx = max(st[2 * index].mx, st[2 * index + 1].mx);
}

void modify(int index, int l, int r, int L, int R, long long val) {
    if(l > r || r < L || R < l) return;
    if(L <= l && r <= R) {
        st[index].mx = val;
        st[index].set = val;
        st[index].both = true;
        st[index].add = 0;
        return;
    }
    propagate(index);
    int mid = (l + r) / 2;
    modify(2 * index, l, mid, L, R, val);
    modify(2 * index + 1, mid + 1, r, L, R, val);
    st[index].mx = max(st[2 * index].mx, st[2 * index + 1].mx);
}

long long get(int index, int l, int r, int x) {
    if(l > r || r < x || x < l) return -inf;
    if(l == r) return st[index].mx;
    propagate(index);
    int mid = (l + r) / 2;
    return max(get(2 * index, l, mid, x), get(2 * index + 1, mid + 1, r, x));
}

int walk(int index, int l, int r, int x, long long val) {
    if(l > r || r < x) return r;
    if(l == r) {
        if(st[index].mx >= val) return l - 1;
        return l;
    }
    if(st[index].mx < val) return r;
    propagate(index);
    int mid = (l + r) / 2;
    int L = walk(2 * index, l, mid, x, val);
    if(L != mid) return L;
    return walk(2 * index + 1, mid + 1, r, x, val);
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++) cin >> a[i] >> s[i] >> p[i];
    for(int i = 1; i <= m; i++) cin >> b[i] >> t[i] >> q[i];
    a[0] = b[0] = 0;
    for(int i = 2; i <= n; i++) a[i] += a[i - 1];
    for(int i = 2; i <= m; i++) b[i] += b[i - 1];
    for(int i = 1; i <= n; i++) {
        if(a[i] > s[i]) {
            x[i] = -1;
            continue;
        }
        int l = 0, r = m;
        int ans = l;
        while(l <= r) {
            int mid = (l + r) / 2;
            if(a[i] + b[mid] <= s[i]) {
                ans = mid;
                l = mid + 1;
            } else r = mid - 1;
        }
        x[i] = ans;
    }
    for(int i = 1; i <= m; i++) {
        if(b[i] > t[i]) {
            y[i] = -1;
            continue;
        }
        int l = 0, r = n;
        int ans = l;
        while(l <= r) {
            int mid = (l + r) / 2;
            if(a[mid] + b[i] <= t[i]) {
                ans = mid;
                l = mid + 1;
            } else r = mid - 1;
        }
        y[i] = ans;
    }
    long long add = 0;
    for(int i = 1; i <= n; i++) {
        if(x[i] == -1) continue;
        add += p[i];
        if(x[i] < m) all[x[i] + 1].push_back({i - 1, -p[i]});
    }
    for(int i = 1; i <= m; i++) {
        if(y[i] == -1) continue;
        all[i].push_back({y[i], q[i]});
    }
    for(int i = 1; i <= m; i++) {
        sort(all[i].begin(), all[i].end(), [&](pt p1, pt p2) {
            return p1.y > p2.y;
        });
    }
    // modify(1, 0, n, 1, n, 39);
    // for(int i = 0; i <= n; i++) cout << get(1, 0, n, i) << " ";
    // return 0;
    // cout << "POINTS:\n";
    // for(int i = 0; i <= m; i++) {
    //     for(pt p : all[i]) cout << i << " " << p.y << " " << p.val << "\n";
    // }
    for(int i = 0; i <= m; i++) {
        // cout << "#" << i << ":\nPOINTS:\n";
        for(pt p : all[i]) {
            int y = p.y;
            long long val = p.val;
            // cout << y << " " << val << "\n";
            update(1, 0, n, 0, y, val);
            // cout << "TREE:\n";
            // for(int j = 0; j <= n; j++) cout << get(1, 0, n, j) << " ";
            // cout << "\n";
        }
        for(pt p : all[i]) {
            int y = p.y;
            long long val = p.val;
            long long mx = get(1, 0, n, y);
            int nxt = walk(1, 0, n, y + 1, mx);
            modify(1, 0, n, y, nxt, mx);
        }
        // cout << "\n";
    }
    // for(int i = 1; i <= m; i++) {
    //     for(pt p : all[i]) cout << i << " " << p.y << " " << p.val << "\n";
    // }
    // for(int i = 1; i <= n; i++) cout << x[i] << " ";
    // cout << "\n";
    // for(int i = 1; i <= m; i++) cout << y[i] << " ";
    // cout << "\n";
    // cout << add << "\n";
    cout << get(1, 0, n, n) + add;
    return 0;
}

Compilation message

dishes.cpp: In function 'int main()':
dishes.cpp:187:23: warning: unused variable 'val' [-Wunused-variable]
  187 |             long long val = p.val;
      |                       ^~~
# Verdict Execution time Memory Grader output
1 Correct 466 ms 184368 KB Output is correct
2 Correct 368 ms 184740 KB Output is correct
3 Correct 197 ms 181072 KB Output is correct
4 Correct 297 ms 181184 KB Output is correct
5 Correct 28 ms 164696 KB Output is correct
6 Correct 347 ms 183884 KB Output is correct
7 Correct 92 ms 176468 KB Output is correct
8 Correct 80 ms 168856 KB Output is correct
9 Correct 213 ms 181588 KB Output is correct
10 Correct 377 ms 184400 KB Output is correct
11 Correct 165 ms 181072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 164684 KB Output is correct
2 Correct 26 ms 164692 KB Output is correct
3 Correct 29 ms 164688 KB Output is correct
4 Correct 26 ms 164700 KB Output is correct
5 Correct 26 ms 164640 KB Output is correct
6 Correct 26 ms 164684 KB Output is correct
7 Correct 28 ms 164692 KB Output is correct
8 Correct 26 ms 164612 KB Output is correct
9 Correct 28 ms 164568 KB Output is correct
10 Correct 26 ms 164700 KB Output is correct
11 Correct 26 ms 164700 KB Output is correct
12 Correct 26 ms 164676 KB Output is correct
13 Correct 26 ms 165016 KB Output is correct
14 Correct 26 ms 164692 KB Output is correct
15 Correct 26 ms 164576 KB Output is correct
16 Correct 26 ms 164564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 164684 KB Output is correct
2 Correct 26 ms 164692 KB Output is correct
3 Correct 29 ms 164688 KB Output is correct
4 Correct 26 ms 164700 KB Output is correct
5 Correct 26 ms 164640 KB Output is correct
6 Correct 26 ms 164684 KB Output is correct
7 Correct 28 ms 164692 KB Output is correct
8 Correct 26 ms 164612 KB Output is correct
9 Correct 28 ms 164568 KB Output is correct
10 Correct 26 ms 164700 KB Output is correct
11 Correct 26 ms 164700 KB Output is correct
12 Correct 26 ms 164676 KB Output is correct
13 Correct 26 ms 165016 KB Output is correct
14 Correct 26 ms 164692 KB Output is correct
15 Correct 26 ms 164576 KB Output is correct
16 Correct 26 ms 164564 KB Output is correct
17 Correct 28 ms 164700 KB Output is correct
18 Correct 30 ms 164832 KB Output is correct
19 Correct 31 ms 164696 KB Output is correct
20 Correct 29 ms 164692 KB Output is correct
21 Correct 29 ms 164700 KB Output is correct
22 Correct 30 ms 164700 KB Output is correct
23 Correct 29 ms 164684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 164684 KB Output is correct
2 Correct 26 ms 164692 KB Output is correct
3 Correct 29 ms 164688 KB Output is correct
4 Correct 26 ms 164700 KB Output is correct
5 Correct 26 ms 164640 KB Output is correct
6 Correct 26 ms 164684 KB Output is correct
7 Correct 28 ms 164692 KB Output is correct
8 Correct 26 ms 164612 KB Output is correct
9 Correct 28 ms 164568 KB Output is correct
10 Correct 26 ms 164700 KB Output is correct
11 Correct 26 ms 164700 KB Output is correct
12 Correct 26 ms 164676 KB Output is correct
13 Correct 26 ms 165016 KB Output is correct
14 Correct 26 ms 164692 KB Output is correct
15 Correct 26 ms 164576 KB Output is correct
16 Correct 26 ms 164564 KB Output is correct
17 Correct 28 ms 164700 KB Output is correct
18 Correct 30 ms 164832 KB Output is correct
19 Correct 31 ms 164696 KB Output is correct
20 Correct 29 ms 164692 KB Output is correct
21 Correct 29 ms 164700 KB Output is correct
22 Correct 30 ms 164700 KB Output is correct
23 Correct 29 ms 164684 KB Output is correct
24 Correct 275 ms 181192 KB Output is correct
25 Correct 213 ms 181072 KB Output is correct
26 Correct 308 ms 183992 KB Output is correct
27 Correct 258 ms 181412 KB Output is correct
28 Correct 292 ms 181464 KB Output is correct
29 Correct 185 ms 181296 KB Output is correct
30 Correct 643 ms 184904 KB Output is correct
31 Correct 85 ms 176980 KB Output is correct
32 Correct 133 ms 171036 KB Output is correct
33 Correct 374 ms 180428 KB Output is correct
34 Correct 494 ms 184408 KB Output is correct
35 Correct 649 ms 185172 KB Output is correct
36 Correct 586 ms 184912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 164684 KB Output is correct
2 Correct 26 ms 164692 KB Output is correct
3 Correct 29 ms 164688 KB Output is correct
4 Correct 26 ms 164700 KB Output is correct
5 Correct 26 ms 164640 KB Output is correct
6 Correct 26 ms 164684 KB Output is correct
7 Correct 28 ms 164692 KB Output is correct
8 Correct 26 ms 164612 KB Output is correct
9 Correct 28 ms 164568 KB Output is correct
10 Correct 26 ms 164700 KB Output is correct
11 Correct 26 ms 164700 KB Output is correct
12 Correct 26 ms 164676 KB Output is correct
13 Correct 26 ms 165016 KB Output is correct
14 Correct 26 ms 164692 KB Output is correct
15 Correct 26 ms 164576 KB Output is correct
16 Correct 26 ms 164564 KB Output is correct
17 Correct 28 ms 164700 KB Output is correct
18 Correct 30 ms 164832 KB Output is correct
19 Correct 31 ms 164696 KB Output is correct
20 Correct 29 ms 164692 KB Output is correct
21 Correct 29 ms 164700 KB Output is correct
22 Correct 30 ms 164700 KB Output is correct
23 Correct 29 ms 164684 KB Output is correct
24 Correct 275 ms 181192 KB Output is correct
25 Correct 213 ms 181072 KB Output is correct
26 Correct 308 ms 183992 KB Output is correct
27 Correct 258 ms 181412 KB Output is correct
28 Correct 292 ms 181464 KB Output is correct
29 Correct 185 ms 181296 KB Output is correct
30 Correct 643 ms 184904 KB Output is correct
31 Correct 85 ms 176980 KB Output is correct
32 Correct 133 ms 171036 KB Output is correct
33 Correct 374 ms 180428 KB Output is correct
34 Correct 494 ms 184408 KB Output is correct
35 Correct 649 ms 185172 KB Output is correct
36 Correct 586 ms 184912 KB Output is correct
37 Correct 331 ms 183988 KB Output is correct
38 Correct 264 ms 181072 KB Output is correct
39 Correct 451 ms 184512 KB Output is correct
40 Correct 380 ms 184400 KB Output is correct
41 Correct 26 ms 164696 KB Output is correct
42 Correct 631 ms 184908 KB Output is correct
43 Correct 397 ms 180592 KB Output is correct
44 Correct 486 ms 184248 KB Output is correct
45 Correct 600 ms 184984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 164684 KB Output is correct
2 Correct 26 ms 164692 KB Output is correct
3 Correct 29 ms 164688 KB Output is correct
4 Correct 26 ms 164700 KB Output is correct
5 Correct 26 ms 164640 KB Output is correct
6 Correct 26 ms 164684 KB Output is correct
7 Correct 28 ms 164692 KB Output is correct
8 Correct 26 ms 164612 KB Output is correct
9 Correct 28 ms 164568 KB Output is correct
10 Correct 26 ms 164700 KB Output is correct
11 Correct 26 ms 164700 KB Output is correct
12 Correct 26 ms 164676 KB Output is correct
13 Correct 26 ms 165016 KB Output is correct
14 Correct 26 ms 164692 KB Output is correct
15 Correct 26 ms 164576 KB Output is correct
16 Correct 26 ms 164564 KB Output is correct
17 Correct 28 ms 164700 KB Output is correct
18 Correct 30 ms 164832 KB Output is correct
19 Correct 31 ms 164696 KB Output is correct
20 Correct 29 ms 164692 KB Output is correct
21 Correct 29 ms 164700 KB Output is correct
22 Correct 30 ms 164700 KB Output is correct
23 Correct 29 ms 164684 KB Output is correct
24 Correct 275 ms 181192 KB Output is correct
25 Correct 213 ms 181072 KB Output is correct
26 Correct 308 ms 183992 KB Output is correct
27 Correct 258 ms 181412 KB Output is correct
28 Correct 292 ms 181464 KB Output is correct
29 Correct 185 ms 181296 KB Output is correct
30 Correct 643 ms 184904 KB Output is correct
31 Correct 85 ms 176980 KB Output is correct
32 Correct 133 ms 171036 KB Output is correct
33 Correct 374 ms 180428 KB Output is correct
34 Correct 494 ms 184408 KB Output is correct
35 Correct 649 ms 185172 KB Output is correct
36 Correct 586 ms 184912 KB Output is correct
37 Correct 331 ms 183988 KB Output is correct
38 Correct 264 ms 181072 KB Output is correct
39 Correct 451 ms 184512 KB Output is correct
40 Correct 380 ms 184400 KB Output is correct
41 Correct 26 ms 164696 KB Output is correct
42 Correct 631 ms 184908 KB Output is correct
43 Correct 397 ms 180592 KB Output is correct
44 Correct 486 ms 184248 KB Output is correct
45 Correct 600 ms 184984 KB Output is correct
46 Correct 1531 ms 256588 KB Output is correct
47 Correct 1271 ms 242976 KB Output is correct
48 Correct 2340 ms 258832 KB Output is correct
49 Correct 2013 ms 258924 KB Output is correct
50 Correct 4065 ms 261800 KB Output is correct
51 Correct 2243 ms 238000 KB Output is correct
52 Correct 2779 ms 253448 KB Output is correct
53 Correct 3798 ms 260436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 466 ms 184368 KB Output is correct
2 Correct 368 ms 184740 KB Output is correct
3 Correct 197 ms 181072 KB Output is correct
4 Correct 297 ms 181184 KB Output is correct
5 Correct 28 ms 164696 KB Output is correct
6 Correct 347 ms 183884 KB Output is correct
7 Correct 92 ms 176468 KB Output is correct
8 Correct 80 ms 168856 KB Output is correct
9 Correct 213 ms 181588 KB Output is correct
10 Correct 377 ms 184400 KB Output is correct
11 Correct 165 ms 181072 KB Output is correct
12 Correct 26 ms 164684 KB Output is correct
13 Correct 26 ms 164692 KB Output is correct
14 Correct 29 ms 164688 KB Output is correct
15 Correct 26 ms 164700 KB Output is correct
16 Correct 26 ms 164640 KB Output is correct
17 Correct 26 ms 164684 KB Output is correct
18 Correct 28 ms 164692 KB Output is correct
19 Correct 26 ms 164612 KB Output is correct
20 Correct 28 ms 164568 KB Output is correct
21 Correct 26 ms 164700 KB Output is correct
22 Correct 26 ms 164700 KB Output is correct
23 Correct 26 ms 164676 KB Output is correct
24 Correct 26 ms 165016 KB Output is correct
25 Correct 26 ms 164692 KB Output is correct
26 Correct 26 ms 164576 KB Output is correct
27 Correct 26 ms 164564 KB Output is correct
28 Correct 28 ms 164700 KB Output is correct
29 Correct 30 ms 164832 KB Output is correct
30 Correct 31 ms 164696 KB Output is correct
31 Correct 29 ms 164692 KB Output is correct
32 Correct 29 ms 164700 KB Output is correct
33 Correct 30 ms 164700 KB Output is correct
34 Correct 29 ms 164684 KB Output is correct
35 Correct 275 ms 181192 KB Output is correct
36 Correct 213 ms 181072 KB Output is correct
37 Correct 308 ms 183992 KB Output is correct
38 Correct 258 ms 181412 KB Output is correct
39 Correct 292 ms 181464 KB Output is correct
40 Correct 185 ms 181296 KB Output is correct
41 Correct 643 ms 184904 KB Output is correct
42 Correct 85 ms 176980 KB Output is correct
43 Correct 133 ms 171036 KB Output is correct
44 Correct 374 ms 180428 KB Output is correct
45 Correct 494 ms 184408 KB Output is correct
46 Correct 649 ms 185172 KB Output is correct
47 Correct 586 ms 184912 KB Output is correct
48 Correct 331 ms 183988 KB Output is correct
49 Correct 264 ms 181072 KB Output is correct
50 Correct 451 ms 184512 KB Output is correct
51 Correct 380 ms 184400 KB Output is correct
52 Correct 26 ms 164696 KB Output is correct
53 Correct 631 ms 184908 KB Output is correct
54 Correct 397 ms 180592 KB Output is correct
55 Correct 486 ms 184248 KB Output is correct
56 Correct 600 ms 184984 KB Output is correct
57 Correct 311 ms 183992 KB Output is correct
58 Correct 264 ms 181296 KB Output is correct
59 Correct 385 ms 184512 KB Output is correct
60 Correct 488 ms 196632 KB Output is correct
61 Correct 610 ms 195864 KB Output is correct
62 Correct 26 ms 164688 KB Output is correct
63 Correct 647 ms 198736 KB Output is correct
64 Correct 389 ms 193872 KB Output is correct
65 Correct 533 ms 198448 KB Output is correct
66 Correct 585 ms 192312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 466 ms 184368 KB Output is correct
2 Correct 368 ms 184740 KB Output is correct
3 Correct 197 ms 181072 KB Output is correct
4 Correct 297 ms 181184 KB Output is correct
5 Correct 28 ms 164696 KB Output is correct
6 Correct 347 ms 183884 KB Output is correct
7 Correct 92 ms 176468 KB Output is correct
8 Correct 80 ms 168856 KB Output is correct
9 Correct 213 ms 181588 KB Output is correct
10 Correct 377 ms 184400 KB Output is correct
11 Correct 165 ms 181072 KB Output is correct
12 Correct 26 ms 164684 KB Output is correct
13 Correct 26 ms 164692 KB Output is correct
14 Correct 29 ms 164688 KB Output is correct
15 Correct 26 ms 164700 KB Output is correct
16 Correct 26 ms 164640 KB Output is correct
17 Correct 26 ms 164684 KB Output is correct
18 Correct 28 ms 164692 KB Output is correct
19 Correct 26 ms 164612 KB Output is correct
20 Correct 28 ms 164568 KB Output is correct
21 Correct 26 ms 164700 KB Output is correct
22 Correct 26 ms 164700 KB Output is correct
23 Correct 26 ms 164676 KB Output is correct
24 Correct 26 ms 165016 KB Output is correct
25 Correct 26 ms 164692 KB Output is correct
26 Correct 26 ms 164576 KB Output is correct
27 Correct 26 ms 164564 KB Output is correct
28 Correct 28 ms 164700 KB Output is correct
29 Correct 30 ms 164832 KB Output is correct
30 Correct 31 ms 164696 KB Output is correct
31 Correct 29 ms 164692 KB Output is correct
32 Correct 29 ms 164700 KB Output is correct
33 Correct 30 ms 164700 KB Output is correct
34 Correct 29 ms 164684 KB Output is correct
35 Correct 275 ms 181192 KB Output is correct
36 Correct 213 ms 181072 KB Output is correct
37 Correct 308 ms 183992 KB Output is correct
38 Correct 258 ms 181412 KB Output is correct
39 Correct 292 ms 181464 KB Output is correct
40 Correct 185 ms 181296 KB Output is correct
41 Correct 643 ms 184904 KB Output is correct
42 Correct 85 ms 176980 KB Output is correct
43 Correct 133 ms 171036 KB Output is correct
44 Correct 374 ms 180428 KB Output is correct
45 Correct 494 ms 184408 KB Output is correct
46 Correct 649 ms 185172 KB Output is correct
47 Correct 586 ms 184912 KB Output is correct
48 Correct 331 ms 183988 KB Output is correct
49 Correct 264 ms 181072 KB Output is correct
50 Correct 451 ms 184512 KB Output is correct
51 Correct 380 ms 184400 KB Output is correct
52 Correct 26 ms 164696 KB Output is correct
53 Correct 631 ms 184908 KB Output is correct
54 Correct 397 ms 180592 KB Output is correct
55 Correct 486 ms 184248 KB Output is correct
56 Correct 600 ms 184984 KB Output is correct
57 Correct 1531 ms 256588 KB Output is correct
58 Correct 1271 ms 242976 KB Output is correct
59 Correct 2340 ms 258832 KB Output is correct
60 Correct 2013 ms 258924 KB Output is correct
61 Correct 4065 ms 261800 KB Output is correct
62 Correct 2243 ms 238000 KB Output is correct
63 Correct 2779 ms 253448 KB Output is correct
64 Correct 3798 ms 260436 KB Output is correct
65 Correct 311 ms 183992 KB Output is correct
66 Correct 264 ms 181296 KB Output is correct
67 Correct 385 ms 184512 KB Output is correct
68 Correct 488 ms 196632 KB Output is correct
69 Correct 610 ms 195864 KB Output is correct
70 Correct 26 ms 164688 KB Output is correct
71 Correct 647 ms 198736 KB Output is correct
72 Correct 389 ms 193872 KB Output is correct
73 Correct 533 ms 198448 KB Output is correct
74 Correct 585 ms 192312 KB Output is correct
75 Correct 1591 ms 328348 KB Output is correct
76 Correct 1442 ms 314916 KB Output is correct
77 Correct 2031 ms 317696 KB Output is correct
78 Correct 2339 ms 317696 KB Output is correct
79 Correct 4116 ms 332408 KB Output is correct
80 Correct 2253 ms 305100 KB Output is correct
81 Correct 3144 ms 325720 KB Output is correct
82 Correct 3913 ms 300004 KB Output is correct
83 Correct 3767 ms 318288 KB Output is correct