Submission #715929

# Submission time Handle Problem Language Result Execution time Memory
715929 2023-03-28T12:45:29 Z tengiz05 Rabbit Carrot (LMIO19_triusis) C++17
100 / 100
314 ms 21504 KB
#include <bits/stdc++.h>

using i64 = long long;

constexpr int inf = 1001111111;

struct SegmentTree {
    int n;
    std::vector<i64> mn, mn2, lazy;
    SegmentTree(int n) : n(n), mn(4 * n, -inf), mn2(4 * n, inf), lazy(4 * n) {}
    void push(int p) {
        mn[2 * p] += lazy[p];
        mn[2 * p + 1] += lazy[p];
        mn2[2 * p] += lazy[p];
        mn2[2 * p + 1] += lazy[p];
        mn[2 * p] = std::max(mn[2 * p], mn[p]);
        mn[2 * p + 1] = std::max(mn[2 * p + 1], mn[p]);
        lazy[2 * p] += lazy[p];
        lazy[2 * p + 1] += lazy[p];
        lazy[p] = 0;
    }
    void pull(int p) {
        mn[p] = std::min(mn[2 * p], mn[2 * p + 1]);
        if (mn[2 * p] == mn[2 * p + 1]) {
            mn2[p] = std::min(mn2[2 * p], mn2[2 * p + 1]);
        } else if (mn[2 * p] == mn[p]) {
            mn2[p] = std::min(mn2[2 * p], mn[2 * p + 1]);
        } else {
            mn2[p] = std::min(mn[2 * p], mn2[2 * p + 1]);
        }
    }
    void modify(int p, int l, int r, int x, int y) {
        if (l == r - 1) {
            mn[p] = y;
            mn2[p] = inf;
        } else {
            int m = (l + r) / 2;
            push(p);
            if (x < m) {
                modify(2 * p, l, m, x, y);
            } else {
                modify(2 * p + 1, m, r, x, y);
            }
            pull(p);
        }
    }
    void modify(int x, int y) {
        modify(1, 0, n, x, y);
    }
    void chmax(int p, int l, int r, int x, int y, int v) {
        if (y <= l || r <= x || v <= mn[p]) return;
        if (x <= l && r <= y && v < mn2[p]) { // could be here
            mn[p] = v;
            return;
        }
        int m = (l + r) / 2;
        push(p);
        chmax(2 * p, l, m, x, y, v);
        chmax(2 * p + 1, m, r, x, y, v);
        mn[p] = std::min(mn[2 * p], mn[2 * p + 1]);
        pull(p);
    }
    void chmax(int l, int r, int v) {
        chmax(1, 0, n, l, r, v);
    }
    int at(int p, int l, int r, int x) {
        if (l == r - 1) {
            return mn[p];
        } else {
            int m = (l + r) / 2;
            push(p);
            if (x < m) {
                return at(2 * p, l, m, x);
            } else {
                return at(2 * p + 1, m, r, x);
            }
        }
    }
    i64 at(int x) {
        return at(1, 0, n, x);
    }
    void add(int p, int l, int r, int x, int y, int v) {
        if (y <= l || r <= x) return;
        if (x <= l && r <= y) {
            lazy[p] += v;
            mn[p] += v;
            mn2[p] += v;
            return;
        }
        int m = (l + r) / 2;
        add(2 * p, l, m, x, y, v);
        add(2 * p + 1, m, r, x, y, v);
        pull(p);
    }
    void add(int l, int r, int v) {
        add(1, 0, n, l, r, v);
    }
    int find(int p, int l, int r, int v) {
        if (mn[p] >= v) {
            return l;
        }
        if (l == r - 1) return r;
        int m = (l + r) / 2;
        push(p);
        if (mn[2 * p + 1] >= v) {
            return find(2 * p, l, m, v);
        } else {
            return find(2 * p + 1, m, r, v);
        }
    }
    int find(int v) {
        return find(1, 0, n, v);
    }
};

