답안 #943736

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
943736 2024-03-11T18:59:03 Z Pannda Fish 2 (JOI22_fish2) C++17
60 / 100
4000 ms 21312 KB
#include <bits/stdc++.h>
using namespace std;

struct Paint {
    struct Node {
        int mn, cnt;
        int lazy = 0;
        void add(int delta) {
            mn += delta;
            lazy += delta;
        }
        void merge(Node a, Node b) {
            mn = min(a.mn, b.mn);
            cnt = 0;
            if (a.mn == mn) cnt += a.cnt;
            if (b.mn == mn) cnt += b.cnt;
        }
    };

    int n;
    vector<Node> nodes;

    Paint(int n) : n(n), nodes(4 * n) {
        auto dfs = [&](auto self, int idx, int l, int r) -> void {
            if (l + 1 == r) {
                nodes[idx].mn = 0;
                nodes[idx].cnt = 1;
            } else {
                int m = (l + r) >> 1;
                self(self, 2 * idx + 1, l, m);
                self(self, 2 * idx + 2, m, r);
                nodes[idx].merge(nodes[2 * idx + 1], nodes[2 * idx + 2]);
            }
        };
        dfs(dfs, 0, 0, n);
    }

    void down(int idx) {
        nodes[2 * idx + 1].add(nodes[idx].lazy);
        nodes[2 * idx + 2].add(nodes[idx].lazy);
        nodes[idx].lazy = 0;
    }

    void add(int ql, int qr, int delta) {
        auto dfs = [&](auto self, int idx, int l, int r) -> void {
            if (r <= ql || qr <= l) return;
            if (ql <= l && r <= qr) return nodes[idx].add(delta);
            down(idx);
            int m = (l + r) >> 1;
            self(self, 2 * idx + 1, l, m);
            self(self, 2 * idx + 2, m, r);
            nodes[idx].merge(nodes[2 * idx + 1], nodes[2 * idx + 2]);
        };
        dfs(dfs, 0, 0, n);
    }

    int countZero(int ql, int qr) {
        int fetch = 0;
        auto dfs = [&](auto self, int idx, int l, int r) -> void {
            if (r <= ql || qr <= l) return;
            if (ql <= l && r <= qr) {
                fetch += nodes[idx].mn == 0 ? nodes[idx].cnt : 0;
                return;
            }
            down(idx);
            int m = (l + r) >> 1;
            self(self, 2 * idx + 1, l, m);
            self(self, 2 * idx + 2, m, r);
        };
        dfs(dfs, 0, 0, n);
        return fetch;
    }
};

struct SegmentWalk {
    int n;
    vector<int> mx;

    SegmentWalk(int n) : n(n), mx(4 * n, 0) {}

    void set(int i, int val) {
        auto dfs = [&](auto self, int idx, int l, int r) -> void {
            if (l + 1 == r) {
                mx[idx] = val;
            } else {
                int m = (l + r) >> 1;
                if (i < m) self(self, 2 * idx + 1, l, m);
                else self(self, 2 * idx + 2, m, r);
                mx[idx] = max(mx[2 * idx + 1], mx[2 * idx + 2]);
            }
        };
        dfs(dfs, 0, 0, n);
    }

    int walk(int ql, int qr, long long bound, bool request_leftmost) { // in [ql, qr), returns the leftmost (rightmost) position with value > 'bound'
        auto dfs = [&](auto self, int idx, int l, int r) -> int {
            if (r <= ql || qr <= l || mx[idx] <= bound) return -1;
            if (ql <= l && r <= qr) {
                while (l + 1 < r) {
                    int m = (l + r) >> 1;
                    if (request_leftmost) {
                        if (mx[2 * idx + 1] > bound) idx = 2 * idx + 1, r = m;
                        else idx = 2 * idx + 2, l = m;
                    } else {
                        if (mx[2 * idx + 2] > bound) idx = 2 * idx + 2, l = m;
                        else idx = 2 * idx + 1, r = m;
                    }
                }
                return l;
            }
            int m = (l + r) >> 1;
            if (request_leftmost) {
                int get = self(self, 2 * idx + 1, l, m);
                if (get != -1) return get;
                return self(self, 2 * idx + 2, m, r);
            } else {
                int get = self(self, 2 * idx + 2, m, r);
                if (get != -1) return get;
                return self(self, 2 * idx + 1, l, m);
            }
        };
        return dfs(dfs, 0, 0, n);
    }
};

