本地过luoguWA的无旋Treap
查看原帖
本地过luoguWA的无旋Treap
432566
carefree_Zhuang楼主2024/1/15 23:49
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,opt,x;
struct Treap{
	int root,cnt;
	struct Treap_Node{
		int v,w,s,lc,rc;
		Treap_Node(){}
	}t[N];
	Treap(){root=cnt=0;}
	void update(int x){
		t[x].s = t[t[x].lc].s+t[t[x].rc].s+1;
	}
	void split(int x,int v,int &l,int &r){
		if(x==0){l=r=0;return;}
		if(t[x].v<=v)
			l = x,split(t[x].rc,v,t[x].rc,r);
		else
			r = x,split(t[x].lc,v,l,t[x].rc);
		update(x);
	}
	int merge(int l,int r){
//		printf("%d %d %d %d %d %d\n",l,r,t[l].lc,t[l].rc,t[r].lc,t[r].rc);
		if(!l||!r)return l+r;
		if(t[l].w<t[r].w){
			t[l].rc = merge(t[l].rc,r);
			update(l);
			return l;
		}
		else{
			t[r].lc = merge(l,t[r].lc);
			update(r);
			return r;
		}
	}
	void newNode(int v){
		++cnt;
		t[cnt].v = v;
		t[cnt].w = rand();
		t[cnt].s = 1;
		t[cnt].lc = t[cnt].rc = 0;
	}
	void insert(int v){
		int l,r;
		split(root,v,l,r);
		newNode(v);
		root = merge(merge(l,cnt),r);
	}
	void del(int &x,int v){
		int a,b,c;
		split(x,v,a,c);
		split(a,v-1,a,b);
		b = merge(t[b].lc,t[b].rc);
		x = merge(merge(a,b),c);
	}
	int query_rank(int v){
		int a,b,rank;
		split(root,v-1,a,b);
		rank=t[a].s+1;
		root = merge(a,b);
		return rank;
	}
	int query_kth(int &x,int v){
		if(v==t[t[x].lc].s+1)return t[x].v;
		if(v<=t[t[x].lc].s)return query_kth(t[x].lc,v);
		return query_kth(t[x].rc,v-t[t[x].lc].s-1);
	}
	int query_pre(int &x,int v){
		int a,b,c;
		split(x,v-1,a,b);
		for(c=a;t[c].rc;c=t[c].rc);
		x = merge(a,b);
		return t[c].v;
	}
	int query_suc(int &x,int v){
		int a,b,c;
		split(x,v,a,b);
		for(c=b;t[c].lc;c=t[c].lc);
		x = merge(a,b);
		return t[c].v;
	}
}t;
int main(){
	freopen("P3369_1.in","r",stdin);
//	freopen("o.out","w",stdout);
	cin>>n;
	for(int i=1;i<=n;++i){
		cin>>opt>>x;
//		printf("%d %d\n",opt,x);
//	for(int j=1;j<=t.cnt;++j)printf("%d %d %d\n",t.t[j].lc,t.t[j].rc,t.t[j].v);
//	puts(""); 
		if(opt==1)t.insert(x);
//		else if(opt==2)t.del(t.root,x);
		else if(opt==3)cout<<t.query_rank(x)<<endl;
//		else if(opt==4)cout<<t.query_kth(t.root,x)<<endl;
//		else if(opt==5)cout<<t.query_pre(t.root,x)<<endl;
//		else cout<<t.query_suc(t.root,x)<<endl;
	}
	return 0;
}

第一个点数据为

4
1 10
1 30
1 20
3 21

正确答案为3

错误记录

2024/1/15 23:49
加载中...