void chmax(int &a, int b) {
    if (a < b) {
        a = b;
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n, M;
    std::cin >> n >> M;
    
    std::vector<int> a(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }
    
    SegmentTree s(n + 1);
    s.modify(n, 0);
    for (int i = 0; i < n; i++) {
        int hi = s.find(a[i] - M);
        s.add(0, n + 1, M);
        s.chmax(hi - 1, n, a[i]);
    }
    
    for (int i = 0; i <= n; i++) {
        if (s.at(i) >= 0) {
            std::cout << i << "\n";
            return 0;
        }
    }
    assert(false);
    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 3 ms 828 KB Output is correct
21 Correct 4 ms 840 KB Output is correct
22 Correct 5 ms 852 KB Output is correct
23 Correct 4 ms 840 KB Output is correct
24 Correct 5 ms 848 KB Output is correct
25 Correct 4 ms 852 KB Output is correct
26 Correct 4 ms 852 KB Output is correct
27 Correct 4 ms 844 KB Output is correct
28 Correct 4 ms 840 KB Output is correct
29 Correct 4 ms 836 KB Output is correct
30 Correct 4 ms 744 KB Output is correct
31 Correct 5 ms 728 KB Output is correct
32 Correct 4 ms 852 KB Output is correct
33 Correct 4 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 3 ms 828 KB Output is correct
5 Correct 4 ms 840 KB Output is correct
6 Correct 5 ms 852 KB Output is correct
7 Correct 4 ms 840 KB Output is correct
8 Correct 5 ms 848 KB Output is correct
9 Correct 4 ms 852 KB Output is correct
10 Correct 4 ms 852 KB Output is correct
11 Correct 4 ms 844 KB Output is correct
12 Correct 4 ms 840 KB Output is correct
13 Correct 4 ms 836 KB Output is correct
14 Correct 4 ms 744 KB Output is correct
15 Correct 5 ms 728 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 4 ms 724 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 324 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 6 ms 840 KB Output is correct
35 Correct 6 ms 864 KB Output is correct
36 Correct 8 ms 864 KB Output is correct
37 Correct 4 ms 732 KB Output is correct
38 Correct 5 ms 752 KB Output is correct
39 Correct 4 ms 856 KB Output is correct
40 Correct 4 ms 728 KB Output is correct
41 Correct 6 ms 848 KB Output is correct
42 Correct 5 ms 832 KB Output is correct
43 Correct 5 ms 736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 840 KB Output is correct
2 Correct 6 ms 864 KB Output is correct
3 Correct 8 ms 864 KB Output is correct
4 Correct 4 ms 732 KB Output is correct
5 Correct 5 ms 752 KB Output is correct
6 Correct 4 ms 856 KB Output is correct
7 Correct 4 ms 728 KB Output is correct
8 Correct 6 ms 848 KB Output is correct
9 Correct 5 ms 832 KB Output is correct
10 Correct 5 ms 736 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 3 ms 828 KB Output is correct
15 Correct 4 ms 840 KB Output is correct
16 Correct 5 ms 852 KB Output is correct
17 Correct 4 ms 840 KB Output is correct
18 Correct 5 ms 848 KB Output is correct
19 Correct 4 ms 852 KB Output is correct
20 Correct 4 ms 852 KB Output is correct
21 Correct 4 ms 844 KB Output is correct
22 Correct 4 ms 840 KB Output is correct
23 Correct 4 ms 836 KB Output is correct
24 Correct 4 ms 744 KB Output is correct
25 Correct 5 ms 728 KB Output is correct
26 Correct 4 ms 852 KB Output is correct
27 Correct 4 ms 724 KB Output is correct
28 Correct 1 ms 316 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 324 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 181 ms 19960 KB Output is correct
45 Correct 314 ms 20032 KB Output is correct
46 Correct 288 ms 21500 KB Output is correct
47 Correct 267 ms 21504 KB Output is correct
48 Correct 192 ms 20768 KB Output is correct
49 Correct 174 ms 21032 KB Output is correct
50 Correct 235 ms 21488 KB Output is correct
51 Correct 194 ms 21500 KB Output is correct
52 Correct 207 ms 20792 KB Output is correct
53 Correct 293 ms 21060 KB Output is correct
54 Correct 209 ms 20940 KB Output is correct
55 Correct 180 ms 20816 KB Output is correct