Splay WA65pts 求调
查看原帖
Splay WA65pts 求调
1067449
Chillturtle楼主2024/6/5 16:42
#include<bits/stdc++.h>
#define int long long 
using namespace std;
const int N=4e6+10;
const int M=1e7+10;
struct node{
	int data;
	int lson,rson;
	int sz;
	int fa;
	int num;
	int lnum,rnum;
}tree[N];
int tot,n,root;
int cnt[N];
struct Splay{
	void change(int x){
		tree[x].num=tree[x].lnum+tree[x].rnum+cnt[x];
	}
	void zag(int x){
		int father=tree[x].fa;
		tree[father].rson=tree[x].lson;
		if(tree[x].lson){
			tree[tree[x].lson].fa=father;
		}
		tree[x].fa=tree[father].fa;
		if(tree[father].fa){
			if(father==tree[tree[father].fa].lson){
				tree[tree[father].fa].lson=x;
			}else{
				tree[tree[father].fa].rson=x;
			}
		}
		tree[x].lson=father;
		tree[father].fa=x;
		tree[father].rnum=tree[x].lnum;
		tree[x].lnum=tree[father].rnum+tree[father].lnum+1;
		return;
	}
	void zig(int x){
		int father=tree[x].fa;
		tree[father].lson=tree[x].rson;
		if(tree[x].rson){
			tree[tree[x].rson].fa=father;
		}
		tree[x].fa=tree[father].fa;
		if(tree[father].fa){
			if(father==tree[tree[father].fa].lson){
				tree[tree[father].fa].lson=x;
			}else{
				tree[tree[father].fa].rson=x;
			}
		}
		tree[x].rson=father;
		tree[father].fa=x;
		tree[father].lnum=tree[x].rnum;
		tree[x].rnum=tree[father].rnum+tree[father].lnum+1;
		return;
	}
	void splay(int x){
		int father;
		while(tree[x].fa){
			father=tree[x].fa;
			if(tree[father].fa==0){
				if(x==tree[father].lson){
					zig(x);
				}else{
					zag(x);
				}
				break;
			}
			if(x==tree[father].lson){
				if(father==tree[tree[father].fa].lson){
					zig(father);
					zig(x);
				}else{
					zig(x);
					zag(x);
				}
			}else{
				if(father==tree[tree[father].fa].rson){
					zag(father);
					zag(x);
				}else{
					zag(x);
					zig(x);
				}
			}
		}
		root=x;
		return;
	}
	void insert(int x){
		int place,father;
		father=root;
		while(father!=0){
			place=father;
//			int opt=tree[father].data;
			if(x<=tree[father].data){
				tree[father].lnum++;
				father=tree[father].lson;
			}else{
				tree[father].rnum++;
				father=tree[father].rson;
			}
		}
		tot++;
		tree[tot].data=x;
		tree[tot].lson=0;
		tree[tot].rson=0;
		tree[tot].lnum=0;
		tree[tot].rnum=0;
		tree[tot].fa=0;
		cnt[tot]++;
		if(root==0){
			root=tot;
			return;
		}
		tree[tot].fa=place;
		if(x<=tree[place].data){
			tree[place].lson=tot;
		}else{
			tree[place].rson=tot;
		}
		change(tot);
		splay(tot);
		return;
	}
	bool find(int x){
		int father=root;
		while(father){
			if(tree[father].data==x){
				splay(father);
				return 1;
			}
			if(tree[father].data>x){
				father=tree[father].lson;
			}else{
				father=tree[father].rson;
			}
		}
		return 0;
	}
	int rk(int x){
		int ans=1,father=root;
		while(father){
			if(x<tree[father].data){
				father=tree[father].lson;
			}else{
				ans+=tree[father].lnum;
				if(x==tree[father].data){
					splay(father);
					return ans;
				}
				ans+=cnt[father];
				father=tree[father].rson;
			}
		}
		return ans;
	}
	int Find(int k){
		int father=root;
//		if(tree[father].lnum+tree[father].rnum+1<k){
//			return -1;
//		}
		while(1){
			if(tree[father].lson&&k<=tree[father].lnum){
				father=tree[father].lson;
			}else{
				k-=(cnt[father]+tree[father].lnum);
				if(k<=0){
					splay(father);
					return tree[father].data;
				}
				father=tree[father].rson;
			}
		}
		return 0;
	}
	int before(int x){
		find(x);
		int child=tree[root].lson;
		while(child){
			if(tree[child].rson==0) break;
			child=tree[child].rson;
		}
		if(child){
			return tree[child].data;
		}
		return 0;
	}
	int nxt(int x){
		find(x);
		int child=tree[root].rson;
		while(child){
			if(tree[child].lson==0) break;
			child=tree[child].lson;
		}
		if(child){
			return tree[child].data;
		}
		return 0;
	}
	void del(int x){
		find(x);
		int father=root;
		int ls=tree[father].lson,rs=tree[father].rson;
		if(cnt[father]>=2){
			cnt[father]--;
			change(root);
			return;
		}
		if(ls==0&&rs==0){
			root=0;
			return;
		}
		if(ls==0){
			root=rs;
			tree[rs].fa=0;
			return;
		}
		if(rs==0){
			root=ls;
			tree[ls].fa=0;
			return;
		}
		father=ls;
		tree[ls].fa=0;
		while(tree[father].rson){
			father=tree[father].rson;
		}
		splay(father);
//		cnt[father]--;
		tree[father].rson=rs;
		tree[rs].fa=father;
		change(root);
		return;
	}
}spl;
signed main(){
	freopen("P3369_8.in","r",stdin);
	freopen("P3369_8.std","w",stdout);
	cin>>n;
//	int times=n;
	while(n--){
		int opt,x;
		cin>>opt>>x;
		if(opt==1){
			spl.insert(x);
		}else if(opt==2){
			spl.del(x);
		}else if(opt==3){
//			cout<<"           ";
			cout<<spl.rk(x)<<endl;
		}else if(opt==4){
//			cout<<"           ";
			cout<<spl.Find(x)<<endl;
		}else if(opt==5){
			int optt=spl.find(x);
			if(optt==0){
				spl.insert(x);
			}
//			cout<<"           ";
			cout<<spl.before(x)<<endl;
			if(optt==0){
				spl.del(x);
			}
		}else{
			int optt=spl.find(x);
			if(optt==0){
				spl.insert(x);
			}
//			cout<<"           ";
			cout<<spl.nxt(x)<<endl;
			if(optt==0){
				spl.del(x);
			}
		}
//		for(int i=1;i<=times;i++){
//			cout<<cnt[i]<<" ";
//		}
//		cout<<endl;
	}
	return 0;
}
/*
  10
  1 106465
  4 1
  1 317721
  1 460929
  1 644985
  1 84185
  1 89851
  6 81968
  1 492737
  5 493598
  
  106465 84185 492737
  
  
  10
  1 10
  1 10
  1 10
  2 10
  2 10
  2 10
  1 20
  1 30
  1 40
  4 3
  
  40
  
  
  7
  1 24
  1 25
  1 20
  3 23
  3 23
  3 23
  3 23
  
  2 2 2 2
  
  5
  1 -10000000
  1 -30000000
  1 -20000000
  2 -20000000
  6 -20000010
  
  -10000000
  
  from:xqz
 */
2024/6/5 16:42
加载中...