struct Fenwick {
    int n;
    vector<long long> bit;

    Fenwick(int n) : n(n), bit(n + 1, 0) {}

    void add(int i, int delta) {
        for (i++; i <= n; i += i & -i) bit[i] += delta;
    }

    long long sum(int i) {
        long long res = 0;
        for (; i > 0; i -= i & -i) res += bit[i];
        return res;
    }

    long long sum(int l, int r) {
        return sum(r) - sum(l);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
//    freopen("inp.inp", "r", stdin);
//    freopen("out.out", "w", stdout);

    int n;
    cin >> n;

    vector<int> a(n);
    Fenwick fen(n);
    SegmentWalk segwalk(n);

    for (int i = 0; i < n; i++) {
        cin >> a[i];
        fen.add(i, a[i]);
        segwalk.set(i, a[i]);
    }

    auto findSaturatedInterval = [&](int ql, int qr, int &l, int &r, long long &sum) -> void { // O(log^2), find the tightest saturated interval (or [l, r)) containing the initial interval [l, r)
        while (ql < l || r < qr) {
            long long old_sum = sum;
            if (ql < l) {
                int p = segwalk.walk(ql, l, sum, false);
                if (p == -1) {
                    sum += fen.sum(ql, l);
                    l = ql;
                } else {
                    sum += fen.sum(p + 1, l);
                    l = p + 1;
                    if (sum >= a[p]) {
                        sum += a[p];
                        l = p;
                    }
                }
            }
            if (r < qr) {
                int p = segwalk.walk(r, qr, sum, true);
                if (p == -1) {
                    sum += fen.sum(r, qr);
                    r = qr;
                } else {
                    sum += fen.sum(r, p);
                    r = p;
                    if (sum >= a[p]) {
                        sum += a[p];
                        r = p + 1;
                    }
                }
            }
            if (sum == old_sum) break;
        }
    };

    auto findAllSaturatedIntervals = [&](int ql, int qr, int i) -> vector<array<int, 2>> { // O(log^2), find all saturated intervals containing i
        vector<array<int, 2>> res;
        int l = i, r = i + 1;
        long long sum = a[i];
        while (true) {
            findSaturatedInterval(ql, qr, l, r, sum);
            if (l == ql && r == qr) break;
            res.push_back({l, r});
            if (l == ql || (r < qr && a[r] <= a[l - 1])) {
                sum += a[r];
                r = r + 1;
            } else {
                sum += a[l - 1];
                l = l - 1;
            }
        }
        return res;
    };

    Paint paint(n);
    SegmentWalk wow(n);
    vector<set<int>> why(n);

    auto erase = [&](int ql, int qr) -> vector<array<int, 2>> { // erases all intervals containing [ql, qr), return the set of deleted intervals
        vector<array<int, 2>> res;
        if (ql < 0 || qr > n) return res;
        while (true) {
            int l = wow.walk(0, ql + 1, qr - 1, true);
            if (l == -1) break;
            int r = *why[l].rbegin();
            res.push_back({l, r});
            paint.add(l, r, -1);
            why[l].erase(r);
            if (why[l].empty()) wow.set(l, 0);
            else wow.set(l, *why[l].rbegin());
        }
        return res;
    };

    auto insert = [&](int l, int r) -> void {
        if (why[l].count(r)) return;
        why[l].insert(r);
        wow.set(l, *why[l].rbegin());
        paint.add(l, r, +1);
    };

    for (int i = 0; i < n; i++) {
        int l = i, r = i + 1;
        long long sum = a[i];
        findSaturatedInterval(0, n, l, r, sum);
        if (l != 0 || r != n) insert(l, r);
    }

    int q;
    cin >> q;
    while (q--) {
        int type;
        cin >> type;
        if (type == 1) {
            int i, x;
            cin >> i >> x;
            i--;
            auto subinsert = [&](int i) -> void {
                if (i < 0 || i >= n) return;
                for (auto [l, r] : findAllSaturatedIntervals(0, n, i)) insert(l, r);
            };
            erase(i - 1, i);
            erase(i, i + 1);
            erase(i + 1, i + 2);
            fen.add(i, -a[i] + x);
            a[i] = x;
            segwalk.set(i, x);
            subinsert(i - 1);
            subinsert(i);
            subinsert(i + 1);
        }
        if (type == 2) {
            int l, r;
            cin >> l >> r;
            l--;
            vector<array<int, 2>> erase0 = erase(l - 1, l + 1);
            vector<array<int, 2>> erase1 = erase(r - 1, r + 1);
            vector<array<int, 2>> erase2 = erase(l, r);
            vector<array<int, 2>> insert0 = findAllSaturatedIntervals(l, r, l);
            vector<array<int, 2>> insert1 = findAllSaturatedIntervals(l, r, r - 1);
            for (auto [l, r] : insert0) paint.add(l, r, +1);
            for (auto [l, r] : insert1) paint.add(l, r, +1);
            cout << paint.countZero(l, r) << '\n';
            for (auto [l, r] : insert0) paint.add(l, r, -1);
            for (auto [l, r] : insert1) paint.add(l, r, -1);
            for (auto [l, r] : erase0) insert(l, r);
            for (auto [l, r] : erase1) insert(l, r);
            for (auto [l, r] : erase2) insert(l, r);
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 7 ms 348 KB Output is correct
6 Correct 6 ms 348 KB Output is correct
7 Correct 9 ms 348 KB Output is correct
8 Correct 10 ms 344 KB Output is correct
9 Correct 6 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 4 ms 348 KB Output is correct
13 Correct 5 ms 348 KB Output is correct
14 Correct 2 ms 348 KB Output is correct
15 Correct 4 ms 348 KB Output is correct
16 Correct 4 ms 756 KB Output is correct
17 Correct 3 ms 348 KB Output is correct
18 Correct 4 ms 460 KB Output is correct
19 Correct 3 ms 344 KB Output is correct
20 Correct 2 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 108 ms 18060 KB Output is correct
3 Correct 110 ms 17488 KB Output is correct
4 Correct 115 ms 18124 KB Output is correct
5 Correct 109 ms 18000 KB Output is correct
6 Correct 70 ms 15956 KB Output is correct
7 Correct 119 ms 15236 KB Output is correct
8 Correct 78 ms 16012 KB Output is correct
9 Correct 115 ms 15324 KB Output is correct
10 Correct 100 ms 15912 KB Output is correct
11 Correct 92 ms 15696 KB Output is correct
12 Correct 89 ms 15952 KB Output is correct
13 Correct 88 ms 15956 KB Output is correct
14 Correct 83 ms 17240 KB Output is correct
15 Correct 88 ms 17236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 7 ms 348 KB Output is correct
6 Correct 6 ms 348 KB Output is correct
7 Correct 9 ms 348 KB Output is correct
8 Correct 10 ms 344 KB Output is correct
9 Correct 6 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 4 ms 348 KB Output is correct
13 Correct 5 ms 348 KB Output is correct
14 Correct 2 ms 348 KB Output is correct
15 Correct 4 ms 348 KB Output is correct
16 Correct 4 ms 756 KB Output is correct
17 Correct 3 ms 348 KB Output is correct
18 Correct 4 ms 460 KB Output is correct
19 Correct 3 ms 344 KB Output is correct
20 Correct 2 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 108 ms 18060 KB Output is correct
23 Correct 110 ms 17488 KB Output is correct
24 Correct 115 ms 18124 KB Output is correct
25 Correct 109 ms 18000 KB Output is correct
26 Correct 70 ms 15956 KB Output is correct
27 Correct 119 ms 15236 KB Output is correct
28 Correct 78 ms 16012 KB Output is correct
29 Correct 115 ms 15324 KB Output is correct
30 Correct 100 ms 15912 KB Output is correct
31 Correct 92 ms 15696 KB Output is correct
32 Correct 89 ms 15952 KB Output is correct
33 Correct 88 ms 15956 KB Output is correct
34 Correct 83 ms 17240 KB Output is correct
35 Correct 88 ms 17236 KB Output is correct
36 Correct 146 ms 19088 KB Output is correct
37 Correct 132 ms 18124 KB Output is correct
38 Correct 127 ms 17488 KB Output is correct
39 Correct 157 ms 19100 KB Output is correct
40 Correct 130 ms 17512 KB Output is correct
41 Correct 74 ms 16976 KB Output is correct
42 Correct 73 ms 17012 KB Output is correct
43 Correct 135 ms 15444 KB Output is correct
44 Correct 138 ms 15496 KB Output is correct
45 Correct 126 ms 16884 KB Output is correct
46 Correct 124 ms 16436 KB Output is correct
47 Correct 114 ms 15396 KB Output is correct
48 Correct 98 ms 16288 KB Output is correct
49 Correct 103 ms 16016 KB Output is correct
50 Correct 88 ms 17928 KB Output is correct
51 Correct 92 ms 17680 KB Output is correct
52 Correct 91 ms 17748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 108 ms 18060 KB Output is correct
3 Correct 110 ms 17488 KB Output is correct
4 Correct 115 ms 18124 KB Output is correct
5 Correct 109 ms 18000 KB Output is correct
6 Correct 70 ms 15956 KB Output is correct
7 Correct 119 ms 15236 KB Output is correct
8 Correct 78 ms 16012 KB Output is correct
9 Correct 115 ms 15324 KB Output is correct
10 Correct 100 ms 15912 KB Output is correct
11 Correct 92 ms 15696 KB Output is correct
12 Correct 89 ms 15952 KB Output is correct
13 Correct 88 ms 15956 KB Output is correct
14 Correct 83 ms 17240 KB Output is correct
15 Correct 88 ms 17236 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 2383 ms 18056 KB Output is correct
18 Correct 2759 ms 21152 KB Output is correct
19 Correct 2439 ms 19912 KB Output is correct
20 Correct 3620 ms 20064 KB Output is correct
21 Correct 2273 ms 19696 KB Output is correct
22 Correct 2769 ms 21312 KB Output is correct
23 Correct 2457 ms 19788 KB Output is correct
24 Correct 3636 ms 20532 KB Output is correct
25 Correct 2402 ms 19852 KB Output is correct
26 Correct 3635 ms 20208 KB Output is correct
27 Correct 379 ms 19028 KB Output is correct
28 Correct 373 ms 18956 KB Output is correct
29 Correct 364 ms 19028 KB Output is correct
30 Correct 2091 ms 17520 KB Output is correct
31 Correct 2243 ms 17188 KB Output is correct
32 Execution timed out 4042 ms 17716 KB Time limit exceeded
33 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 108 ms 18060 KB Output is correct
3 Correct 110 ms 17488 KB Output is correct
4 Correct 115 ms 18124 KB Output is correct
5 Correct 109 ms 18000 KB Output is correct
6 Correct 70 ms 15956 KB Output is correct
7 Correct 119 ms 15236 KB Output is correct
8 Correct 78 ms 16012 KB Output is correct
9 Correct 115 ms 15324 KB Output is correct
10 Correct 100 ms 15912 KB Output is correct
11 Correct 92 ms 15696 KB Output is correct
12 Correct 89 ms 15952 KB Output is correct
13 Correct 88 ms 15956 KB Output is correct
14 Correct 83 ms 17240 KB Output is correct
15 Correct 88 ms 17236 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1919 ms 18484 KB Output is correct
18 Correct 999 ms 18296 KB Output is correct
19 Correct 1782 ms 18040 KB Output is correct
20 Correct 944 ms 19044 KB Output is correct
21 Correct 1950 ms 18432 KB Output is correct
22 Correct 1076 ms 18260 KB Output is correct
23 Correct 1826 ms 18004 KB Output is correct
24 Correct 959 ms 18256 KB Output is correct
25 Correct 1626 ms 17964 KB Output is correct
26 Correct 382 ms 16208 KB Output is correct
27 Correct 457 ms 16208 KB Output is correct
28 Correct 752 ms 17232 KB Output is correct
29 Correct 450 ms 16520 KB Output is correct
30 Correct 473 ms 16080 KB Output is correct
31 Correct 836 ms 17000 KB Output is correct
32 Correct 996 ms 17576 KB Output is correct
33 Correct 733 ms 15700 KB Output is correct
34 Correct 909 ms 17864 KB Output is correct
35 Correct 758 ms 16168 KB Output is correct
36 Correct 819 ms 17492 KB Output is correct
37 Correct 857 ms 16976 KB Output is correct
38 Correct 692 ms 16956 KB Output is correct
39 Correct 513 ms 17484 KB Output is correct
40 Correct 386 ms 17492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 7 ms 348 KB Output is correct
6 Correct 6 ms 348 KB Output is correct
7 Correct 9 ms 348 KB Output is correct
8 Correct 10 ms 344 KB Output is correct
9 Correct 6 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 4 ms 348 KB Output is correct
13 Correct 5 ms 348 KB Output is correct
14 Correct 2 ms 348 KB Output is correct
15 Correct 4 ms 348 KB Output is correct
16 Correct 4 ms 756 KB Output is correct
17 Correct 3 ms 348 KB Output is correct
18 Correct 4 ms 460 KB Output is correct
19 Correct 3 ms 344 KB Output is correct
20 Correct 2 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 108 ms 18060 KB Output is correct
23 Correct 110 ms 17488 KB Output is correct
24 Correct 115 ms 18124 KB Output is correct
25 Correct 109 ms 18000 KB Output is correct
26 Correct 70 ms 15956 KB Output is correct
27 Correct 119 ms 15236 KB Output is correct
28 Correct 78 ms 16012 KB Output is correct
29 Correct 115 ms 15324 KB Output is correct
30 Correct 100 ms 15912 KB Output is correct
31 Correct 92 ms 15696 KB Output is correct
32 Correct 89 ms 15952 KB Output is correct
33 Correct 88 ms 15956 KB Output is correct
34 Correct 83 ms 17240 KB Output is correct
35 Correct 88 ms 17236 KB Output is correct
36 Correct 146 ms 19088 KB Output is correct
37 Correct 132 ms 18124 KB Output is correct
38 Correct 127 ms 17488 KB Output is correct
39 Correct 157 ms 19100 KB Output is correct
40 Correct 130 ms 17512 KB Output is correct
41 Correct 74 ms 16976 KB Output is correct
42 Correct 73 ms 17012 KB Output is correct
43 Correct 135 ms 15444 KB Output is correct
44 Correct 138 ms 15496 KB Output is correct
45 Correct 126 ms 16884 KB Output is correct
46 Correct 124 ms 16436 KB Output is correct
47 Correct 114 ms 15396 KB Output is correct
48 Correct 98 ms 16288 KB Output is correct
49 Correct 103 ms 16016 KB Output is correct
50 Correct 88 ms 17928 KB Output is correct
51 Correct 92 ms 17680 KB Output is correct
52 Correct 91 ms 17748 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 2383 ms 18056 KB Output is correct
55 Correct 2759 ms 21152 KB Output is correct
56 Correct 2439 ms 19912 KB Output is correct
57 Correct 3620 ms 20064 KB Output is correct
58 Correct 2273 ms 19696 KB Output is correct
59 Correct 2769 ms 21312 KB Output is correct
60 Correct 2457 ms 19788 KB Output is correct
61 Correct 3636 ms 20532 KB Output is correct
62 Correct 2402 ms 19852 KB Output is correct
63 Correct 3635 ms 20208 KB Output is correct
64 Correct 379 ms 19028 KB Output is correct
65 Correct 373 ms 18956 KB Output is correct
66 Correct 364 ms 19028 KB Output is correct
67 Correct 2091 ms 17520 KB Output is correct
68 Correct 2243 ms 17188 KB Output is correct
69 Execution timed out 4042 ms 17716 KB Time limit exceeded
70 Halted 0 ms 0 KB -