結果

問題 No.151 セグメントフィッシング
ユーザー shimomireshimomire
提出日時 2015-02-10 23:32:07
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 102 ms / 5,000 ms
コード長 4,130 bytes
コンパイル時間 2,129 ms
コンパイル使用メモリ 135,760 KB
実行使用メモリ 7,384 KB
最終ジャッジ日時 2023-09-05 23:05:16
合計ジャッジ時間 4,091 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 17 ms
4,380 KB
testcase_09 AC 17 ms
4,376 KB
testcase_10 AC 16 ms
4,380 KB
testcase_11 AC 17 ms
4,380 KB
testcase_12 AC 69 ms
7,216 KB
testcase_13 AC 71 ms
7,184 KB
testcase_14 AC 71 ms
7,144 KB
testcase_15 AC 70 ms
7,196 KB
testcase_16 AC 68 ms
7,192 KB
testcase_17 AC 44 ms
7,208 KB
testcase_18 AC 46 ms
7,240 KB
testcase_19 AC 102 ms
7,156 KB
testcase_20 AC 101 ms
7,384 KB
testcase_21 AC 54 ms
7,140 KB
testcase_22 AC 55 ms
7,112 KB
testcase_23 AC 35 ms
5,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>// c
#include <iostream>// io
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>// container
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <algorithm>// other
#include <complex>
#include <numeric>
#include <functional>
#include <random>
#include <regex>
using namespace std;

using ll =long long;

#define ALL(c) (begin(c)),(end(c))
#define REP(i,n) FOR(i,0,n)
#define REPr(i,n) FORr(i,0,n)
#define FOR(i,l,r) for(int i=(int)(l);i<(int)(r);++i)
#define FORr(i,l,r) for(int i=(int)(r)-1;i>=(int)(l);--i)
#define EACH(it,o) for(auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l,v,r) ((l)<=(v) && (v)<(r))
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end())
//debug
#define DUMP(x)  cerr << #x << " = " << (x)
#define LINE()    cerr<< " (L" << __LINE__ << ")"

ll pmod(ll v,ll M){return (v%M+M)%M;}

//[a,b)にxを加算 O(logN) [a,b)の最小値 O(logN)
template<typename T> struct RBST{
public:
    struct Node{
        T v,sum;// 値,和
        int sz;//部分木のサイズ
        Node *l,*r;
        Node(T v):v(v),sum(v),sz(1),l(NULL),r(NULL){
        }
    };
    int size(){return size(root);}
    int size(Node* t){return !t?0:t->sz;}
    T sum(Node* t){return !t?0:t->sum;}

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

    Node* update(Node* t){
        t->sz=size(t->l)+size(t->r)+1;
        t->sum=sum(t->l)+sum(t->r)+t->v;   
        return t;
    }

    Node* merge(Node* l,Node* r){
        if(!l || !r)return l?l:r;
        update(l);update(r);
        if(rand()%(size(l)+size(r))<size(l)){
            l->r = merge(l->r,r);
            return update(l);
        }else{
            r->l = merge(l,r->l);
            return update(r);
        }
    }

    pair<Node*,Node*> split(int k,Node* t){//[0,k) [k,n)
        if(!t)return {NULL,NULL};   
        update(t);
        if(k<=size(t->l)){
            auto s=split(k,t->l);
            t->l=s.second; s.second=update(t);
            return s;
        }else{
            auto s=split(k - (size(t->l)+1),t->r);
            t->r=s.first; s.first=update(t);
            return s;
        }
    }

    Node* insert(int k,T v){return root=insert(k,v,root);}
    Node* insert(int k,T v,Node* t){
        auto s=split(k,t);
        return update(merge(merge(s.first,new Node(v)),s.second));
    }

    Node* erase(int k){return root=erase(k,root);}
    Node* erase(int k,Node* t){
        auto sr=split(k+1,t),sl=split(k,sr.first);  
        return update(merge(sl.first,sr.second));
    }

    Node* find(int k){return find(k,root);}
    Node* find(int k,Node* t){
        update(t);
        if(k==size(t->l))return t;
        if(k<size(t->l)) return find(k,t->l);
        else return find(k - size(t->l) - 1,t->r);
    }

    void add(int l,int r,T x){return add(l,r,x,root);}
    void add(int l,int r,T x,Node* t){
        if(!t || size(t)<=l || r<=0)return;
        if(l<=0 && size(t) <=r){
            t->v+=x;
        	update(t);
			return;
        }
        add(l,r,x,t->l);
        if(l<=size(t->l) && size(t->l) < r) t->v += x;
        add(l- size(t->l) -1,r- size(t->l) -1,x,t->r);
        update(t);
    }

    T query(int l,int r){return query(l,r,root);}
    T query(int l,int r,Node* t){
        if(!t || size(t)<=l || r<=0)return 0;
        // cerr << make_tuple(l,r,t->sum)<<endl;
        if(l<=0 && size(t)<=r)return sum(t);
        update(t);
        T res=query(l,r,t->l) + query(l- size(t->l) -1,r- size(t->l) -1,t->r);
        if(l<=size(t->l) && size(t->l) < r) res += t->v;
        return res;
    }
};

class Main{
	public:
	void run(){
		int N,Q;cin >> N >> Q;

		RBST<ll> L,R;
		REP(i,N)L.insert(i,0);
		REP(i,N)R.insert(i,0);

		REP(t,Q){
			char x;ll y,z;cin >> x >> y >> z;
			if(x=='L')L.add(y,y+1,z);
			if(x=='R')R.add(y,y+1,z);
			if(x=='C'){
				cout << L.query(y,z) + R.query(y,z)<<endl;
			}

			//shift
			ll lv=L.find(0)->v;
			ll rv=R.find(N-1)->v;

			L.erase(0);L.insert(N-1,rv);
			R.erase(N-1);R.insert(0,lv);
		}
	}
};

int main(){
	cout <<fixed<<setprecision(20);
	cin.tie(0);
	ios::sync_with_stdio(false);
	Main().run();
	return 0;
}
0