Submission #875935

# Submission time Handle Problem Language Result Execution time Memory
875935 2023-11-20T19:15:53 Z vjudge1 Two Dishes (JOI19_dishes) C++17
100 / 100
6213 ms 322808 KB
// https://oj.uz/problem/view/JOI19_dishes

#include <bits/stdc++.h>
using namespace std;

const int64_t inf = 1e18;

class segtree_t {
       public:
        segtree_t *left, *right;
        int l, r, m;
        int64_t val, lazy_add, lazy_max;

        segtree_t(int l, int r) : l(l), r(r), m(l + r >> 1), val(0), lazy_add(0), lazy_max(-inf) {
                if (l == r) return;
                left = new segtree_t(l, m);
                right = new segtree_t(m + 1, r);
        }

        void update_add(int s, int t, int64_t x) {
                if (l > t || r < s) return;
                if (s <= l && r <= t) {
                        val += x;
                        lazy_add += x;
                        lazy_max += x;
                        return;
                }
                Down();
                left->update_add(s, t, x);
                right->update_add(s, t, x);
                Up();
        }

        void update_max(int s, int t, int64_t x) {
                if (l > t || r < s) return;
                if (s <= l && r <= t) {
                        val = max(val, x);
                        lazy_max = max(lazy_max, x);
                        return;
                }
                Down();
                left->update_max(s, t, x);
                right->update_max(s, t, x);
                Up();
        }

        int64_t get(int s, int t) {
                if (l > t || r < s) return -inf;
                if (s <= l && r <= t) return val;
                Down();
                return max(left->get(s, t), right->get(s, t));
        }

        void Down() {
                left->lazy_add += lazy_add;
                right->lazy_add += lazy_add;
                left->lazy_max += lazy_add;
                right->lazy_max += lazy_add;
                left->val += lazy_add;
                right->val += lazy_add;
                left->val = max(left->val, lazy_max);
                right->val = max(right->val, lazy_max);
                left->lazy_max = max(left->lazy_max, lazy_max);
                right->lazy_max = max(right->lazy_max, lazy_max);
                lazy_add = 0;
                lazy_max = -inf;
        }

        void Up() {
                val = max(left->val, right->val);
        }
};

int32_t main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);

        int n, m;
        cin >> n >> m;
        vector<int64_t> a(n + 1), s(n), p(n);
        vector<int64_t> b(m + 1), t(m), q(m);
        for (int i = 0; i < n; i++) cin >> a[i + 1] >> s[i] >> p[i];
        for (int i = 0; i < m; i++) cin >> b[i + 1] >> t[i] >> q[i];
        for (int i = 0; i < n; i++) a[i + 1] += a[i];
        for (int i = 0; i < m; i++) b[i + 1] += b[i];
        for (int i = 0; i < n; i++) s[i] -= a[i + 1];
        for (int i = 0; i < m; i++) t[i] -= b[i + 1];
        vector<int> c(n), d(m);
        for (int i = 0; i < n; i++) c[i] = upper_bound(b.begin(), b.end(), s[i]) - b.begin() - 1;
        for (int i = 0; i < m; i++) d[i] = upper_bound(a.begin(), a.end(), t[i]) - a.begin() - 1;

        segtree_t *dp = new segtree_t(0, m);
        vector<vector<pair<int, int>>> upd(n);
        for (int i = 0; i < m; i++) {
                if (d[i] == 0) {
                        dp->update_add(i + 1, m, q[i]);
                        upd[d[i]].emplace_back(i + 1, 0);
                }
                if (d[i] > 0) {
                        upd[d[i] - 1].emplace_back(i + 1, q[i]);
                        if (d[i] < n) upd[d[i]].emplace_back(i + 1, 0);
                }
        }
        int last = 0;
        for (int i = 0; i < n; i++) {
                sort(upd[i].begin(), upd[i].end());
                upd[i].emplace_back(m + 1, -1);
                if (c[i] != -1) dp->update_add(0, c[i], p[i]);
                int64_t sum = 0;
                dp->update_max(c[i] + 1, upd[i][0].first - 1, dp->get(0, c[i]));
                last = 0;
                for (int j = 0; j + 1 < upd[i].size(); j++) {
                        sum += upd[i][j].second;
                        int64_t x = dp->get(last, upd[i][j].first - 1);
                        dp->update_add(upd[i][j].first, upd[i][j + 1].first - 1, sum);
                        dp->update_max(upd[i][j].first, upd[i][j + 1].first - 1, x + upd[i][j].second);
                        if (c[i] != -1 && upd[i][j].first <= c[i] && c[i] < upd[i][j + 1].first) {
                                int64_t y = dp->get(upd[i][j].first, c[i]);
                                dp->update_max(c[i], upd[i][j + 1].first - 1, y);
                        }
                        last = upd[i][j].first;
                }
        }
        cout << dp->get(last, m);
}

