답안 #988807

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
988807 2024-05-26T09:30:43 Z IdanRosen Election (BOI18_election) C++
100 / 100
436 ms 86252 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) {
        if (curr_l == curr_r - 1) {
            tree[node].left = curr_l;
            tree[node].right = curr_r;

            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);

        pull_index(node);
    }

    inline void pull_index(int node_in) {
        tree[node_in] = pull(tree[node_in * 2], tree[node_in * 2 + 1]);
    }

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

        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))) throw;

        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";
    }

}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 78684 KB Output is correct
2 Correct 12 ms 78720 KB Output is correct
3 Correct 13 ms 78616 KB Output is correct
4 Correct 12 ms 78736 KB Output is correct
5 Correct 13 ms 78684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 78684 KB Output is correct
2 Correct 12 ms 78720 KB Output is correct
3 Correct 13 ms 78616 KB Output is correct
4 Correct 12 ms 78736 KB Output is correct
5 Correct 13 ms 78684 KB Output is correct
6 Correct 52 ms 79604 KB Output is correct
7 Correct 63 ms 79652 KB Output is correct
8 Correct 49 ms 79452 KB Output is correct
9 Correct 45 ms 79480 KB Output is correct
10 Correct 53 ms 79452 KB Output is correct
11 Correct 53 ms 79700 KB Output is correct
12 Correct 60 ms 79696 KB Output is correct
13 Correct 55 ms 79752 KB Output is correct
14 Correct 55 ms 79608 KB Output is correct
15 Correct 53 ms 79700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 78684 KB Output is correct
2 Correct 12 ms 78720 KB Output is correct
3 Correct 13 ms 78616 KB Output is correct
4 Correct 12 ms 78736 KB Output is correct
5 Correct 13 ms 78684 KB Output is correct
6 Correct 52 ms 79604 KB Output is correct
7 Correct 63 ms 79652 KB Output is correct
8 Correct 49 ms 79452 KB Output is correct
9 Correct 45 ms 79480 KB Output is correct
10 Correct 53 ms 79452 KB Output is correct
11 Correct 53 ms 79700 KB Output is correct
12 Correct 60 ms 79696 KB Output is correct
13 Correct 55 ms 79752 KB Output is correct
14 Correct 55 ms 79608 KB Output is correct
15 Correct 53 ms 79700 KB Output is correct
16 Correct 432 ms 85224 KB Output is correct
17 Correct 340 ms 85220 KB Output is correct
18 Correct 381 ms 85224 KB Output is correct
19 Correct 328 ms 84712 KB Output is correct
20 Correct 418 ms 84200 KB Output is correct
21 Correct 424 ms 85988 KB Output is correct
22 Correct 420 ms 85736 KB Output is correct
23 Correct 413 ms 86252 KB Output is correct
24 Correct 415 ms 85768 KB Output is correct
25 Correct 436 ms 85292 KB Output is correct