Submission #544712

# Submission time Handle Problem Language Result Execution time Memory
544712 2022-04-02T16:01:49 Z eecs Tortoise (CEOI21_tortoise) C++17
100 / 100
726 ms 48456 KB
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
const int maxn = 500010, offset = 1 << 19, INF = 1e9;
int n, a[maxn], l[maxn], r[maxn], before[maxn];
unordered_map<int, int> specialChair;

struct SEG {
    vector<int> t, p;
    SEG() { t.resize(2 * offset), p.resize(2 * offset); }

    void init() {
        for (int i = offset; i < 2 * offset; i++) {
            t[i] = INF + (i - offset);
        }
        for (int i = offset - 1; i; i--) {
            t[i] = min(t[i * 2], t[i * 2 + 1]);
        }
    }

    void prop(int x) {
        if (p[x]) {
            t[x] += p[x];
            if (x < offset) {
                p[x * 2] += p[x];
                p[x * 2 + 1] += p[x];
            }
            p[x] = 0;
        }
    }

    void upd(int a, int b, int v, int x = 1, int lo = 0, int hi = offset) {
        if (lo >= a && hi <= b) { p[x] += v, prop(x); return; }
        prop(x);
        if (lo >= b || hi <= a) return;
        int mi = (lo + hi) >> 1;
        upd(a, b, v, x * 2, lo, mi);
        upd(a, b, v, x * 2 + 1, mi, hi);
        t[x] = min(t[x * 2], t[x * 2 + 1]);
    }
    int get(int a, int b, int x = 1, int lo = 0, int hi = offset) {
        if (lo >= b || hi <= a) return INF;
        prop(x);
        if (lo >= a && hi <= b) return t[x];
        int mi = (lo + hi) >> 1;
        return min(get(a, b, x * 2, lo, mi), get(a, b, x * 2 + 1, mi, hi));
    }
} special, updates, tracker;

void moveSpecial(int a, int b) {
    special.upd(a, a + 1, +INF);
    special.upd(b, b + 1, -INF);
}
void addSpecial(int a) { special.upd(a, a + 1, -INF); }

long long sum;

void solve(int x, int d, int type) {
    int cnt = 0;
    while (a[x] > 0) {
        if (tracker.get(x, offset) < 2 * d) break;
        if (x - updates.get(x, x + 1) < type * d * 2) break;
        if (r[x] != -1) {
            int spec = specialChair[r[x]];
            if (type) spec = min(spec, x);
            if (a[spec] == 1 && spec == x) {
                if (!type) break;
                spec = before[spec];
            }
            if (spec <= l[x]) break;
            if (a[spec] <= 0) break;
            moveSpecial(specialChair[r[x]], spec);
            special.upd(x + type, offset, -2 * d);
            int diff = special.get(0, offset);
            if (diff < 0) {
                special.upd(x + type, offset, 2 * d);
                moveSpecial(spec, specialChair[r[x]]);
                break;
            }
            specialChair[r[x]] = spec;
        }
        if (cnt == 0) tracker.upd(x, x + 1, -INF - 2 * d * (type - 1));
        tracker.upd(x, offset, -2 * d);
        updates.upd(x, offset, 2 * d);
        a[x]--, sum--, cnt++;
    }
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        if (a[i] != -1) sum += a[i];
    }
    int tmp = -1;
    for (int i = 0; i < n; i++) {
        if (a[i] == -1) tmp = i;
        l[i] = tmp;
    }
    tmp = -1;
    for (int i = n - 1; i >= 0; --i) {
        if (a[i] == -1) tmp = i;
        r[i] = tmp;
    }
    tmp = -1;
    for (int i = 0; i < n; i++) {
        before[i] = tmp;
        if (a[i] > 0) tmp = i;
    }
    special.init(), tracker.init();
    for (int i = 0; i < n; i++) {
        if (a[i] > 0 && r[i] != -1 && before[r[i]] == i) {
            addSpecial(i);
            specialChair[r[i]] = i;
        }
    }
    vector<pair<int, pii>> cand;
    for (int i = 0; i < n; i++) if (a[i] > 0) {
        if (r[i] != -1) cand.push_back({r[i] - i, {i, 1}});
        if (l[i] != -1) cand.push_back({i - l[i], {i, 0}});
    }
    sort(cand.begin(), cand.end());
    for (auto &p : cand) {
        solve(p.second.first, p.first, p.second.second);
    }
    for (int i = 0; i < n; i++) {
        if (a[i] > 0 && r[i] != -1) a[r[i]] = -2;
        if (a[i] == -2) sum--;
    }
    cout << sum << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 24916 KB Output is correct
