FHQ_Treap 28分求调
查看原帖
FHQ_Treap 28分求调
954640
zhengzidu楼主2024/6/22 12:03
#include<bits/stdc++.h>
using namespace std;
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<<1)+(x<<3)+ch-'0';
	ch=getchar();
}
	return x*f;
}
void write(long long x){
	if(x<0){
		putchar('-');
		x=-x;
	}
	if(x>9)
		write(x/10);
	putchar(x%10+'0');
	return;
}
const int dacty=5e5+5;
int n,dfn,ll,rr,cnt,tt,pre;
int root[500005];
struct treap{
	struct node{
		int lson,rson,sum,val,priority;
	}t[dacty*32*4];
	int build(int x){
		t[++cnt].val=x;
		t[cnt].priority=rand();
		t[cnt].sum=1;
		return cnt;
	}
	void push_up(int x){
		t[x].sum=t[t[x].lson].sum+t[t[x].rson].sum+1;
	}
	void split(int id,int x,int &l,int &r){
		if(!id){
			l=r=0;
			return;
		}
		if(t[id].val<=x){
			l=++cnt;
			t[l]=t[id];
			split(t[l].rson,x,t[l].rson,r);
			push_up(l);
		}
		else{
			r=++cnt;
			t[r]=t[id];
			split(t[r].lson,x,l,t[r].lson);
			push_up(r);
		}
	}
	int merge(int l,int r){
		if(!l||!r){
			return l+r;
		}
		if(t[l].priority<t[r].priority){
			t[l].rson=merge(t[l].rson,r);
			push_up(l);
			return l;
		}
		else{
			t[r].lson=merge(l,t[r].lson);
			push_up(r);
			return r;
		}
	}
	void insertt(int x,int now){
		split(root[pre],x,ll,rr);
		root[now]=merge(merge(ll,build(x)),rr);
	}
	void delet(int x,int now){
		split(root[pre],x,ll,rr);
		split(ll,x-1,ll,tt);
		tt=merge(t[tt].lson,t[tt].rson);
		root[now]=merge(merge(ll,tt),rr);
	}
	int get_rank(int x,int now){
		split(root[pre],x-1,ll,rr);
		root[now]=merge(ll,rr);
		return t[ll].sum+1;
	}
	int get_val(int id,int x){
		if(t[t[id].lson].sum==x-1){
			return t[id].val;
		}
		else if(t[t[id].lson].sum>x-1){
			return get_val(t[id].lson,x);
		}
		else{
			return get_val(t[id].rson,x-t[t[id].lson].sum-1);
		}
	}
	int get_pre(int x,int now){
		split(root[pre],x-1,ll,rr);
		int k=get_val(ll,t[ll].sum);
		if(!ll){
			k=INT_MIN+1;
		}
		root[now]=merge(ll,rr);
		return k;
	}
	int get_suc(int x,int now){
		split(root[pre],x,ll,rr);
		int k=get_val(rr,1);
		if(!rr){
			k=INT_MAX;
		}
		root[now]=merge(ll,rr);
		return k;
	}
}tree;
signed main(){
	srand(time(0));
	n=read();
	for(int i=1;i<=n;i++){
		pre=read();
		int op=read();
		int x=read();
		if(op==1){
			tree.insertt(x,i);
		}
		if(op==2){
			tree.delet(x,i);	
		}
		if(op==3){
			write(tree.get_rank(x,i));
			printf("\n");
		}
		if(op==4){
			write(tree.get_val(root[pre],x));
			printf("\n");
			root[i]=root[pre];
		}
		if(op==5){
			write(tree.get_pre(x,i));
			printf("\n");
		}
		if(op==6){
			write(tree.get_suc(x,i));
			printf("\n");
		}
	}
	return 0;
}

3~20 WA ,玄关

2024/6/22 12:03
加载中...