Submission #604216

# Submission time Handle Problem Language Result Execution time Memory
604216 2022-07-24T21:08:21 Z elkernos Two Antennas (JOI19_antennas) C++17
100 / 100
746 ms 45164 KB
#include <bits/stdc++.h>

using namespace std;

struct ant {
    int h, a, b;
    void read() { cin >> h >> a >> b; }
};

const int oo = 1e9 + 213;

struct T {
    int d = -1, c = 0, lazy = oo;
    void merge(T l, T r)
    {
        assert(lazy == oo);
        d = max({d, l.d, r.d}); // why:c
        c = max(l.c, r.c);
    }
};

const int nax = 2e5 + 213;

struct tree {
#define lc 2 * v
#define rc 2 * v + 1
#define m (l + r) / 2
    vector<T> t = vector<T>(4 * nax);
    void push(int v, int l, int r)
    {
        if(t[v].lazy == oo) {
            return;
        }
        if(l != r) {
            for(int to : {lc, rc}) {
                t[to].lazy = min(t[to].lazy, t[v].lazy);
            }
        }
        t[v].d = max(t[v].d, t[v].c - t[v].lazy);
        t[v].lazy = oo;
    }
    void setC(int v, int l, int r, int p, int x)
    {
        push(v, l, r);
        if(l == r) {
            t[v].c = x;
            return;
        }
        if(p <= m) {
            setC(lc, l, m, p, x);
        }
        else {
            setC(rc, m + 1, r, p, x);
        }
        t[v].merge(t[lc], t[rc]);
    }
    void putLazy(int v, int l, int r, int ql, int qr, int x)
    {
        push(v, l, r);
        if(ql <= l && r <= qr) {
            t[v].lazy = x;
            push(v, l, r);
            return;
        }
        if(ql <= m) {
            putLazy(lc, l, m, ql, qr, x);
        }
        if(m < qr) {
            putLazy(rc, m + 1, r, ql, qr, x);
        }
        t[v].merge(t[lc], t[rc]);
    }
    int query(int v, int l, int r, int ql, int qr)
    {
        push(v, l, r);
        if(ql <= l && r <= qr) {
            return t[v].d;
        }
        int ret = -1;
        if(ql <= m) {
            ret = max(ret, query(lc, l, m, ql, qr));
        }
        if(m < qr) {
            ret = max(ret, query(rc, m + 1, r, ql, qr));
        }
        t[v].merge(t[lc], t[rc]);
        return ret;
    }
};

