洛谷P5055 【模板】可持久化文艺平衡树(FHQ Treap)

题面

传送门

题解

日常敲板子.jpg

//minamoto
#include<bits/stdc++.h>
#define R register
#define inline __inline__ __attribute__((always_inline))
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
long long read(){
    R long long res,f=1;R char ch;
    while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
    for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
    return res*f;
}
char sr[1<<21],z[20];int K=-1,Z=0;
inline void Ot(){fwrite(sr,1,K+1,stdout),K=-1;}
void print(R long long x){
    if(K>1<<20)Ot();if(x<0)sr[++K]='-',x=-x;
    while(z[++Z]=x%10+48,x/=10);
    while(sr[++K]=z[Z],--Z);sr[++K]='\n';
}
const int N=2e5+5,M=2e7+5;
typedef long long ll;
struct node;typedef node* ptr;
unsigned int aaa=19260817;
inline unsigned int rd(){aaa^=aaa>>15,aaa+=aaa<<12,aaa^=aaa>>3;return aaa;}
inline void swap(R ptr &s,R ptr &t){R ptr p=s;s=t,t=p;}
struct node{
	ptr lc,rc;bool t;int v,sz;ll sum;unsigned int pr;
	inline ptr upd(){return sz=lc->sz+rc->sz+1,sum=lc->sum+rc->sum+v,this;}
	inline ptr init(R int val){return sum=v=val,sz=1,pr=rd(),this;}
	inline ptr ppd(){return swap(lc,rc),t^=1,this;}
}e[M],*rt[N],*pp=e;
inline ptr newnode(R int v){return ++pp,pp->lc=pp->rc=e,pp->init(v);}
inline ptr cl(ptr p){return ++pp,*pp=*p,pp;}
inline ptr pd(ptr p){
	if(p->t){
		if(p->lc!=e)p->lc=cl(p->lc),p->lc->ppd();
		if(p->rc!=e)p->rc=cl(p->rc),p->rc->ppd();
		p->t=0;
	}return p;
}
void split(ptr p,int k,ptr &s,ptr &t){
	if(p==e)return s=t=e,void();
	if(pd(p)->lc->sz<k)s=cl(p),split(s->rc,k-s->lc->sz-1,s->rc,t),s->upd();
		else t=cl(p),split(t->lc,k,s,t->lc),t->upd();
}
ptr merge(ptr s,ptr t){
	if(s==e)return t;if(t==e)return s;
	if(pd(s)->pr<pd(t)->pr)return s->rc=merge(s->rc,t),s->upd();
	return t->lc=merge(s,t->lc),t->upd();
}
void push(ptr &rt,int k,int x){
	ptr s,t;
	split(rt,k,s,t),rt=merge(merge(s,newnode(x)),t);
}
void pop(ptr &rt,int k){
	ptr s,t,p,q;
	split(rt,k,s,t),split(s,k-1,p,q),rt=merge(p,t);
}
void rev(ptr &rt,int l,int r){
	ptr s,t,p,q;
	split(rt,r,s,t),split(s,l-1,p,q);
	rt=merge(merge(p,q->ppd()),t);
}
ll query(ptr &rt,int l,int r){
	ptr s,t,p,q;ll res;
	split(rt,r,s,t),split(s,l-1,p,q),res=q->sum;
	return rt=merge(merge(p,q),t),res;
}
ll lasans,id,op,l,r,x,k;
int main(){
//	freopen("testdata.in","r",stdin);
	rt[0]=e,e->lc=e->rc=e,lasans=0;
	for(int Q=read(),i=1;i<=Q;++i){
		id=read(),op=read(),rt[i]=rt[id];
		switch(op){
			case 1:k=read()^lasans,x=read()^lasans,push(rt[i],k,x);break;
			case 2:k=read()^lasans,pop(rt[i],k);break;
			case 3:l=read()^lasans,r=read()^lasans,rev(rt[i],l,r);break;
			case 4:l=read()^lasans,r=read()^lasans,print(lasans=query(rt[i],l,r));break;
		}
	}
	return Ot(),0;
}
posted @ 2019-05-17 22:30  bztMinamoto  阅读(353)  评论(0编辑  收藏  举报
Live2D