Submission #988806

# Submission time Handle Problem Language Result Execution time Memory
988806 2024-05-26T09:26:24 Z IdanRosen Election (BOI18_election) C++
100 / 100
429 ms 93688 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <ld, ld> pld;

struct seg_tree {
    struct node {
        int left = 0;
        int right = 0;

        int range_sz() { return right - left; }
        bool touches(int l, int r) { return left < r && l < right; }

        ll sum;
        ll min_prefix;
        ll min_suffix;
        ll local_ans;
    };

#define MAXN 500001
    node tree[4 * MAXN];

    template<typename T>
    void build(const vector <T> &arr) {
        build(arr, 1, 0, arr.size());
    }

    template<typename T>
    void build(const vector <T> &arr, int node, int curr_l, int curr_r) {
        tree[node].left = curr_l;
        tree[node].right = curr_r;

        if (curr_l == curr_r - 1) {
            tree[node].sum = arr[curr_l];
            tree[node].min_prefix = tree[node].min_suffix = min((ll) 0, arr[curr_l]);
            tree[node].local_ans = (arr[curr_l] == 1 ? 0 : 1);
            return;
        }

        int curr_m = curr_l + (curr_r - curr_l) / 2;

        build(arr, 2 * node, curr_l, curr_m);
        build(arr, 2 * node + 1, curr_m, curr_r);

        tree[node] = pull(tree[node * 2], tree[node * 2 + 1]);
        tree[node].left = curr_l;
        tree[node].right = curr_r;
    }


    node pull(node left, node right) {
        node ans;

        ans.sum = left.sum + right.sum;
        ans.min_prefix = min(left.min_prefix, left.sum + right.min_prefix);
        ans.min_suffix = min(right.min_suffix, left.min_suffix + right.sum);

        ans.local_ans = max(
                abs(left.min_prefix) + abs(right.min_suffix),
                max(
//                        abs(left.min_prefix) + max(0ll, abs(right.min_prefix) -
//                                                        (left.sum +
//                                                         abs(left.min_prefix))),
//                        abs(right.min_suffix) + max(0ll, abs(left.min_suffix) -
//                                                         (right.sum +
//                                                          abs(right.min_suffix)))
                    left.local_ans - right.sum,
                    - left.sum + right.local_ans
                )
        );

        return ans;
    }

    void push(int node) {
        if (tree[node].range_sz() == 0) return;
    }

    ll query(int query_l, int query_r) {
        return query < 0, node > (query_l, query_r, 1).local_ans;
    }

    template<int OPERATION, typename DATA>
    DATA query(int query_l, int query_r, int node) {
        push(node);

        if (!(tree[node].touches(query_l, query_r))) {return {0,0,0,0};}

        if (query_l <= tree[node].left && tree[node].right <= query_r) {
            return tree[node];
        }

        bool touches_left = tree[2 * node].touches(query_l, query_r);
        bool touches_right = tree[2 * node + 1].touches(query_l, query_r);

        if (!touches_right) return query < OPERATION, DATA > (query_l, query_r, 2 * node);
        else if (!touches_left)return query < OPERATION, DATA > (query_l, query_r, 2 * node + 1);
        else {
            DATA left(query < OPERATION, DATA > (query_l, query_r, 2 * node));
            DATA right(query < OPERATION, DATA > (query_l, query_r, 2 * node + 1));

            return pull(left, right);
        }
    }

};


seg_tree segtree;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    ll n;
    cin >> n;

    string str;
    cin >> str;

    vector <ll> arr(n);
    for (int i = 0; i < n; i++) arr[i] = (str[i] == 'C' ? 1 : -1);

    segtree.build(arr);

    ll q;
    cin >> q;
    while (q--) {
        ll a, b;
        cin >> a >> b;
        a--;

        cout << segtree.query(a, b) << "\n";
    }

}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 78684 KB Output is correct
2 Correct 12 ms 78544 KB Output is correct
3 Correct 13 ms 78684 KB Output is correct
4 Correct 14 ms 78756 KB Output is correct
5 Correct 12 ms 78684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 78684 KB Output is correct
2 Correct 12 ms 78544 KB Output is correct
3 Correct 13 ms 78684 KB Output is correct
4 Correct 14 ms 78756 KB Output is correct
5 Correct 12 ms 78684 KB Output is correct
6 Correct 52 ms 80208 KB Output is correct
7 Correct 50 ms 80464 KB Output is correct
8 Correct 54 ms 80464 KB Output is correct
9 Correct 44 ms 80592 KB Output is correct
10 Correct 56 ms 80452 KB Output is correct
11 Correct 52 ms 80516 KB Output is correct
12 Correct 52 ms 80532 KB Output is correct
13 Correct 53 ms 80724 KB Output is correct
14 Correct 51 ms 80720 KB Output is correct
15 Correct 52 ms 80468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 78684 KB Output is correct
2 Correct 12 ms 78544 KB Output is correct
3 Correct 13 ms 78684 KB Output is correct
4 Correct 14 ms 78756 KB Output is correct
5 Correct 12 ms 78684 KB Output is correct
6 Correct 52 ms 80208 KB Output is correct
7 Correct 50 ms 80464 KB Output is correct
8 Correct 54 ms 80464 KB Output is correct
9 Correct 44 ms 80592 KB Output is correct
10 Correct 56 ms 80452 KB Output is correct
11 Correct 52 ms 80516 KB Output is correct
12 Correct 52 ms 80532 KB Output is correct
13 Correct 53 ms 80724 KB Output is correct
14 Correct 51 ms 80720 KB Output is correct
15 Correct 52 ms 80468 KB Output is correct
16 Correct 405 ms 92812 KB Output is correct
17 Correct 337 ms 92396 KB Output is correct
18 Correct 381 ms 92788 KB Output is correct
19 Correct 300 ms 92132 KB Output is correct
20 Correct 407 ms 92076 KB Output is correct
21 Correct 429 ms 93668 KB Output is correct
22 Correct 410 ms 93672 KB Output is correct
23 Correct 420 ms 93688 KB Output is correct
24 Correct 414 ms 93416 KB Output is correct
25 Correct 412 ms 92896 KB Output is correct