Compilation message

dishes.cpp: In constructor 'segtree_t::segtree_t(int, int)':
dishes.cpp:14:51: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   14 |         segtree_t(int l, int r) : l(l), r(r), m(l + r >> 1), val(0), lazy_add(0), lazy_max(-inf) {
      |                                                 ~~^~~
dishes.cpp: In function 'int32_t main()':
dishes.cpp:112:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |                 for (int j = 0; j + 1 < upd[i].size(); j++) {
      |                                 ~~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 487 ms 56940 KB Output is correct
2 Correct 478 ms 57024 KB Output is correct
3 Correct 247 ms 55664 KB Output is correct
4 Correct 378 ms 55888 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 428 ms 54876 KB Output is correct
7 Correct 165 ms 36604 KB Output is correct
8 Correct 62 ms 20508 KB Output is correct
9 Correct 253 ms 56296 KB Output is correct
10 Correct 544 ms 54356 KB Output is correct
11 Correct 221 ms 53184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 452 KB Output is correct
5 Correct 0 ms 456 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 452 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 456 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 452 KB Output is correct
5 Correct 0 ms 456 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 452 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 456 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 460 KB Output is correct
17 Correct 3 ms 860 KB Output is correct
18 Correct 2 ms 860 KB Output is correct
19 Correct 5 ms 860 KB Output is correct
20 Correct 3 ms 860 KB Output is correct
21 Correct 5 ms 980 KB Output is correct
22 Correct 5 ms 860 KB Output is correct
23 Correct 5 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 452 KB Output is correct
5 Correct 0 ms 456 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 452 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 456 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 460 KB Output is correct
17 Correct 3 ms 860 KB Output is correct
18 Correct 2 ms 860 KB Output is correct
19 Correct 5 ms 860 KB Output is correct
20 Correct 3 ms 860 KB Output is correct
21 Correct 5 ms 980 KB Output is correct
22 Correct 5 ms 860 KB Output is correct
23 Correct 5 ms 860 KB Output is correct
24 Correct 308 ms 53740 KB Output is correct
25 Correct 279 ms 54536 KB Output is correct
26 Correct 289 ms 53608 KB Output is correct
27 Correct 411 ms 56196 KB Output is correct
28 Correct 393 ms 54344 KB Output is correct
29 Correct 231 ms 54724 KB Output is correct
30 Correct 877 ms 56296 KB Output is correct
31 Correct 216 ms 35912 KB Output is correct
32 Correct 58 ms 19672 KB Output is correct
33 Correct 479 ms 53360 KB Output is correct
34 Correct 720 ms 54892 KB Output is correct
35 Correct 853 ms 53136 KB Output is correct
36 Correct 812 ms 52828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 452 KB Output is correct
5 Correct 0 ms 456 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 452 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 456 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 460 KB Output is correct
17 Correct 3 ms 860 KB Output is correct
18 Correct 2 ms 860 KB Output is correct
19 Correct 5 ms 860 KB Output is correct
20 Correct 3 ms 860 KB Output is correct
21 Correct 5 ms 980 KB Output is correct
22 Correct 5 ms 860 KB Output is correct
23 Correct 5 ms 860 KB Output is correct
24 Correct 308 ms 53740 KB Output is correct
25 Correct 279 ms 54536 KB Output is correct
26 Correct 289 ms 53608 KB Output is correct
27 Correct 411 ms 56196 KB Output is correct
28 Correct 393 ms 54344 KB Output is correct
29 Correct 231 ms 54724 KB Output is correct
30 Correct 877 ms 56296 KB Output is correct
31 Correct 216 ms 35912 KB Output is correct
32 Correct 58 ms 19672 KB Output is correct
33 Correct 479 ms 53360 KB Output is correct
34 Correct 720 ms 54892 KB Output is correct
35 Correct 853 ms 53136 KB Output is correct
36 Correct 812 ms 52828 KB Output is correct
37 Correct 339 ms 55364 KB Output is correct
38 Correct 432 ms 57696 KB Output is correct
39 Correct 531 ms 56332 KB Output is correct
40 Correct 533 ms 56404 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 886 ms 58160 KB Output is correct
43 Correct 489 ms 54760 KB Output is correct
44 Correct 756 ms 55920 KB Output is correct
45 Correct 873 ms 54716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 452 KB Output is correct
5 Correct 0 ms 456 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 452 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 456 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 460 KB Output is correct
17 Correct 3 ms 860 KB Output is correct
18 Correct 2 ms 860 KB Output is correct
19 Correct 5 ms 860 KB Output is correct
20 Correct 3 ms 860 KB Output is correct
21 Correct 5 ms 980 KB Output is correct
22 Correct 5 ms 860 KB Output is correct
23 Correct 5 ms 860 KB Output is correct
24 Correct 308 ms 53740 KB Output is correct
25 Correct 279 ms 54536 KB Output is correct
26 Correct 289 ms 53608 KB Output is correct
27 Correct 411 ms 56196 KB Output is correct
28 Correct 393 ms 54344 KB Output is correct
29 Correct 231 ms 54724 KB Output is correct
30 Correct 877 ms 56296 KB Output is correct
31 Correct 216 ms 35912 KB Output is correct
32 Correct 58 ms 19672 KB Output is correct
33 Correct 479 ms 53360 KB Output is correct
34 Correct 720 ms 54892 KB Output is correct
35 Correct 853 ms 53136 KB Output is correct
36 Correct 812 ms 52828 KB Output is correct
37 Correct 339 ms 55364 KB Output is correct
38 Correct 432 ms 57696 KB Output is correct
39 Correct 531 ms 56332 KB Output is correct
40 Correct 533 ms 56404 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 886 ms 58160 KB Output is correct
43 Correct 489 ms 54760 KB Output is correct
44 Correct 756 ms 55920 KB Output is correct
45 Correct 873 ms 54716 KB Output is correct
46 Correct 1684 ms 273948 KB Output is correct
47 Correct 2336 ms 303836 KB Output is correct
48 Correct 2939 ms 279816 KB Output is correct
49 Correct 2923 ms 279248 KB Output is correct
50 Correct 5747 ms 287336 KB Output is correct
51 Correct 3048 ms 268468 KB Output is correct
52 Correct 4217 ms 265760 KB Output is correct
53 Correct 5460 ms 270996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 487 ms 56940 KB Output is correct
2 Correct 478 ms 57024 KB Output is correct
3 Correct 247 ms 55664 KB Output is correct
4 Correct 378 ms 55888 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 428 ms 54876 KB Output is correct
7 Correct 165 ms 36604 KB Output is correct
8 Correct 62 ms 20508 KB Output is correct
9 Correct 253 ms 56296 KB Output is correct
10 Correct 544 ms 54356 KB Output is correct
11 Correct 221 ms 53184 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 452 KB Output is correct
16 Correct 0 ms 456 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 452 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 456 KB Output is correct
24 Correct 0 ms 344 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 0 ms 460 KB Output is correct
28 Correct 3 ms 860 KB Output is correct
29 Correct 2 ms 860 KB Output is correct
30 Correct 5 ms 860 KB Output is correct
31 Correct 3 ms 860 KB Output is correct
32 Correct 5 ms 980 KB Output is correct
33 Correct 5 ms 860 KB Output is correct
34 Correct 5 ms 860 KB Output is correct
35 Correct 308 ms 53740 KB Output is correct
36 Correct 279 ms 54536 KB Output is correct
37 Correct 289 ms 53608 KB Output is correct
38 Correct 411 ms 56196 KB Output is correct
39 Correct 393 ms 54344 KB Output is correct
40 Correct 231 ms 54724 KB Output is correct
41 Correct 877 ms 56296 KB Output is correct
42 Correct 216 ms 35912 KB Output is correct
43 Correct 58 ms 19672 KB Output is correct
44 Correct 479 ms 53360 KB Output is correct
45 Correct 720 ms 54892 KB Output is correct
46 Correct 853 ms 53136 KB Output is correct
47 Correct 812 ms 52828 KB Output is correct
48 Correct 339 ms 55364 KB Output is correct
49 Correct 432 ms 57696 KB Output is correct
50 Correct 531 ms 56332 KB Output is correct
51 Correct 533 ms 56404 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 886 ms 58160 KB Output is correct
54 Correct 489 ms 54760 KB Output is correct
55 Correct 756 ms 55920 KB Output is correct
56 Correct 873 ms 54716 KB Output is correct
57 Correct 327 ms 55484 KB Output is correct
58 Correct 437 ms 57936 KB Output is correct
59 Correct 538 ms 62548 KB Output is correct
60 Correct 567 ms 62808 KB Output is correct
61 Correct 929 ms 61796 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 909 ms 64652 KB Output is correct
64 Correct 485 ms 60596 KB Output is correct
65 Correct 745 ms 63524 KB Output is correct
66 Correct 848 ms 58124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 487 ms 56940 KB Output is correct
2 Correct 478 ms 57024 KB Output is correct
3 Correct 247 ms 55664 KB Output is correct
4 Correct 378 ms 55888 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 428 ms 54876 KB Output is correct
7 Correct 165 ms 36604 KB Output is correct
8 Correct 62 ms 20508 KB Output is correct
9 Correct 253 ms 56296 KB Output is correct
10 Correct 544 ms 54356 KB Output is correct
11 Correct 221 ms 53184 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 452 KB Output is correct
16 Correct 0 ms 456 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 452 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 456 KB Output is correct
24 Correct 0 ms 344 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 0 ms 460 KB Output is correct
28 Correct 3 ms 860 KB Output is correct
29 Correct 2 ms 860 KB Output is correct
30 Correct 5 ms 860 KB Output is correct
31 Correct 3 ms 860 KB Output is correct
32 Correct 5 ms 980 KB Output is correct
33 Correct 5 ms 860 KB Output is correct
34 Correct 5 ms 860 KB Output is correct
35 Correct 308 ms 53740 KB Output is correct
36 Correct 279 ms 54536 KB Output is correct
37 Correct 289 ms 53608 KB Output is correct
38 Correct 411 ms 56196 KB Output is correct
39 Correct 393 ms 54344 KB Output is correct
40 Correct 231 ms 54724 KB Output is correct
41 Correct 877 ms 56296 KB Output is correct
42 Correct 216 ms 35912 KB Output is correct
43 Correct 58 ms 19672 KB Output is correct
44 Correct 479 ms 53360 KB Output is correct
45 Correct 720 ms 54892 KB Output is correct
46 Correct 853 ms 53136 KB Output is correct
47 Correct 812 ms 52828 KB Output is correct
48 Correct 339 ms 55364 KB Output is correct
49 Correct 432 ms 57696 KB Output is correct
50 Correct 531 ms 56332 KB Output is correct
51 Correct 533 ms 56404 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 886 ms 58160 KB Output is correct
54 Correct 489 ms 54760 KB Output is correct
55 Correct 756 ms 55920 KB Output is correct
56 Correct 873 ms 54716 KB Output is correct
57 Correct 1684 ms 273948 KB Output is correct
58 Correct 2336 ms 303836 KB Output is correct
59 Correct 2939 ms 279816 KB Output is correct
60 Correct 2923 ms 279248 KB Output is correct
61 Correct 5747 ms 287336 KB Output is correct
62 Correct 3048 ms 268468 KB Output is correct
63 Correct 4217 ms 265760 KB Output is correct
64 Correct 5460 ms 270996 KB Output is correct
65 Correct 327 ms 55484 KB Output is correct
66 Correct 437 ms 57936 KB Output is correct
67 Correct 538 ms 62548 KB Output is correct
68 Correct 567 ms 62808 KB Output is correct
69 Correct 929 ms 61796 KB Output is correct
70 Correct 0 ms 348 KB Output is correct
71 Correct 909 ms 64652 KB Output is correct
72 Correct 485 ms 60596 KB Output is correct
73 Correct 745 ms 63524 KB Output is correct
74 Correct 848 ms 58124 KB Output is correct
75 Correct 1649 ms 311232 KB Output is correct
76 Correct 2322 ms 322676 KB Output is correct
77 Correct 2932 ms 309724 KB Output is correct
78 Correct 2991 ms 309780 KB Output is correct
79 Correct 5803 ms 322808 KB Output is correct
80 Correct 2990 ms 297156 KB Output is correct
81 Correct 4614 ms 309688 KB Output is correct
82 Correct 5670 ms 290584 KB Output is correct
83 Correct 6213 ms 309472 KB Output is correct