Submission #861036

# Submission time Handle Problem Language Result Execution time Memory
861036 2023-10-15T07:26:19 Z Alfraganus Bigger segments (IZhO19_segments) C++14
100 / 100
166 ms 35408 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define endl '\n'
#define fs first
#define ss second
#define str string
#define all(a) a.begin(), a.end()
#define print(a)          \
    for (auto x : a)      \
        cout << x << ' '; \
    cout << endl;
#define each(x, a) for (auto x : a)

struct SegTree {
    int size = 1;
    vector<int> mins, add;

    SegTree (int n){
        while(size < n)
            size <<= 1;
        mins.resize(size << 1, 1e18);
        add.resize(size << 1);
    }

    void set(int i, int v, int x, int lx, int rx){
        if(rx == lx + 1){
            mins[x] = min(mins[x], v);
            return;
        }
        int m = (lx + rx) >> 1;
        if(i < m)
            set(i, v, (x << 1) + 1, lx, m);
        else
            set(i, v, (x << 1) + 2, m, rx);
        mins[x] = min(mins[(x << 1) + 1] - add[(x << 1) + 1], mins[(x << 1) + 2] - add[(x << 1) + 2]);
    }

    void set(int i, int v)
    {
        set(i, v, 0, 0, size);
    }

    void push(int r, int k, int x, int lx, int rx){
        if(rx <= r){
            add[x] += k;
            return;
        }
        if(lx >= r){
            return;
        }
        int m = (lx + rx) >> 1;
        push(r, k, (x << 1) + 1, lx, m);
        push(r, k, (x << 1) + 2, m, rx);
        mins[x] = min(mins[(x << 1) + 1] - add[(x << 1) + 1], mins[(x << 1) + 2] - add[(x << 1) + 2]);
    }

    void push(int r, int k){
        push(r, k, 0, 0, size);
    }

    int ans(int x, int lx, int rx, int s = 0){
        if(rx == lx + 1){
            return lx;
        }
        s += add[x];
        int m = (lx + rx) >> 1;
        if(mins[(x << 1) + 2] - s <= 0)
            return ans((x << 1) + 2, m, rx, s);
        return ans((x << 1) + 1, lx, m, s);
    }

    int ans(){
        return ans(0, 0, size);
    }
};

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; i ++)
        cin >> a[i];
    vector<int> pre(n + 1);
    for(int i = 0; i < n; i ++)
        pre[i + 1] = pre[i] + a[i];
    SegTree s(n + 1);
    s.set(0, 0);
    vector<int> dp(n + 1), sum(n + 1);
    for(int i = 0; i < n; i ++){
        s.push(i + 1, a[i]);
        int j = s.ans();
        dp[i + 1] = dp[j] + 1;
        sum[i + 1] = pre[i + 1] - pre[j];
        s.set(i + 1, sum[i + 1]);
    }
    cout << dp[n];
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
        cout << endl;
    }
}
# 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 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 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 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 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 0 ms 348 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 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 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 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 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 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 0 ms 348 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 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 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 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 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 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 1 ms 508 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 600 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 600 KB Output is correct
44 Correct 1 ms 604 KB Output is correct
45 Correct 1 ms 604 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 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 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 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 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 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 1 ms 508 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 600 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 600 KB Output is correct
44 Correct 1 ms 604 KB Output is correct
45 Correct 1 ms 604 KB Output is correct
46 Correct 30 ms 8028 KB Output is correct
47 Correct 29 ms 8076 KB Output is correct
48 Correct 31 ms 8280 KB Output is correct
49 Correct 31 ms 8284 KB Output is correct
50 Correct 32 ms 8464 KB Output is correct
51 Correct 32 ms 8280 KB Output is correct
52 Correct 15 ms 4584 KB Output is correct
53 Correct 9 ms 2652 KB Output is correct
54 Correct 27 ms 7760 KB Output is correct
55 Correct 31 ms 8280 KB Output is correct
56 Correct 28 ms 8024 KB Output is correct
57 Correct 28 ms 8028 KB Output is correct
58 Correct 29 ms 8168 KB Output is correct
59 Correct 32 ms 8168 KB Output is correct
60 Correct 29 ms 8024 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 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 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 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 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 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 1 ms 508 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 600 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 600 KB Output is correct
44 Correct 1 ms 604 KB Output is correct
45 Correct 1 ms 604 KB Output is correct
46 Correct 30 ms 8028 KB Output is correct
47 Correct 29 ms 8076 KB Output is correct
48 Correct 31 ms 8280 KB Output is correct
49 Correct 31 ms 8284 KB Output is correct
50 Correct 32 ms 8464 KB Output is correct
51 Correct 32 ms 8280 KB Output is correct
52 Correct 15 ms 4584 KB Output is correct
53 Correct 9 ms 2652 KB Output is correct
54 Correct 27 ms 7760 KB Output is correct
55 Correct 31 ms 8280 KB Output is correct
56 Correct 28 ms 8024 KB Output is correct
57 Correct 28 ms 8028 KB Output is correct
58 Correct 29 ms 8168 KB Output is correct
59 Correct 32 ms 8168 KB Output is correct
60 Correct 29 ms 8024 KB Output is correct
61 Correct 138 ms 33816 KB Output is correct
62 Correct 163 ms 33616 KB Output is correct
63 Correct 144 ms 34128 KB Output is correct
64 Correct 160 ms 35092 KB Output is correct
65 Correct 158 ms 35188 KB Output is correct
66 Correct 159 ms 35152 KB Output is correct
67 Correct 127 ms 31580 KB Output is correct
68 Correct 63 ms 15952 KB Output is correct
69 Correct 141 ms 33296 KB Output is correct
70 Correct 166 ms 35408 KB Output is correct
71 Correct 146 ms 33620 KB Output is correct
72 Correct 147 ms 34068 KB Output is correct
73 Correct 148 ms 34320 KB Output is correct
74 Correct 152 ms 34128 KB Output is correct
75 Correct 152 ms 34316 KB Output is correct