답안 #414223

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
414223 2021-05-30T09:21:15 Z shivensinha4 Election (BOI18_election) C++17
100 / 100
776 ms 50364 KB
#include <bits/stdc++.h>
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
#define endl '\n'

struct Node {
    int pref = 0, sum = 0;
};

class SegmentTree {
private:
    vector<Node> tree; int n;
    Node bound = {0, 0};

    static Node merge(Node a, Node b) {
        Node ans;
        ans.sum = a.sum + b.sum;
        ans.pref = max(a.pref, a.sum + b.pref);
        return ans;
    }

    void update(int i, int val, int l, int r, int p) {
        if (l > i or r < i) return;
        if (l == r) {
            tree[p] = {val, val};
            return;
        }

        int mid = (l + r) / 2;
        int c1 = 2*p+1, c2 = 2*p+2;
        update(i, val, l, mid, c1); update(i, val, mid+1, r, c2);
        tree[p] = merge(tree[c1], tree[c2]);
    }

    Node mx(int i, int j, int l, int r, int p) {
        if (l > j or r < i) return bound;
        if (l >= i and r <= j) return tree[p];

        int mid = (l + r) / 2;
        int c1 = 2*p+1, c2 = 2*p+2;
        return merge(mx(i, j, l, mid, c1), mx(i, j, mid+1, r, c2));
    }

public:
    SegmentTree(int _n) {
        n = _n;
        tree.resize(4*n, bound);
    }

    Node mx(int i, int j) {
        return mx(i, j, 0, n-1, 0);
    }

    void update(int i, int val) {
        update(i, val, 0, n-1, 0);
    }
};

// Ask for sum 1 -> n for full (one based indexing)
class BIT {
private: vector<ll> tree; int n;
    int LSOne(int x) {
        return x&(-x);
    }

public:
    BIT(int x) {
        n = x;
        tree.resize(n+1);
    }
    ll sum(int a) {
        ll sum = 0;
        for (; a > 0; a -= LSOne(a)) sum += tree[a];
        return sum;
    }
    ll sum(int a, int b) {
        return sum(b) - (a == 1 ? 0 : sum(a-1));
    }
    void update(int p, ll v) {
        for (; p < n+1; p += LSOne(p)) tree[p] += v;
    }
};


int main() {
#ifdef mlocal
    freopen("test.in", "r", stdin);
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n; cin >> n;
    vi nums(n);
    for_(i, 0, n) {
        char c; cin >> c;
        nums[i] = c == 'C' ? 1 : -1;
    }


    int q; cin >> q;
    vector<vector<ii>> ends(n);
    for_(i, 0, q) {
        int l, r; cin >> l >> r;
        ends[l-1].push_back({r-1, i});
    }

    vi out(q), st;

    SegmentTree tree(n);
    BIT negpref(n);

    for (int i = n-1; i >= 0; i--) {
        if (nums[i] == -1) {
            negpref.update(i+1, 1);
            st.push_back(i);
        }
        else {
            tree.update(i, 1);
            if (st.size()) {
                negpref.update(st.back()+1, -1);
                tree.update(st.back(), -1);
                st.pop_back();
            }
        }

        for (auto &r: ends[i]) {
            Node temp = tree.mx(i, r[0]);
            out[r[1]] = temp.pref - temp.sum + negpref.sum(r[0]+1);
        }

    }

    for (auto i: out) cout << i << endl;
    cout << endl;

//    cout << ans + tree.mx(0, n-1) - tree.tree[0].sum << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 460 KB Output is correct
2 Correct 3 ms 456 KB Output is correct
3 Correct 3 ms 424 KB Output is correct
4 Correct 3 ms 460 KB Output is correct
5 Correct 3 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 460 KB Output is correct
2 Correct 3 ms 456 KB Output is correct
3 Correct 3 ms 424 KB Output is correct
4 Correct 3 ms 460 KB Output is correct
5 Correct 3 ms 460 KB Output is correct
6 Correct 81 ms 7516 KB Output is correct
7 Correct 66 ms 7192 KB Output is correct
8 Correct 71 ms 7160 KB Output is correct
9 Correct 73 ms 7480 KB Output is correct
10 Correct 81 ms 7492 KB Output is correct
11 Correct 72 ms 7712 KB Output is correct
12 Correct 72 ms 7656 KB Output is correct
13 Correct 72 ms 7908 KB Output is correct
14 Correct 79 ms 7668 KB Output is correct
15 Correct 74 ms 7588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 460 KB Output is correct
2 Correct 3 ms 456 KB Output is correct
3 Correct 3 ms 424 KB Output is correct
4 Correct 3 ms 460 KB Output is correct
5 Correct 3 ms 460 KB Output is correct
6 Correct 81 ms 7516 KB Output is correct
7 Correct 66 ms 7192 KB Output is correct
8 Correct 71 ms 7160 KB Output is correct
9 Correct 73 ms 7480 KB Output is correct
10 Correct 81 ms 7492 KB Output is correct
11 Correct 72 ms 7712 KB Output is correct
12 Correct 72 ms 7656 KB Output is correct
13 Correct 72 ms 7908 KB Output is correct
14 Correct 79 ms 7668 KB Output is correct
15 Correct 74 ms 7588 KB Output is correct
16 Correct 728 ms 48696 KB Output is correct
17 Correct 549 ms 45092 KB Output is correct
18 Correct 658 ms 45876 KB Output is correct
19 Correct 590 ms 47600 KB Output is correct
20 Correct 736 ms 47752 KB Output is correct
21 Correct 708 ms 50072 KB Output is correct
22 Correct 747 ms 49696 KB Output is correct
23 Correct 708 ms 50364 KB Output is correct
24 Correct 776 ms 49712 KB Output is correct
25 Correct 728 ms 48960 KB Output is correct