Splay 90pts 求助
查看原帖
Splay 90pts 求助
356925
快斗游鹿楼主2022/8/5 21:15

RT,第五个点 WA。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=200000;
struct Node{
	ll ch[2];
	ll f,cnt,son,val;
}t[N];
ll m,q,root,tot;
void push_up(ll x){
	t[x].son=t[t[x].ch[0]].son+t[t[x].ch[1]].son+t[x].cnt;
}
void rotate(ll x){
	ll y=t[x].f;
	ll z=t[y].f;
	ll k=(t[y].ch[1]==x);
	t[z].ch[t[z].ch[1]==y]=x;t[x].f=z;
	t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].f=y;
	t[x].ch[k^1]=y;t[y].f=x;
	push_up(y);push_up(x);
}
void Splay(ll x,ll goal){
	while(t[x].f!=goal){
		ll y=t[x].f;
		ll z=t[y].f;
		if(z!=goal){
			(t[z].ch[0]==y)^(t[y].ch[0]==x)?rotate(x):rotate(y);
		}
		rotate(x);
	}
	if(!goal){
		root=x;
	}
}
void insert(ll x){//插入
	ll u=root,ff=0;
	while(u&&t[u].val!=x){
		ff=u;
		u=t[u].ch[t[u].val<x];
	}
	if(u){
		t[u].cnt++;
	}
	else{
		u=++tot;
		if(ff){
			t[ff].ch[t[ff].val<x]=u;
		}
		t[u].cnt=t[u].son=1;t[u].f=ff;t[u].val=x;
	}
	Splay(u,0);
}
ll rk(ll x){//按排名找值
	ll u=root;
	if(t[u].son<x)return t[u].son;
	while(1){
		ll y=t[u].ch[0];
		if(x>t[y].son+t[u].cnt){
			x=x-t[y].son-t[u].cnt;
			u=t[u].ch[1];
		}
		else if(t[y].son>=x){
			u=y;
		}
		else{
			Splay(u,0);
			return t[u].val;
		}
	}
}
int main(){
	//freopen("P2343_5.in","r",stdin);
	//freopen("std.out","w",stdout);
	scanf("%lld%lld",&m,&q);
	insert(-1145141999);
	insert(1145141999);
	for(int i=1;i<=m;i++){
		ll aaa;scanf("%lld",&aaa);
		insert(aaa);
	}
	for(int i=1;i<=q;i++){
		ll c,n;scanf("%lld%lld",&c,&n);
		if(c==1){
			printf("%lld\n",rk(-n+m+2));
		}
		else{
			insert(n);m++; 
		}
	}
}
2022/8/5 21:15
加载中...