Treap WA 求助
查看原帖
Treap WA 求助
556362
Unnamed114514楼主2022/2/6 23:31
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1e5+1e6+5;
int tot,root,n,m,last,ans;
struct Treap{
	int l,r,val,dat,cnt,size;
}a[maxn];
inline int read(){
	int res=0,f=0;
	char ch=getchar();
	while(ch<'0'||ch>'9')
		f|=(ch=='-'),ch=getchar();
	while(ch>='0'&&ch<='9'){
		res=(res<<1)+(res<<3)+(ch^'0');
		ch=getchar();
	}
	return f?-res:res;
}
inline int New(int val){
	a[++tot].val=val;
	a[tot].dat=rand();
	a[tot].cnt=a[tot].size=1;
	return tot;
}
inline void Update(int p){
	a[p].size=a[a[p].l].size+a[a[p].r].size+a[p].cnt;
}
inline void Build(){
	New(-inf),New(inf);
	root=1,a[1].r=2;
	Update(root);
}
int GetRank(int p,int val){
	if(p==0)
		return 0;
	if(val==a[p].val)
		return a[a[p].l].size+1;
	if(val<a[p].val)
		return GetRank(a[p].l,val);
	return GetRank(a[p].r,val)+a[a[p].l].size+a[p].cnt;
}
int GetVal(int p,int rank){
	if(p==0)
		return inf;
	if(a[a[p].l].size>=rank)
		return GetVal(a[p].l,rank);
	if(a[a[p].l].size+a[p].cnt>=rank)
		return a[p].val;
	return GetVal(a[p].r,rank-a[a[p].l].size-a[p].cnt);
}
inline void Zig(int &p){
	int q=a[p].l;
	a[p].l=a[q].r,a[q].r=p,p=q;
	Update(a[p].r),Update(p);
}
inline void Zag(int &p){
	int q=a[p].r;
	a[p].r=a[q].l,a[q].l=p,p=q;
	Update(a[p].l),Update(p);
}
void Insert(int &p,int val){
	if(!p){
		p=New(val);
		return;
	}
	if(val==a[p].val){
		++a[p].cnt,Update(p);
		return;
	}
	if(val<a[p].val){
		Insert(a[p].l,val);
		if(a[p].dat<a[a[p].l].dat)
			Zig(p);
	} else{
		Insert(a[p].r,val);
		if(a[p].dat<a[a[p].r].dat)
			Zag(p);
	}
	Update(p);
}
inline int GetPre(int val){
	int ans=1,p=root;
	while(p){
		if(val==a[p].val){
			if(a[p].l>0){
				p=a[p].l;
				while(a[p].r>0)
					p=a[p].r;
				ans=p;
			}
			break;
		}
		if(a[p].val<val&&a[p].val>a[ans].val)
			ans=p;
		p=val<a[p].val?a[p].l:a[p].r;
	}
	return a[ans].val;
}
inline int GetNxt(int val){
	int ans=2,p=root;
	while(p){
		if(val==a[p].val){
			if(a[p].r>0){
				p=a[p].r;
				while(a[p].l>0)
					p=a[p].l;
				ans=p;
			}
			break;
		}
		if(a[p].val>val&&a[p].val<a[ans].val)
			ans=p;
		p=val<a[p].val?a[p].l:a[p].r;
	}
	return a[ans].val;
}
void Remove(int &p,int val){
	if(p==0)
		return;
	if(val==a[p].val){
		if(a[p].cnt>1){
			a[p].cnt--,Update(p);
			return;
		}
		if(a[p].l||a[p].r){
			if(a[p].r==0||a[a[p].l].dat>a[a[p].r].dat)
				Zig(p),Remove(a[p].r,val);
			else
				Zag(p),Remove(a[p].l,val);
			Update(p);
		} else
			p=0;
		return;
	}
	val<a[p].val?Remove(a[p].l,val):Remove(a[p].r,val);
	Update(p);
}
int main(){
	Build();
	n=read(),m=read();
	while(n--){
		int x=read();
		Insert(root,x);
	}
	while(m--){
		int opt=read(),x=read()^last;
		if(opt==1)
			Insert(root,x);
		else if(opt==2)
			Remove(root,x);
		else if(opt==3)
			last=GetRank(root,x)-1,ans^=last;
		else if(opt==4)
			last=GetVal(root,x+1),ans^=last;
		else if(opt==5)
			last=GetPre(x),ans^=last;
		else
			last=GetNxt(x),ans^=last;
	}
	printf("%d\n",ans);
	return 0;
}

P3369 过了

2022/2/6 23:31
加载中...