2 Correct 12 ms 24912 KB Output is correct
3 Correct 11 ms 24916 KB Output is correct
4 Correct 13 ms 24920 KB Output is correct
5 Correct 12 ms 24916 KB Output is correct
6 Correct 12 ms 24984 KB Output is correct
7 Correct 12 ms 24916 KB Output is correct
8 Correct 12 ms 24988 KB Output is correct
9 Correct 11 ms 24916 KB Output is correct
10 Correct 12 ms 24916 KB Output is correct
11 Correct 11 ms 24916 KB Output is correct
12 Correct 12 ms 24980 KB Output is correct
13 Correct 11 ms 24976 KB Output is correct
14 Correct 11 ms 24916 KB Output is correct
15 Correct 11 ms 24908 KB Output is correct
16 Correct 12 ms 24984 KB Output is correct
17 Correct 11 ms 24916 KB Output is correct
18 Correct 11 ms 24916 KB Output is correct
19 Correct 12 ms 24916 KB Output is correct
20 Correct 12 ms 24916 KB Output is correct
21 Correct 11 ms 24928 KB Output is correct
22 Correct 12 ms 24984 KB Output is correct
23 Correct 12 ms 24924 KB Output is correct
24 Correct 12 ms 24980 KB Output is correct
25 Correct 12 ms 24984 KB Output is correct
26 Correct 11 ms 25044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 24916 KB Output is correct
2 Correct 12 ms 24912 KB Output is correct
3 Correct 11 ms 24916 KB Output is correct
4 Correct 13 ms 24920 KB Output is correct
5 Correct 12 ms 24916 KB Output is correct
6 Correct 12 ms 24984 KB Output is correct
7 Correct 12 ms 24916 KB Output is correct
8 Correct 12 ms 24988 KB Output is correct
9 Correct 11 ms 24916 KB Output is correct
10 Correct 12 ms 24916 KB Output is correct
11 Correct 11 ms 24916 KB Output is correct
12 Correct 12 ms 24980 KB Output is correct
13 Correct 11 ms 24976 KB Output is correct
14 Correct 11 ms 24916 KB Output is correct
15 Correct 11 ms 24908 KB Output is correct
16 Correct 12 ms 24984 KB Output is correct
17 Correct 11 ms 24916 KB Output is correct
18 Correct 11 ms 24916 KB Output is correct
19 Correct 12 ms 24916 KB Output is correct
20 Correct 12 ms 24916 KB Output is correct
21 Correct 11 ms 24928 KB Output is correct
22 Correct 12 ms 24984 KB Output is correct
23 Correct 12 ms 24924 KB Output is correct
24 Correct 12 ms 24980 KB Output is correct
25 Correct 12 ms 24984 KB Output is correct
26 Correct 11 ms 25044 KB Output is correct
27 Correct 11 ms 24932 KB Output is correct
28 Correct 11 ms 25020 KB Output is correct
29 Correct 12 ms 24916 KB Output is correct
30 Correct 12 ms 24956 KB Output is correct
31 Correct 12 ms 24916 KB Output is correct
32 Correct 12 ms 24916 KB Output is correct
33 Correct 12 ms 24916 KB Output is correct
34 Correct 13 ms 24980 KB Output is correct
35 Correct 12 ms 24952 KB Output is correct
36 Correct 12 ms 24916 KB Output is correct
37 Correct 11 ms 24916 KB Output is correct
38 Correct 12 ms 24916 KB Output is correct
39 Correct 12 ms 24916 KB Output is correct
40 Correct 12 ms 24980 KB Output is correct
41 Correct 11 ms 24916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 24916 KB Output is correct
2 Correct 12 ms 24912 KB Output is correct
3 Correct 11 ms 24916 KB Output is correct
4 Correct 13 ms 24920 KB Output is correct
5 Correct 12 ms 24916 KB Output is correct
6 Correct 12 ms 24984 KB Output is correct
7 Correct 12 ms 24916 KB Output is correct
8 Correct 12 ms 24988 KB Output is correct
9 Correct 11 ms 24916 KB Output is correct
10 Correct 12 ms 24916 KB Output is correct
11 Correct 11 ms 24916 KB Output is correct
12 Correct 12 ms 24980 KB Output is correct
13 Correct 11 ms 24976 KB Output is correct
14 Correct 11 ms 24916 KB Output is correct
15 Correct 11 ms 24908 KB Output is correct
16 Correct 12 ms 24984 KB Output is correct
17 Correct 11 ms 24916 KB Output is correct
18 Correct 11 ms 24916 KB Output is correct
19 Correct 12 ms 24916 KB Output is correct
20 Correct 12 ms 24916 KB Output is correct
21 Correct 11 ms 24928 KB Output is correct
22 Correct 12 ms 24984 KB Output is correct
23 Correct 12 ms 24924 KB Output is correct
24 Correct 12 ms 24980 KB Output is correct
25 Correct 12 ms 24984 KB Output is correct
26 Correct 11 ms 25044 KB Output is correct
27 Correct 11 ms 24932 KB Output is correct
28 Correct 11 ms 25020 KB Output is correct
29 Correct 12 ms 24916 KB Output is correct
30 Correct 12 ms 24956 KB Output is correct
31 Correct 12 ms 24916 KB Output is correct
32 Correct 12 ms 24916 KB Output is correct
33 Correct 12 ms 24916 KB Output is correct
34 Correct 13 ms 24980 KB Output is correct
35 Correct 12 ms 24952 KB Output is correct
36 Correct 12 ms 24916 KB Output is correct
37 Correct 11 ms 24916 KB Output is correct
38 Correct 12 ms 24916 KB Output is correct
39 Correct 12 ms 24916 KB Output is correct
40 Correct 12 ms 24980 KB Output is correct
41 Correct 11 ms 24916 KB Output is correct
42 Correct 12 ms 24984 KB Output is correct
43 Correct 11 ms 24916 KB Output is correct
44 Correct 12 ms 24916 KB Output is correct
45 Correct 12 ms 25012 KB Output is correct
46 Correct 14 ms 25112 KB Output is correct
47 Correct 12 ms 24916 KB Output is correct
48 Correct 13 ms 24988 KB Output is correct
49 Correct 12 ms 24980 KB Output is correct
50 Correct 12 ms 24988 KB Output is correct
51 Correct 12 ms 24992 KB Output is correct
52 Correct 12 ms 24980 KB Output is correct
53 Correct 12 ms 24992 KB Output is correct
54 Correct 12 ms 24916 KB Output is correct
55 Correct 12 ms 24916 KB Output is correct
56 Correct 12 ms 24916 KB Output is correct
57 Correct 11 ms 24916 KB Output is correct
58 Correct 13 ms 24992 KB Output is correct
59 Correct 11 ms 24916 KB Output is correct
60 Correct 12 ms 25116 KB Output is correct
61 Correct 12 ms 24976 KB Output is correct
62 Correct 12 ms 25012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 24916 KB Output is correct
2 Correct 12 ms 24912 KB Output is correct
3 Correct 11 ms 24916 KB Output is correct
4 Correct 13 ms 24920 KB Output is correct
5 Correct 12 ms 24916 KB Output is correct
6 Correct 12 ms 24984 KB Output is correct
7 Correct 12 ms 24916 KB Output is correct
8 Correct 12 ms 24988 KB Output is correct
9 Correct 11 ms 24916 KB Output is correct
10 Correct 12 ms 24916 KB Output is correct
11 Correct 11 ms 24916 KB Output is correct
12 Correct 12 ms 24980 KB Output is correct
13 Correct 11 ms 24976 KB Output is correct
14 Correct 11 ms 24916 KB Output is correct
15 Correct 11 ms 24908 KB Output is correct
16 Correct 12 ms 24984 KB Output is correct
17 Correct 11 ms 24916 KB Output is correct
18 Correct 11 ms 24916 KB Output is correct
19 Correct 12 ms 24916 KB Output is correct
20 Correct 12 ms 24916 KB Output is correct
21 Correct 11 ms 24928 KB Output is correct
22 Correct 12 ms 24984 KB Output is correct
23 Correct 12 ms 24924 KB Output is correct
24 Correct 12 ms 24980 KB Output is correct
25 Correct 12 ms 24984 KB Output is correct
26 Correct 11 ms 25044 KB Output is correct
27 Correct 11 ms 24932 KB Output is correct
28 Correct 11 ms 25020 KB Output is correct
29 Correct 12 ms 24916 KB Output is correct
30 Correct 12 ms 24956 KB Output is correct
31 Correct 12 ms 24916 KB Output is correct
32 Correct 12 ms 24916 KB Output is correct
33 Correct 12 ms 24916 KB Output is correct
34 Correct 13 ms 24980 KB Output is correct
35 Correct 12 ms 24952 KB Output is correct
36 Correct 12 ms 24916 KB Output is correct
37 Correct 11 ms 24916 KB Output is correct
38 Correct 12 ms 24916 KB Output is correct
39 Correct 12 ms 24916 KB Output is correct
40 Correct 12 ms 24980 KB Output is correct
41 Correct 11 ms 24916 KB Output is correct
42 Correct 12 ms 24984 KB Output is correct
43 Correct 11 ms 24916 KB Output is correct
44 Correct 12 ms 24916 KB Output is correct
45 Correct 12 ms 25012 KB Output is correct
46 Correct 14 ms 25112 KB Output is correct
47 Correct 12 ms 24916 KB Output is correct
48 Correct 13 ms 24988 KB Output is correct
49 Correct 12 ms 24980 KB Output is correct
50 Correct 12 ms 24988 KB Output is correct
51 Correct 12 ms 24992 KB Output is correct
52 Correct 12 ms 24980 KB Output is correct
53 Correct 12 ms 24992 KB Output is correct
54 Correct 12 ms 24916 KB Output is correct
55 Correct 12 ms 24916 KB Output is correct
56 Correct 12 ms 24916 KB Output is correct
57 Correct 11 ms 24916 KB Output is correct
58 Correct 13 ms 24992 KB Output is correct
59 Correct 11 ms 24916 KB Output is correct
60 Correct 12 ms 25116 KB Output is correct
61 Correct 12 ms 24976 KB Output is correct
62 Correct 12 ms 25012 KB Output is correct
63 Correct 15 ms 25172 KB Output is correct
64 Correct 18 ms 25172 KB Output is correct
65 Correct 15 ms 25112 KB Output is correct
66 Correct 16 ms 25124 KB Output is correct
67 Correct 16 ms 25200 KB Output is correct
68 Correct 16 ms 25172 KB Output is correct
69 Correct 16 ms 25124 KB Output is correct
70 Correct 17 ms 25252 KB Output is correct
71 Correct 19 ms 25300 KB Output is correct
72 Correct 18 ms 25300 KB Output is correct
73 Correct 18 ms 25300 KB Output is correct
74 Correct 18 ms 25400 KB Output is correct
75 Correct 12 ms 25000 KB Output is correct
76 Correct 16 ms 25172 KB Output is correct
77 Correct 16 ms 25172 KB Output is correct
78 Correct 16 ms 25248 KB Output is correct
79 Correct 17 ms 25172 KB Output is correct
80 Correct 15 ms 25172 KB Output is correct
81 Correct 16 ms 25172 KB Output is correct
82 Correct 17 ms 25172 KB Output is correct
83 Correct 17 ms 25296 KB Output is correct
84 Correct 17 ms 25252 KB Output is correct
85 Correct 19 ms 25172 KB Output is correct
86 Correct 18 ms 25280 KB Output is correct
87 Correct 17 ms 25264 KB Output is correct
88 Correct 19 ms 25224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 24916 KB Output is correct
2 Correct 12 ms 24912 KB Output is correct
3 Correct 11 ms 24916 KB Output is correct
4 Correct 13 ms 24920 KB Output is correct
5 Correct 12 ms 24916 KB Output is correct
6 Correct 12 ms 24984 KB Output is correct
7 Correct 12 ms 24916 KB Output is correct
8 Correct 12 ms 24988 KB Output is correct
9 Correct 11 ms 24916 KB Output is correct
10 Correct 12 ms 24916 KB Output is correct
11 Correct 11 ms 24916 KB Output is correct
12 Correct 12 ms 24980 KB Output is correct
13 Correct 11 ms 24976 KB Output is correct
14 Correct 11 ms 24916 KB Output is correct
15 Correct 11 ms 24908 KB Output is correct
16 Correct 12 ms 24984 KB Output is correct
17 Correct 11 ms 24916 KB Output is correct
18 Correct 11 ms 24916 KB Output is correct
19 Correct 12 ms 24916 KB Output is correct
20 Correct 12 ms 24916 KB Output is correct
21 Correct 11 ms 24928 KB Output is correct
22 Correct 12 ms 24984 KB Output is correct
23 Correct 12 ms 24924 KB Output is correct
24 Correct 12 ms 24980 KB Output is correct
25 Correct 12 ms 24984 KB Output is correct
26 Correct 11 ms 25044 KB Output is correct
27 Correct 11 ms 24932 KB Output is correct
28 Correct 11 ms 25020 KB Output is correct
29 Correct 12 ms 24916 KB Output is correct
30 Correct 12 ms 24956 KB Output is correct
31 Correct 12 ms 24916 KB Output is correct
32 Correct 12 ms 24916 KB Output is correct
33 Correct 12 ms 24916 KB Output is correct
34 Correct 13 ms 24980 KB Output is correct
35 Correct 12 ms 24952 KB Output is correct
36 Correct 12 ms 24916 KB Output is correct
37 Correct 11 ms 24916 KB Output is correct
38 Correct 12 ms 24916 KB Output is correct
39 Correct 12 ms 24916 KB Output is correct
40 Correct 12 ms 24980 KB Output is correct
41 Correct 11 ms 24916 KB Output is correct
42 Correct 12 ms 24984 KB Output is correct
43 Correct 11 ms 24916 KB Output is correct
44 Correct 12 ms 24916 KB Output is correct
45 Correct 12 ms 25012 KB Output is correct
46 Correct 14 ms 25112 KB Output is correct
47 Correct 12 ms 24916 KB Output is correct
48 Correct 13 ms 24988 KB Output is correct
49 Correct 12 ms 24980 KB Output is correct
50 Correct 12 ms 24988 KB Output is correct
51 Correct 12 ms 24992 KB Output is correct
52 Correct 12 ms 24980 KB Output is correct
53 Correct 12 ms 24992 KB Output is correct
54 Correct 12 ms 24916 KB Output is correct
55 Correct 12 ms 24916 KB Output is correct
56 Correct 12 ms 24916 KB Output is correct
57 Correct 11 ms 24916 KB Output is correct
58 Correct 13 ms 24992 KB Output is correct
59 Correct 11 ms 24916 KB Output is correct
60 Correct 12 ms 25116 KB Output is correct
61 Correct 12 ms 24976 KB Output is correct
62 Correct 12 ms 25012 KB Output is correct
63 Correct 15 ms 25172 KB Output is correct
64 Correct 18 ms 25172 KB Output is correct
65 Correct 15 ms 25112 KB Output is correct
66 Correct 16 ms 25124 KB Output is correct
67 Correct 16 ms 25200 KB Output is correct
68 Correct 16 ms 25172 KB Output is correct
69 Correct 16 ms 25124 KB Output is correct
70 Correct 17 ms 25252 KB Output is correct
71 Correct 19 ms 25300 KB Output is correct
72 Correct 18 ms 25300 KB Output is correct
73 Correct 18 ms 25300 KB Output is correct
74 Correct 18 ms 25400 KB Output is correct
75 Correct 12 ms 25000 KB Output is correct
76 Correct 16 ms 25172 KB Output is correct
77 Correct 16 ms 25172 KB Output is correct
78 Correct 16 ms 25248 KB Output is correct
79 Correct 17 ms 25172 KB Output is correct
80 Correct 15 ms 25172 KB Output is correct
81 Correct 16 ms 25172 KB Output is correct
82 Correct 17 ms 25172 KB Output is correct
83 Correct 17 ms 25296 KB Output is correct
84 Correct 17 ms 25252 KB Output is correct
85 Correct 19 ms 25172 KB Output is correct
86 Correct 18 ms 25280 KB Output is correct
87 Correct 17 ms 25264 KB Output is correct
88 Correct 19 ms 25224 KB Output is correct
89 Correct 317 ms 39416 KB Output is correct
90 Correct 329 ms 39440 KB Output is correct
91 Correct 316 ms 39364 KB Output is correct
92 Correct 502 ms 43084 KB Output is correct
93 Correct 480 ms 42988 KB Output is correct
94 Correct 482 ms 43088 KB Output is correct
95 Correct 376 ms 39520 KB Output is correct
96 Correct 404 ms 39776 KB Output is correct
97 Correct 441 ms 41760 KB Output is correct
98 Correct 326 ms 40260 KB Output is correct
99 Correct 713 ms 48340 KB Output is correct
100 Correct 726 ms 48456 KB Output is correct
101 Correct 726 ms 47948 KB Output is correct
102 Correct 714 ms 47492 KB Output is correct
103 Correct 38 ms 33736 KB Output is correct
104 Correct 41 ms 26456 KB Output is correct
105 Correct 42 ms 26436 KB Output is correct
106 Correct 42 ms 26404 KB Output is correct
107 Correct 32 ms 26040 KB Output is correct
108 Correct 37 ms 26448 KB Output is correct
109 Correct 245 ms 33860 KB Output is correct