int32_t main()
{
    cin.tie(0)->sync_with_stdio(0);
    int n;
    cin >> n;
    vector<ant> in(n + 1);
    for(int i = 1; i <= n; i++) {
        in[i].read();
    }
    int q;
    cin >> q;
    vector<int> ql(q + 1), qr(q + 1), ans(q + 1, -1);
    for(int i = 1; i <= q; i++) {
        cin >> ql[i] >> qr[i];
    }
    auto solve = [&]() {
        vector<vector<int>> qs(n + 1), st(n + 1), en(n + 1);
        tree seg;
        for(int i = 1; i <= q; i++) {
            qs[qr[i]].emplace_back(i);
        }
        for(int i = 1; i <= n; i++) {
            if(i + in[i].a <= n) {
                st[i + in[i].a].emplace_back(i);
            }
            if(i + in[i].b <= n) {
                en[i + in[i].b].emplace_back(i);
            }
            for(int j : st[i]) {
                seg.setC(1, 1, n, j, in[j].h);
            }
            if(i - in[i].a >= 1) {
                seg.putLazy(1, 1, n, max(1, i - in[i].b), i - in[i].a, in[i].h);
            }
            for(int j : qs[i]) {
                ans[j] = max(ans[j], seg.query(1, 1, n, ql[j], qr[j]));
            }
            for(int j : en[i]) {
                seg.setC(1, 1, n, j, 0);
            }
        }
    };
    solve();
    reverse(1 + in.begin(), in.end());
    for(int i = 1; i <= q; i++) {
        ql[i] = n - ql[i] + 1;
        qr[i] = n - qr[i] + 1;
        swap(ql[i], qr[i]);
    }
    solve();
    for(int i = 1; i <= q; i++) {
        cout << ans[i] << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9684 KB Output is correct
2 Correct 9 ms 9768 KB Output is correct
3 Correct 9 ms 9700 KB Output is correct
4 Correct 11 ms 9796 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9768 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 8 ms 9768 KB Output is correct
9 Correct 9 ms 9768 KB Output is correct
10 Correct 9 ms 9768 KB Output is correct
11 Correct 9 ms 9768 KB Output is correct
12 Correct 9 ms 9768 KB Output is correct
13 Correct 11 ms 9768 KB Output is correct
14 Correct 11 ms 9684 KB Output is correct
15 Correct 9 ms 9720 KB Output is correct
16 Correct 9 ms 9784 KB Output is correct
17 Correct 12 ms 9728 KB Output is correct
18 Correct 9 ms 9744 KB Output is correct
19 Correct 10 ms 9768 KB Output is correct
20 Correct 13 ms 9684 KB Output is correct
21 Correct 9 ms 9768 KB Output is correct
22 Correct 9 ms 9768 KB Output is correct
23 Correct 9 ms 9732 KB Output is correct
24 Correct 9 ms 9768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9684 KB Output is correct
2 Correct 9 ms 9768 KB Output is correct
3 Correct 9 ms 9700 KB Output is correct
4 Correct 11 ms 9796 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9768 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 8 ms 9768 KB Output is correct
9 Correct 9 ms 9768 KB Output is correct
10 Correct 9 ms 9768 KB Output is correct
11 Correct 9 ms 9768 KB Output is correct
12 Correct 9 ms 9768 KB Output is correct
13 Correct 11 ms 9768 KB Output is correct
14 Correct 11 ms 9684 KB Output is correct
15 Correct 9 ms 9720 KB Output is correct
16 Correct 9 ms 9784 KB Output is correct
17 Correct 12 ms 9728 KB Output is correct
18 Correct 9 ms 9744 KB Output is correct
19 Correct 10 ms 9768 KB Output is correct
20 Correct 13 ms 9684 KB Output is correct
21 Correct 9 ms 9768 KB Output is correct
22 Correct 9 ms 9768 KB Output is correct
23 Correct 9 ms 9732 KB Output is correct
24 Correct 9 ms 9768 KB Output is correct
25 Correct 124 ms 13656 KB Output is correct
26 Correct 22 ms 10420 KB Output is correct
27 Correct 168 ms 17020 KB Output is correct
28 Correct 183 ms 17104 KB Output is correct
29 Correct 119 ms 15084 KB Output is correct
30 Correct 148 ms 14828 KB Output is correct
31 Correct 116 ms 16272 KB Output is correct
32 Correct 168 ms 17168 KB Output is correct
33 Correct 151 ms 16656 KB Output is correct
34 Correct 84 ms 13384 KB Output is correct
35 Correct 151 ms 17212 KB Output is correct
36 Correct 164 ms 17108 KB Output is correct
37 Correct 107 ms 13608 KB Output is correct
38 Correct 154 ms 16196 KB Output is correct
39 Correct 30 ms 10908 KB Output is correct
40 Correct 154 ms 16208 KB Output is correct
41 Correct 145 ms 14600 KB Output is correct
42 Correct 163 ms 16188 KB Output is correct
43 Correct 56 ms 12120 KB Output is correct
44 Correct 151 ms 16196 KB Output is correct
45 Correct 48 ms 11008 KB Output is correct
46 Correct 164 ms 16204 KB Output is correct
47 Correct 49 ms 11552 KB Output is correct
48 Correct 149 ms 16120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 366 ms 27680 KB Output is correct
2 Correct 339 ms 34188 KB Output is correct
3 Correct 212 ms 27028 KB Output is correct
4 Correct 406 ms 34200 KB Output is correct
5 Correct 134 ms 20852 KB Output is correct
6 Correct 353 ms 34272 KB Output is correct
7 Correct 282 ms 30976 KB Output is correct
8 Correct 341 ms 34316 KB Output is correct
9 Correct 45 ms 13540 KB Output is correct
10 Correct 407 ms 34252 KB Output is correct
11 Correct 191 ms 24948 KB Output is correct
12 Correct 368 ms 34196 KB Output is correct
13 Correct 213 ms 33160 KB Output is correct
14 Correct 199 ms 32912 KB Output is correct
15 Correct 190 ms 32060 KB Output is correct
16 Correct 214 ms 32224 KB Output is correct
17 Correct 216 ms 33352 KB Output is correct
18 Correct 198 ms 32272 KB Output is correct
19 Correct 213 ms 32480 KB Output is correct
20 Correct 203 ms 32576 KB Output is correct
21 Correct 214 ms 33132 KB Output is correct
22 Correct 210 ms 32572 KB Output is correct
23 Correct 208 ms 32856 KB Output is correct
24 Correct 183 ms 31840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9684 KB Output is correct
2 Correct 9 ms 9768 KB Output is correct
3 Correct 9 ms 9700 KB Output is correct
4 Correct 11 ms 9796 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9768 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 8 ms 9768 KB Output is correct
9 Correct 9 ms 9768 KB Output is correct
10 Correct 9 ms 9768 KB Output is correct
11 Correct 9 ms 9768 KB Output is correct
12 Correct 9 ms 9768 KB Output is correct
13 Correct 11 ms 9768 KB Output is correct
14 Correct 11 ms 9684 KB Output is correct
15 Correct 9 ms 9720 KB Output is correct
16 Correct 9 ms 9784 KB Output is correct
17 Correct 12 ms 9728 KB Output is correct
18 Correct 9 ms 9744 KB Output is correct
19 Correct 10 ms 9768 KB Output is correct
20 Correct 13 ms 9684 KB Output is correct
21 Correct 9 ms 9768 KB Output is correct
22 Correct 9 ms 9768 KB Output is correct
23 Correct 9 ms 9732 KB Output is correct
24 Correct 9 ms 9768 KB Output is correct
25 Correct 124 ms 13656 KB Output is correct
26 Correct 22 ms 10420 KB Output is correct
27 Correct 168 ms 17020 KB Output is correct
28 Correct 183 ms 17104 KB Output is correct
29 Correct 119 ms 15084 KB Output is correct
30 Correct 148 ms 14828 KB Output is correct
31 Correct 116 ms 16272 KB Output is correct
32 Correct 168 ms 17168 KB Output is correct
33 Correct 151 ms 16656 KB Output is correct
34 Correct 84 ms 13384 KB Output is correct
35 Correct 151 ms 17212 KB Output is correct
36 Correct 164 ms 17108 KB Output is correct
37 Correct 107 ms 13608 KB Output is correct
38 Correct 154 ms 16196 KB Output is correct
39 Correct 30 ms 10908 KB Output is correct
40 Correct 154 ms 16208 KB Output is correct
41 Correct 145 ms 14600 KB Output is correct
42 Correct 163 ms 16188 KB Output is correct
43 Correct 56 ms 12120 KB Output is correct
44 Correct 151 ms 16196 KB Output is correct
45 Correct 48 ms 11008 KB Output is correct
46 Correct 164 ms 16204 KB Output is correct
47 Correct 49 ms 11552 KB Output is correct
48 Correct 149 ms 16120 KB Output is correct
49 Correct 366 ms 27680 KB Output is correct
50 Correct 339 ms 34188 KB Output is correct
51 Correct 212 ms 27028 KB Output is correct
52 Correct 406 ms 34200 KB Output is correct
53 Correct 134 ms 20852 KB Output is correct
54 Correct 353 ms 34272 KB Output is correct
55 Correct 282 ms 30976 KB Output is correct
56 Correct 341 ms 34316 KB Output is correct
57 Correct 45 ms 13540 KB Output is correct
58 Correct 407 ms 34252 KB Output is correct
59 Correct 191 ms 24948 KB Output is correct
60 Correct 368 ms 34196 KB Output is correct
61 Correct 213 ms 33160 KB Output is correct
62 Correct 199 ms 32912 KB Output is correct
63 Correct 190 ms 32060 KB Output is correct
64 Correct 214 ms 32224 KB Output is correct
65 Correct 216 ms 33352 KB Output is correct
66 Correct 198 ms 32272 KB Output is correct
67 Correct 213 ms 32480 KB Output is correct
68 Correct 203 ms 32576 KB Output is correct
69 Correct 214 ms 33132 KB Output is correct
70 Correct 210 ms 32572 KB Output is correct
71 Correct 208 ms 32856 KB Output is correct
72 Correct 183 ms 31840 KB Output is correct
73 Correct 623 ms 40100 KB Output is correct
74 Correct 413 ms 35076 KB Output is correct
75 Correct 594 ms 37044 KB Output is correct
76 Correct 724 ms 45088 KB Output is correct
77 Correct 360 ms 28084 KB Output is correct
78 Correct 595 ms 41940 KB Output is correct
79 Correct 677 ms 41520 KB Output is correct
80 Correct 746 ms 45164 KB Output is correct
81 Correct 280 ms 21344 KB Output is correct
82 Correct 526 ms 39832 KB Output is correct
83 Correct 570 ms 34904 KB Output is correct
84 Correct 740 ms 45020 KB Output is correct
85 Correct 453 ms 39188 KB Output is correct
86 Correct 534 ms 42576 KB Output is correct
87 Correct 280 ms 33676 KB Output is correct
88 Correct 549 ms 41484 KB Output is correct
89 Correct 533 ms 40720 KB Output is correct
90 Correct 601 ms 41948 KB Output is correct
91 Correct 365 ms 36168 KB Output is correct
92 Correct 625 ms 42348 KB Output is correct
93 Correct 274 ms 35076 KB Output is correct
94 Correct 715 ms 42280 KB Output is correct
95 Correct 403 ms 35532 KB Output is correct
96 Correct 638 ms 41452 KB Output is correct