Submission #1107532

# Submission time Handle Problem Language Result Execution time Memory
1107532 2024-11-01T11:53:59 Z qwerasdfzxcl Tortoise (CEOI21_tortoise) C++17
100 / 100
434 ms 39632 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

constexpr int INF = 1e9 + 100;

int a[500500];

struct Seg {
    int tree[2002000], lazy[2002000];

    void init(int i, int l, int r, const vector<int> &V) {
        lazy[i] = 0;
        if (l==r) {tree[i] = V[l]; return;}
        int m = (l+r)/2;
        init(i<<1, l, m, V);
        init(i<<1|1, m+1, r, V);
        tree[i] = min(tree[i<<1], tree[i<<1|1]);
    }

    void propagate(int i, int l, int r) {
        tree[i] += lazy[i];
        if (l!=r) lazy[i<<1] += lazy[i], lazy[i<<1|1] += lazy[i];
        lazy[i] = 0;
    }

    void update(int i, int l, int r, int s, int e, int x) {
        propagate(i, l, r);
        if (r<s || e<l) return;
        if (s<=l && r<=e) {
            lazy[i] += x;
            propagate(i, l, r);
            return;
        }

        int m = (l+r)/2;
        update(i<<1, l, m, s, e, x);
        update(i<<1|1, m+1, r, s, e, x);
        tree[i] = min(tree[i<<1], tree[i<<1|1]);
    }

    void set(int i, int l, int r, int p, int x){
        propagate(i, l, r);
        if (r<p || p<l) return;
        if (l==r) {
            tree[i] = x;
            return;
        }

        int m = (l+r)/2;
        set(i<<1, l, m, p, x);
        set(i<<1|1, m+1, r, p, x);
        tree[i] = min(tree[i<<1], tree[i<<1|1]);
    }

    int query(int i, int l, int r, int s, int e) {
        propagate(i, l, r);
        if (r<s || e<l) return INF;
        if (s<=l && r<=e) return tree[i];

        int m = (l+r)/2;
        return min(query(i<<1, l, m, s, e), query(i<<1|1, m+1, r, s, e));
    }
}tree, tree2;

int main() {
    int n;
    scanf("%d", &n);

    for (int i=1;i<=n;i++) scanf("%d", a+i);
    a[0] = a[n+1] = -1;
    ll ans = 0;
    for (int i=1;i<=n;i++) ans += max(a[i], 0);

    if (ans==0) {
        printf("0\n");
        return 0;
    }

    priority_queue<array<int, 5>, vector<array<int, 5>>, greater<array<int, 5>>> pq;
    vector<int> V, W(n+1, INF), chk(n+1);
    vector<vector<int>> L;

    for (int i=0,j=0;i<=n;i=j) {
        if (a[i]!=-1){j = i+1; continue;}
        j = i+1;
        while(j <= n && a[j]!=-1) j++;

        int ti = i==0?-INF:i, tj = j==n+1?INF:j;

        int r = -1, mx = -1, idx = -1;
        for (int k=i+1;k<j;k++) if (a[k] > 0) {
            r = k;
            if (mx < min(k-ti, tj-k)) {
                mx = min(k-ti, tj-k);
                idx = k;
            }
        }
        if (r < 0) continue;

        V.push_back(-1);
        L.emplace_back();
        W[r] = r-1;
        chk[r] = 1;
        ans--;

        for (int k=i+1;k<j;k++) if (a[k] > 0){
            L.back().push_back(k);
            int c = k==idx?a[k]-1:a[k];
            if (c==0) continue;

            if (k-ti <= tj-k){
                pq.push({k-ti, c, k, (int)V.size()-1, 0});
            } 
            else{
                pq.push({tj-k, c, k, (int)V.size()-1, 1});
            } 
        }
    }

    int sz = V.size();
    tree.init(1, 0, n, W);
    tree2.init(1, 0, sz-1, V);

    while(!pq.empty()) {
        auto [w, c, x, i, typ] = pq.top(); pq.pop();

        if (typ==0 && !chk[x]){
            int v = tree2.query(1, 0, sz-1, i, i) + x;
            
            if (v < 0) continue;
            if (tree.query(1, 0, n, x+1, n) < w*2) continue;

            chk[x] = 1;
            tree.set(1, 0, n, x, v);
            tree.update(1, 0, n, x+1, n, -w*2);
            tree2.update(1, 0, sz-1, i, sz-1, -w*2);

            if (c > 1) pq.push({w, c-1, x, i, typ});
        }

        else if (typ==1 && c==1 && L[i].size() >= 2){
            int y = L[i].end()[-2];
            int v = tree2.query(1, 0, sz-1, i, i) + y;

            if (v < 0) continue;
            if (tree.query(1, 0, n, y+1, n) < w*2) continue;

            chk[y] = 1;
            tree.set(1, 0, n, y, v);
            tree.update(1, 0, n, y+1, n, -w*2);
            tree2.update(1, 0, sz-1, i+1, sz-1, -w*2);

            L[i].pop_back();
        }

        else{
            if (tree.query(1, 0, n, x, n) < w*2) continue;

            tree.update(1, 0, n, x, n, -w*2);
            tree2.update(1, 0, sz-1, i+typ, sz-1, -w*2);

            if (c > 1) pq.push({w, c-1, x, i, typ});
        }

        ans--;
    }

    printf("%lld\n", ans);
}

