bzoj1895: Pku3580 supermemo

传送门:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1895

思路:fhq_treap。

其他操作都好办,对于revolve操作其实就是交换两个区间而已,知道这一点就没有什么了。

(在bzoj srand(time(0))会0ms RE.....)


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define ls ch[p][0]
#define rs ch[p][1]
#define PI pair<int,int>
#define mp(a,b) make_pair(a,b)
const int maxn=200010;
using namespace std;
int n,m,v;char op[10];

struct Treap{
	int root,tot,ch[maxn][2],size[maxn],add[maxn],mins[maxn],val[maxn],key[maxn];bool rev[maxn];
	int newnode(int v){size[++tot]=1,ch[tot][0]=ch[tot][1]=0,add[tot]=0,rev[tot]=0,mins[tot]=val[tot]=v,key[tot]=rand()%1164186217;return tot;}
	void inc(int p,int v){if (p) val[p]+=v,mins[p]+=v,add[p]+=v;}
	void rever(int p){if (p) rev[p]^=1,swap(ls,rs);}
	void down(int p){
		if (add[p]) inc(ls,add[p]),inc(rs,add[p]),add[p]=0;
		if (rev[p]) rever(ls),rever(rs),rev[p]=0;
	}
	void update(int p){
		size[p]=1,mins[p]=val[p];
		if (ls) size[p]+=size[ls],mins[p]=min(mins[p],mins[ls]);
		if (rs) size[p]+=size[rs],mins[p]=min(mins[p],mins[rs]);
	}
	int merge(int a,int b){
		if (!a){update(b);return b;}
		if (!b){update(a);return a;}
		down(a),down(b);
		if (key[a]<key[b]){ch[a][1]=merge(ch[a][1],b),update(a);return a;}
		else{ch[b][0]=merge(a,ch[b][0]),update(b);return b;}
	}
	PI split(int p,int k){
		if (!p) return mp(0,0);
		down(p);
		if (size[ls]+1<=k){
			PI tmp=split(rs,k-size[ls]-1);
			rs=tmp.first,update(p);
			return mp(p,tmp.second);
		}
		else{
			PI tmp=split(ls,k);
			ls=tmp.second,update(p);
			return mp(tmp.first,p);
		}
	}
	void insert(int k,int v){
		PI tmp=split(root,k);
		root=merge(merge(tmp.first,newnode(v)),tmp.second);
	}
	void del(int k){
		PI x=split(root,k-1),y=split(x.second,1);
		root=merge(x.first,y.second);
	}
	void work(int l,int r,int v,int op){
		PI tmp1=split(root,l-1),tmp2=split(tmp1.second,r-l+1);
		if (op==1) inc(tmp2.first,v);
		else if (op==2) rever(tmp2.first);
		else if (op==3) printf("%d\n",mins[tmp2.first]);
		root=merge(merge(tmp1.first,tmp2.first),tmp2.second);
	}
	void revolve(int l,int r,int t){
		int len=r-l+1;t%=len;
		if (!t) return;
		PI tmp1=split(root,l-1),tmp2=split(tmp1.second,len),tmp3=split(tmp2.first,len-t);
		root=merge(merge(merge(tmp1.first,tmp3.second),tmp3.first),tmp2.second);
	}
}T;

void watch(){
	for (int i=1;i<=T.tot;i++) T.work(i,i,0,3);puts("");
}

int main(){
//	freopen("aa.in","r",stdin);freopen("aa.out","w",stdout);
	srand(1564154),scanf("%d%d",&n,&v),T.root=T.newnode(v);
	for (int i=2;i<=n;i++) scanf("%d",&v),T.root=T.merge(T.root,T.newnode(v));
	scanf("%d",&m);
	for (int i=1,l,r;i<=m;i++){
		scanf("%s",op);
		if (op[0]=='A') scanf("%d%d%d",&l,&r,&v),T.work(l,r,v,1);
		else if (op[0]=='R'){
			if (op[3]=='E') scanf("%d%d",&l,&r),T.work(l,r,0,2);
			else if (op[3]=='O') scanf("%d%d%d",&l,&r,&v),T.revolve(l,r,v);
		}
		else if (op[0]=='M') scanf("%d%d",&l,&r),T.work(l,r,0,3);//watch();
		else if (op[0]=='I') scanf("%d%d",&l,&v),T.insert(l,v);
		else if (op[0]=='D') scanf("%d",&l),T.del(l);
		//puts("fuck");
	}
	return 0;
}


你可能感兴趣的:(treap)