結果

問題 No.151 セグメントフィッシング
ユーザー latte0119latte0119
提出日時 2015-10-26 21:22:27
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 167 ms / 5,000 ms
コード長 2,468 bytes
コンパイル時間 2,995 ms
コンパイル使用メモリ 164,000 KB
実行使用メモリ 8,368 KB
最終ジャッジ日時 2023-10-11 12:54:00
合計ジャッジ時間 4,689 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,356 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 27 ms
4,464 KB
testcase_09 AC 27 ms
4,476 KB
testcase_10 AC 27 ms
4,504 KB
testcase_11 AC 27 ms
4,584 KB
testcase_12 AC 129 ms
7,784 KB
testcase_13 AC 129 ms
7,752 KB
testcase_14 AC 130 ms
7,800 KB
testcase_15 AC 129 ms
7,772 KB
testcase_16 AC 128 ms
7,820 KB
testcase_17 AC 65 ms
8,368 KB
testcase_18 AC 91 ms
8,080 KB
testcase_19 AC 167 ms
7,168 KB
testcase_20 AC 166 ms
7,168 KB
testcase_21 AC 106 ms
8,088 KB
testcase_22 AC 106 ms
8,020 KB
testcase_23 AC 59 ms
5,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
struct RBST{
    struct node{
        node *lch,*rch;
        int val,sum,cnt;
        node(int val):val(val),sum(val),cnt(1),lch(NULL),rch(NULL){}
    };

    node *root;
    RBST():root(NULL){}

    inline int count(node *t){return t?t->cnt:0;}
    inline int sum(node *t){return t?t->sum:0;}

    node *update(node *t){
        if(!t)return t;
        t->cnt=count(t->lch)+count(t->rch)+1;
        t->sum=sum(t->lch)+sum(t->rch)+t->val;
        return t;
    }

    node *merge(node *a,node *b){
        if(!a)return b;
        if(!b)return a;
        if(rand()%(count(a)+count(b))<count(a)){
            a->rch=merge(a->rch,b);
            return update(a);
        }
        else{
            b->lch=merge(a,b->lch);
            return update(b);
        }
    }

    pair<node *,node *>split(node *t,int k){
        if(!t)return make_pair(t,t);
        if(k<=count(t->lch)){
            pair<node *,node *>s=split(t->lch,k);
            t->lch=s.second;
            return make_pair(s.first,update(t));
        }
        else{
            pair<node *,node *>s=split(t->rch,k-count(t->lch)-1);
            t->rch=s.first;
            return make_pair(update(t),s.second);
        }
    }
    void insert(int k,int x){
        pair<node *,node *>s=split(root,k);
        root=merge(merge(s.first,new node(x)),s.second);
    }
    int erase(int k){
        pair<node *,node *>s1,s2;
        int ret;
        s1=split(root,k);
        s2=split(s1.second,1);
        ret=s2.first->val;
        root=merge(s1.first,s2.second);
        return ret;
    }

    int sum(int l,int r){
        pair<node *,node *>s1,s2;
        int ret;
        s1=split(root,l);
        s2=split(s1.second,r-l);
        ret=s2.first->sum;
        root=merge(merge(s1.first,s2.first),s2.second);
        return ret;
    }
};

int N,Q;
RBST L,R;

signed main(){
    cin>>N>>Q;
    for(int i=0;i<N;i++){
        L.insert(i,0);
        R.insert(i,0);
    }

    while(Q--){
        char type;
        int x,y;
        cin>>type>>x>>y;
        if(type=='L'){
            int val=L.erase(x);
            L.insert(x,val+y);
        }
        else if(type=='R'){
            int val=R.erase(x);
            R.insert(x,val+y);
        }
        else{
            cout<<L.sum(x,y)+R.sum(x,y)<<endl;
        }
        int l=L.erase(0);
        int r=R.erase(N-1);
        L.insert(N-1,r);
        R.insert(0,l);
    }
    return 0;
}
0