Compilation message

tortoise.cpp: In function 'int main()':
tortoise.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
tortoise.cpp:71:33: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |     for (int i=1;i<=n;i++) scanf("%d", a+i);
      |                            ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 1 ms 8528 KB Output is correct
3 Correct 2 ms 8528 KB Output is correct
4 Correct 2 ms 8528 KB Output is correct
5 Correct 2 ms 8528 KB Output is correct
6 Correct 1 ms 8528 KB Output is correct
7 Correct 2 ms 8528 KB Output is correct
8 Correct 2 ms 8528 KB Output is correct
9 Correct 2 ms 8528 KB Output is correct
10 Correct 2 ms 8696 KB Output is correct
11 Correct 2 ms 8528 KB Output is correct
12 Correct 2 ms 8528 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 1 ms 8528 KB Output is correct
15 Correct 2 ms 8528 KB Output is correct
16 Correct 2 ms 8528 KB Output is correct
17 Correct 2 ms 8640 KB Output is correct
18 Correct 2 ms 8528 KB Output is correct
19 Correct 1 ms 8528 KB Output is correct
20 Correct 1 ms 8528 KB Output is correct
21 Correct 2 ms 8528 KB Output is correct
22 Correct 1 ms 8528 KB Output is correct
23 Correct 2 ms 8528 KB Output is correct
24 Correct 2 ms 8528 KB Output is correct
25 Correct 2 ms 8528 KB Output is correct
26 Correct 1 ms 8528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 1 ms 8528 KB Output is correct
3 Correct 2 ms 8528 KB Output is correct
4 Correct 2 ms 8528 KB Output is correct
5 Correct 2 ms 8528 KB Output is correct
6 Correct 1 ms 8528 KB Output is correct
7 Correct 2 ms 8528 KB Output is correct
8 Correct 2 ms 8528 KB Output is correct
9 Correct 2 ms 8528 KB Output is correct
10 Correct 2 ms 8696 KB Output is correct
11 Correct 2 ms 8528 KB Output is correct
12 Correct 2 ms 8528 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 1 ms 8528 KB Output is correct
15 Correct 2 ms 8528 KB Output is correct
16 Correct 2 ms 8528 KB Output is correct
17 Correct 2 ms 8640 KB Output is correct
18 Correct 2 ms 8528 KB Output is correct
19 Correct 1 ms 8528 KB Output is correct
20 Correct 1 ms 8528 KB Output is correct
21 Correct 2 ms 8528 KB Output is correct
22 Correct 1 ms 8528 KB Output is correct
23 Correct 2 ms 8528 KB Output is correct
24 Correct 2 ms 8528 KB Output is correct
25 Correct 2 ms 8528 KB Output is correct
26 Correct 1 ms 8528 KB Output is correct
27 Correct 2 ms 8528 KB Output is correct
28 Correct 1 ms 8636 KB Output is correct
29 Correct 2 ms 8528 KB Output is correct
30 Correct 2 ms 8528 KB Output is correct
31 Correct 2 ms 8640 KB Output is correct
32 Correct 2 ms 8784 KB Output is correct
33 Correct 2 ms 8640 KB Output is correct
34 Correct 2 ms 8644 KB Output is correct
35 Correct 2 ms 8784 KB Output is correct
36 Correct 1 ms 8528 KB Output is correct
37 Correct 2 ms 8528 KB Output is correct
38 Correct 2 ms 8640 KB Output is correct
39 Correct 1 ms 8528 KB Output is correct
40 Correct 1 ms 8640 KB Output is correct
41 Correct 2 ms 8696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 1 ms 8528 KB Output is correct
3 Correct 2 ms 8528 KB Output is correct
4 Correct 2 ms 8528 KB Output is correct
5 Correct 2 ms 8528 KB Output is correct
6 Correct 1 ms 8528 KB Output is correct
7 Correct 2 ms 8528 KB Output is correct
8 Correct 2 ms 8528 KB Output is correct
9 Correct 2 ms 8528 KB Output is correct
10 Correct 2 ms 8696 KB Output is correct
11 Correct 2 ms 8528 KB Output is correct
12 Correct 2 ms 8528 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 1 ms 8528 KB Output is correct
15 Correct 2 ms 8528 KB Output is correct
16 Correct 2 ms 8528 KB Output is correct
17 Correct 2 ms 8640 KB Output is correct
18 Correct 2 ms 8528 KB Output is correct
19 Correct 1 ms 8528 KB Output is correct
20 Correct 1 ms 8528 KB Output is correct
21 Correct 2 ms 8528 KB Output is correct
22 Correct 1 ms 8528 KB Output is correct
23 Correct 2 ms 8528 KB Output is correct
24 Correct 2 ms 8528 KB Output is correct
25 Correct 2 ms 8528 KB Output is correct
26 Correct 1 ms 8528 KB Output is correct
27 Correct 2 ms 8528 KB Output is correct
28 Correct 1 ms 8636 KB Output is correct
29 Correct 2 ms 8528 KB Output is correct
30 Correct 2 ms 8528 KB Output is correct
31 Correct 2 ms 8640 KB Output is correct
32 Correct 2 ms 8784 KB Output is correct
33 Correct 2 ms 8640 KB Output is correct
34 Correct 2 ms 8644 KB Output is correct
35 Correct 2 ms 8784 KB Output is correct
36 Correct 1 ms 8528 KB Output is correct
37 Correct 2 ms 8528 KB Output is correct
38 Correct 2 ms 8640 KB Output is correct
39 Correct 1 ms 8528 KB Output is correct
40 Correct 1 ms 8640 KB Output is correct
41 Correct 2 ms 8696 KB Output is correct
42 Correct 2 ms 8528 KB Output is correct
43 Correct 2 ms 8528 KB Output is correct
44 Correct 1 ms 8528 KB Output is correct
45 Correct 1 ms 8528 KB Output is correct
46 Correct 1 ms 8528 KB Output is correct
47 Correct 2 ms 8528 KB Output is correct
48 Correct 2 ms 8528 KB Output is correct
49 Correct 1 ms 8528 KB Output is correct
50 Correct 1 ms 8528 KB Output is correct
51 Correct 1 ms 8528 KB Output is correct
52 Correct 1 ms 8664 KB Output is correct
53 Correct 2 ms 8528 KB Output is correct
54 Correct 1 ms 8528 KB Output is correct
55 Correct 2 ms 8528 KB Output is correct
56 Correct 1 ms 8528 KB Output is correct
57 Correct 1 ms 8528 KB Output is correct
58 Correct 1 ms 8528 KB Output is correct
59 Correct 2 ms 8696 KB Output is correct
60 Correct 1 ms 8528 KB Output is correct
61 Correct 1 ms 8528 KB Output is correct
62 Correct 1 ms 8528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 1 ms 8528 KB Output is correct
3 Correct 2 ms 8528 KB Output is correct
4 Correct 2 ms 8528 KB Output is correct
5 Correct 2 ms 8528 KB Output is correct
6 Correct 1 ms 8528 KB Output is correct
7 Correct 2 ms 8528 KB Output is correct
8 Correct 2 ms 8528 KB Output is correct
9 Correct 2 ms 8528 KB Output is correct
10 Correct 2 ms 8696 KB Output is correct
11 Correct 2 ms 8528 KB Output is correct
12 Correct 2 ms 8528 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 1 ms 8528 KB Output is correct
15 Correct 2 ms 8528 KB Output is correct
16 Correct 2 ms 8528 KB Output is correct
17 Correct 2 ms 8640 KB Output is correct
18 Correct 2 ms 8528 KB Output is correct
19 Correct 1 ms 8528 KB Output is correct
20 Correct 1 ms 8528 KB Output is correct
21 Correct 2 ms 8528 KB Output is correct
22 Correct 1 ms 8528 KB Output is correct
23 Correct 2 ms 8528 KB Output is correct
24 Correct 2 ms 8528 KB Output is correct
25 Correct 2 ms 8528 KB Output is correct
26 Correct 1 ms 8528 KB Output is correct
27 Correct 2 ms 8528 KB Output is correct
28 Correct 1 ms 8636 KB Output is correct
29 Correct 2 ms 8528 KB Output is correct
30 Correct 2 ms 8528 KB Output is correct
31 Correct 2 ms 8640 KB Output is correct
32 Correct 2 ms 8784 KB Output is correct
33 Correct 2 ms 8640 KB Output is correct
34 Correct 2 ms 8644 KB Output is correct
35 Correct 2 ms 8784 KB Output is correct
36 Correct 1 ms 8528 KB Output is correct
37 Correct 2 ms 8528 KB Output is correct
38 Correct 2 ms 8640 KB Output is correct
39 Correct 1 ms 8528 KB Output is correct
40 Correct 1 ms 8640 KB Output is correct
41 Correct 2 ms 8696 KB Output is correct
42 Correct 2 ms 8528 KB Output is correct
43 Correct 2 ms 8528 KB Output is correct
44 Correct 1 ms 8528 KB Output is correct
45 Correct 1 ms 8528 KB Output is correct
46 Correct 1 ms 8528 KB Output is correct
47 Correct 2 ms 8528 KB Output is correct
48 Correct 2 ms 8528 KB Output is correct
49 Correct 1 ms 8528 KB Output is correct
50 Correct 1 ms 8528 KB Output is correct
51 Correct 1 ms 8528 KB Output is correct
52 Correct 1 ms 8664 KB Output is correct
53 Correct 2 ms 8528 KB Output is correct
54 Correct 1 ms 8528 KB Output is correct
55 Correct 2 ms 8528 KB Output is correct
56 Correct 1 ms 8528 KB Output is correct
57 Correct 1 ms 8528 KB Output is correct
58 Correct 1 ms 8528 KB Output is correct
59 Correct 2 ms 8696 KB Output is correct
60 Correct 1 ms 8528 KB Output is correct
61 Correct 1 ms 8528 KB Output is correct
62 Correct 1 ms 8528 KB Output is correct
63 Correct 2 ms 8784 KB Output is correct
64 Correct 4 ms 8784 KB Output is correct
65 Correct 3 ms 8784 KB Output is correct
66 Correct 3 ms 8784 KB Output is correct
67 Correct 3 ms 8648 KB Output is correct
68 Correct 3 ms 8952 KB Output is correct
69 Correct 3 ms 8784 KB Output is correct
70 Correct 3 ms 8784 KB Output is correct
71 Correct 4 ms 8784 KB Output is correct
72 Correct 5 ms 8916 KB Output is correct
73 Correct 4 ms 9040 KB Output is correct
74 Correct 4 ms 8784 KB Output is correct
75 Correct 2 ms 8528 KB Output is correct
76 Correct 3 ms 8784 KB Output is correct
77 Correct 3 ms 8844 KB Output is correct
78 Correct 3 ms 8784 KB Output is correct
79 Correct 3 ms 8784 KB Output is correct
80 Correct 4 ms 8784 KB Output is correct
81 Correct 3 ms 8784 KB Output is correct
82 Correct 3 ms 8832 KB Output is correct
83 Correct 3 ms 8784 KB Output is correct
84 Correct 3 ms 8784 KB Output is correct
85 Correct 4 ms 8732 KB Output is correct
86 Correct 3 ms 8648 KB Output is correct
87 Correct 3 ms 8652 KB Output is correct
88 Correct 4 ms 8784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 1 ms 8528 KB Output is correct
3 Correct 2 ms 8528 KB Output is correct
4 Correct 2 ms 8528 KB Output is correct
5 Correct 2 ms 8528 KB Output is correct
6 Correct 1 ms 8528 KB Output is correct
7 Correct 2 ms 8528 KB Output is correct
8 Correct 2 ms 8528 KB Output is correct
9 Correct 2 ms 8528 KB Output is correct
10 Correct 2 ms 8696 KB Output is correct
11 Correct 2 ms 8528 KB Output is correct
12 Correct 2 ms 8528 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 1 ms 8528 KB Output is correct
15 Correct 2 ms 8528 KB Output is correct
16 Correct 2 ms 8528 KB Output is correct
17 Correct 2 ms 8640 KB Output is correct
18 Correct 2 ms 8528 KB Output is correct
19 Correct 1 ms 8528 KB Output is correct
20 Correct 1 ms 8528 KB Output is correct
21 Correct 2 ms 8528 KB Output is correct
22 Correct 1 ms 8528 KB Output is correct
23 Correct 2 ms 8528 KB Output is correct
24 Correct 2 ms 8528 KB Output is correct
25 Correct 2 ms 8528 KB Output is correct
26 Correct 1 ms 8528 KB Output is correct
27 Correct 2 ms 8528 KB Output is correct
28 Correct 1 ms 8636 KB Output is correct
29 Correct 2 ms 8528 KB Output is correct
30 Correct 2 ms 8528 KB Output is correct
31 Correct 2 ms 8640 KB Output is correct
32 Correct 2 ms 8784 KB Output is correct
33 Correct 2 ms 8640 KB Output is correct
34 Correct 2 ms 8644 KB Output is correct
35 Correct 2 ms 8784 KB Output is correct
36 Correct 1 ms 8528 KB Output is correct
37 Correct 2 ms 8528 KB Output is correct
38 Correct 2 ms 8640 KB Output is correct
39 Correct 1 ms 8528 KB Output is correct
40 Correct 1 ms 8640 KB Output is correct
41 Correct 2 ms 8696 KB Output is correct
42 Correct 2 ms 8528 KB Output is correct
43 Correct 2 ms 8528 KB Output is correct
44 Correct 1 ms 8528 KB Output is correct
45 Correct 1 ms 8528 KB Output is correct
46 Correct 1 ms 8528 KB Output is correct
47 Correct 2 ms 8528 KB Output is correct
48 Correct 2 ms 8528 KB Output is correct
49 Correct 1 ms 8528 KB Output is correct
50 Correct 1 ms 8528 KB Output is correct
51 Correct 1 ms 8528 KB Output is correct
52 Correct 1 ms 8664 KB Output is correct
53 Correct 2 ms 8528 KB Output is correct
54 Correct 1 ms 8528 KB Output is correct
55 Correct 2 ms 8528 KB Output is correct
56 Correct 1 ms 8528 KB Output is correct
57 Correct 1 ms 8528 KB Output is correct
58 Correct 1 ms 8528 KB Output is correct
59 Correct 2 ms 8696 KB Output is correct
60 Correct 1 ms 8528 KB Output is correct
61 Correct 1 ms 8528 KB Output is correct
62 Correct 1 ms 8528 KB Output is correct
63 Correct 2 ms 8784 KB Output is correct
64 Correct 4 ms 8784 KB Output is correct
65 Correct 3 ms 8784 KB Output is correct
66 Correct 3 ms 8784 KB Output is correct
67 Correct 3 ms 8648 KB Output is correct
68 Correct 3 ms 8952 KB Output is correct
69 Correct 3 ms 8784 KB Output is correct
70 Correct 3 ms 8784 KB Output is correct
71 Correct 4 ms 8784 KB Output is correct
72 Correct 5 ms 8916 KB Output is correct
73 Correct 4 ms 9040 KB Output is correct
74 Correct 4 ms 8784 KB Output is correct
75 Correct 2 ms 8528 KB Output is correct
76 Correct 3 ms 8784 KB Output is correct
77 Correct 3 ms 8844 KB Output is correct
78 Correct 3 ms 8784 KB Output is correct
79 Correct 3 ms 8784 KB Output is correct
80 Correct 4 ms 8784 KB Output is correct
81 Correct 3 ms 8784 KB Output is correct
82 Correct 3 ms 8832 KB Output is correct
83 Correct 3 ms 8784 KB Output is correct
84 Correct 3 ms 8784 KB Output is correct
85 Correct 4 ms 8732 KB Output is correct
86 Correct 3 ms 8648 KB Output is correct
87 Correct 3 ms 8652 KB Output is correct
88 Correct 4 ms 8784 KB Output is correct
89 Correct 125 ms 28004 KB Output is correct
90 Correct 123 ms 28100 KB Output is correct
91 Correct 108 ms 28004 KB Output is correct
92 Correct 190 ms 30568 KB Output is correct
93 Correct 202 ms 30580 KB Output is correct
94 Correct 207 ms 30560 KB Output is correct
95 Correct 138 ms 28516 KB Output is correct
96 Correct 156 ms 29792 KB Output is correct
97 Correct 205 ms 31700 KB Output is correct
98 Correct 211 ms 28372 KB Output is correct
99 Correct 427 ms 39120 KB Output is correct
100 Correct 434 ms 39632 KB Output is correct
101 Correct 434 ms 38864 KB Output is correct
102 Correct 411 ms 36464 KB Output is correct
103 Correct 31 ms 21840 KB Output is correct
104 Correct 16 ms 9320 KB Output is correct
105 Correct 16 ms 9492 KB Output is correct
106 Correct 16 ms 9320 KB Output is correct
107 Correct 12 ms 9132 KB Output is correct
108 Correct 16 ms 9388 KB Output is correct
109 Correct 93 ms 21832 KB Output is correct