답안 #646732

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
646732 2022-09-30T13:19:15 Z jasmin Election (BOI18_election) C++14
100 / 100
683 ms 89884 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long

struct node{
    int free_left=0;
    int free_right=0;
    int left=0;
    int right=0;
    int both=0;
};
node zero={0, 0, 0, 0, 0};

struct segtree{
    vector<node> tree;

    segtree(int n, string& s){
        tree.resize(n*4);
        build(0, n, 0, s);
    }
    node build(int l, int r, int v, string& s){
        if(l+1==r){

            if(s[l]=='T'){
                tree[v].both=1;
            }
            else{
                tree[v].free_left=1;
                tree[v].free_right=1;
            }
            //cout << l << " " << r << " " << v << ": \n";
            //cout << tree[v].both << " " << tree[v].left << " " << tree[v].right << " " << tree[v].free_left << " " << tree[v].free_right << "\n";
            return tree[v];
        }
        int m=l+(r-l)/2;
        tree[v]=merge(build(l, m, v*2+1, s), build(m, r, v*2+2, s));
        //cout << l << " " << r << " " << v << ": \n";
        //cout << tree[v].both << " " << tree[v].left << " " << tree[v].right << " " << tree[v].free_left << " " << tree[v].free_right << "\n";
        return tree[v];
    }

    node merge(node a, node b){
        node ans;
        ans.free_left=a.free_left+b.free_left;
        ans.free_right=a.free_right+b.free_right;
        ans.left=a.left+b.left;
        ans.right=a.right+b.right;
        ans.both=a.both+b.both;

        int sright=0;
        int sleft=0;

        int x=min(a.right, b.free_left);
        sright=a.right-x;
        ans.right-=x; 
        ans.free_left-=x;

        x=min(a.both, b.free_left-x);
        ans.left+=x;
        ans.both-=x;
        ans.free_left-=x;

        int y=min(a.free_right, b.left);
        sleft=b.left-y;
        ans.left-=y;
        ans.free_right-=y;

        y=min(a.free_right-y, b.both);
        ans.right+=y;
        ans.both-=y;
        ans.free_right-=y;

        int k=min(sright, sleft);
        ans.right-=k;
        ans.left-=k;
        ans.both+=k;
        return ans;
    }

    node query(int l, int r, int v, int ql, int qr){
        if(qr<=l || r<=ql) return zero;
        if(ql<=l && r<=qr) return tree[v];
        int m=l+(r-l)/2;
        return merge(query(l, m, v*2+1, ql, qr), query(m, r, v*2+2, ql, qr));
    }
};

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    string votes;
    cin >> votes;
    segtree seg(n, votes);

    int q;
    cin >> q;
    for(int i=0; i<q; i++){
        int l, r;
        cin >> l >> r;

        node resp=seg.query(0, n, 0, l-1, r);
        int ans=resp.both+resp.left+resp.right;
        cout << ans << "\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 2 ms 584 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 2 ms 584 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 68 ms 12616 KB Output is correct
7 Correct 60 ms 12492 KB Output is correct
8 Correct 63 ms 12532 KB Output is correct
9 Correct 59 ms 12572 KB Output is correct
10 Correct 68 ms 12532 KB Output is correct
11 Correct 69 ms 12640 KB Output is correct
12 Correct 71 ms 12712 KB Output is correct
13 Correct 71 ms 12728 KB Output is correct
14 Correct 67 ms 12668 KB Output is correct
15 Correct 67 ms 12604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 2 ms 584 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 68 ms 12616 KB Output is correct
7 Correct 60 ms 12492 KB Output is correct
8 Correct 63 ms 12532 KB Output is correct
9 Correct 59 ms 12572 KB Output is correct
10 Correct 68 ms 12532 KB Output is correct
11 Correct 69 ms 12640 KB Output is correct
12 Correct 71 ms 12712 KB Output is correct
13 Correct 71 ms 12728 KB Output is correct
14 Correct 67 ms 12668 KB Output is correct
15 Correct 67 ms 12604 KB Output is correct
16 Correct 683 ms 88860 KB Output is correct
17 Correct 561 ms 88372 KB Output is correct
18 Correct 648 ms 88668 KB Output is correct
19 Correct 556 ms 88124 KB Output is correct
20 Correct 675 ms 87960 KB Output is correct
21 Correct 665 ms 89716 KB Output is correct
22 Correct 653 ms 89764 KB Output is correct
23 Correct 655 ms 89884 KB Output is correct
24 Correct 648 ms 89440 KB Output is correct
25 Correct 659 ms 89024 KB Output is correct