Splay#9TLE求助
查看原帖
Splay#9TLE求助
671338
Super_dark楼主2023/1/30 17:04

rt,代码如下

#include<bits/stdc++.h>
using namespace std;
const long long maxn=2e6+10;
const long long Inf=0x7fffffff;
long long n,tot,root,Ans,ll;
inline long long read() {
    long long x=0,f=1;
    char ch=getchar();
    while (ch<'0'||ch>'9') {
        if (ch=='-')
            f=-1;
        ch=getchar();
    }
    while (ch>='0'&&ch<='9') {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
inline void write(long long x){
    if(x<0){
        x=-x;
        putchar('-');
    }
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
struct Node{
	long long size,ch[2],fa,cnt,val;
}t[maxn];
inline void pushup(long long x){
	t[x].size=t[t[x].ch[0]].size+t[t[x].ch[1]].size+t[x].cnt;
}
inline void rotate(long long x){
	long long y=t[x].fa;
	long long z=t[y].fa;
	long long k=t[y].ch[1]==x;
	t[z].ch[t[z].ch[1]==y]=x;
	t[x].fa=z;
	t[y].ch[k]=t[x].ch[k^1];
	t[t[x].ch[k^1]].fa=y;
	t[x].ch[k^1]=y;
	t[y].fa=x;
	pushup(y);
	pushup(x);
}
inline void splay(long long x,long long goal){
	while(t[x].fa!=goal){
		long long y=t[x].fa;
		long long z=t[y].fa;
		if(z!=goal){
			(t[y].ch[0]==x)^(t[z].ch[0]==y)?rotate(x):rotate(y);
		}
		rotate(x);
	}
	if(goal==0){
		root=x;
	}
}
inline void Find(long long x){
	long long u=root;
	if(!u)return;
	while(t[u].ch[x>t[u].val] && x!=t[u].val){
		u=t[u].ch[x>t[u].val];
	}
	splay(u,0);
}
inline void Add(long long x){
	long long u=root,fa=0;
	while(u&&t[u].val!=x){
		fa=u;
		u=t[u].ch[x>t[u].val];
	}
	if(u){
		t[u].cnt++;
	}
	else{
		u=++tot;
		if(fa)t[fa].ch[x>t[fa].val]=u;
		t[tot].ch[1]=0;
		t[tot].ch[0]=0;
		t[tot].val=x;
		t[tot].fa=fa;
		t[tot].cnt=1;
		t[tot].size=1;
	}
	splay(u,0);
}
inline long long fr(long long x,long long flag){
	Find(x);
	long long u=root;
	if((t[u].val>x&&flag) || (t[u].val<x && !flag)){
		return u;
	}
	u=t[u].ch[flag];
	while(t[u].ch[flag^1])
		u=t[u].ch[flag^1];
	splay(u,0);
	return u;
}
inline void Delete(long long x){
	long long Front=fr(x,0);
	long long Last=fr(x,1);
	splay(Front,0);
	splay(Last,Front);
	long long del=t[Last].ch[0];
	if(t[del].cnt>1){
		t[del].cnt--;
		splay(del,0);
	}
	else{
		t[Last].ch[0]=0;
	}
}
inline long long find(long long x){
	long long u=root;
	if(t[u].size<x){
		return 0;
	}
	while(u){
		long long y=t[u].ch[0];
		if(x>t[y].size+t[u].cnt){
			x-=t[y].size+t[u].cnt;
			u=t[u].ch[1];
		}
		else{
			if(x<=t[y].size){
				u=y;
			}
			else return t[u].val;
		}
	}
	splay(u,0);
}
signed main(){
	long long n,m;
	Add(Inf);
	Add(-Inf);
	n=read();
	m=read();
	for(long long i=1;i<=n;i++){
		long long xx;
		xx=read();
		Add(xx);
	}
	while(m--){
		long long opt,x,ans;
		opt=read();
		x=read();
		x=x^ll;
		if(opt==1){
			Add(x);
		}
		if(opt==2){
			Delete(x);
		}
		if(opt==3){
			Find(x);
			ll=t[t[root].ch[0]].size+(t[root].val<x?t[root].cnt:0);
		}
		if(opt==4){
			ll=find(x+1);
		}
		if(opt==5){
			ll=t[fr(x,0)].val;
		}
		if(opt==6){
			ll=t[fr(x,1)].val;
		}
		if(3<=opt&&opt<=6){
			Ans=Ans^ll;
		}
	}
	write(Ans);
	return 0;
}
2023/1/30 17:04
加载中...