結果

問題 No.259 セグメントフィッシング+
ユーザー shimomireshimomire
提出日時 2015-02-16 18:04:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 548 ms / 2,000 ms
コード長 4,305 bytes
コンパイル時間 1,815 ms
コンパイル使用メモリ 138,932 KB
実行使用メモリ 22,328 KB
最終ジャッジ日時 2023-09-06 01:38:50
合計ジャッジ時間 12,344 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 452 ms
22,152 KB
testcase_09 AC 454 ms
22,208 KB
testcase_10 AC 459 ms
22,232 KB
testcase_11 AC 449 ms
22,128 KB
testcase_12 AC 443 ms
22,100 KB
testcase_13 AC 543 ms
22,328 KB
testcase_14 AC 536 ms
22,252 KB
testcase_15 AC 546 ms
22,272 KB
testcase_16 AC 539 ms
22,264 KB
testcase_17 AC 535 ms
22,096 KB
testcase_18 AC 534 ms
22,100 KB
testcase_19 AC 543 ms
22,260 KB
testcase_20 AC 548 ms
22,228 KB
testcase_21 AC 543 ms
22,152 KB
testcase_22 AC 527 ms
22,328 KB
testcase_23 AC 73 ms
4,516 KB
testcase_24 AC 420 ms
22,088 KB
testcase_25 AC 421 ms
22,144 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> tree;
		REP(i,2*N)tree.insert(i,0);

        ll pt=1;
		REP(q,Q){
			char x;ll t;int y,z;cin >> x >>t >> y >> z;
            
            //shift
            {
                int sh=pmod(t-pt,2*N);
                auto sp = tree.split(2*N-sh,tree.root);
                tree.root = tree.merge(sp.second,sp.first);
            }

            pt=t;
            if(x=='R')tree.add(y,y+1,z);
			if(x=='L')tree.add(N+(N-1-y),N+(N-1-y)+1,z);
			if(x=='C'){
				cout << tree.query(y,z) + tree.query(N+(N-1-z)+1,N+(N-1-y)+1)<<endl;
			}
		}
	}
};

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