RT,本人刚学两天Splay,WA了9个点,感觉写的没问题,求助大佬帮忙查错,您将得到我的关注
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=100005;
int n,m,cnt[N],siz[N],val[N],root,tot,f[N],ch[N][2];
int check (int x) {
if (ch[f[x]][1]==x) return 1;
else return 0;
}
void pushup (int x) {
siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+cnt[x];
}
void rotate (int x) {
int y=f[x],z=f[y],k=check(x),w=ch[x][k^1];
ch[y][k]=w,f[w]=y;
ch[z][check(y)]=x,f[x]=z;
ch[x][k^1]=y,f[y]=x;
pushup(y),pushup(x);
return ;
}
void splay (int x,int goal=0) {
while (f[x]!=goal) {
int y=f[x],z=f[y];
if (z!=goal) {
if (check(x)==check(y)) rotate(y);
else rotate(x);
}
rotate(x);
}
if (!goal) root=x;
return ;
}
void find (int x) {
if (root==0) return ;
int cur=root;
while (ch[cur][x>val[cur]]&&x!=val[cur]) {
cur=ch[cur][x>val[cur]];
}
splay(cur);
return ;
}
void insert (int x) {
int cur=root,p=0;
while (cur&&val[cur]!=x) {
p=cur;
cur=ch[cur][x>val[cur]];
}
if (cur) {
cnt[cur]++;
}else {
cur=++tot;
if (p) ch[p][x>val[p]]=cur;
ch[cur][0]=ch[cur][1]=0;
val[cur]=x,f[cur]=p;
siz[cur]=cnt[cur]=1;
}
splay(cur);
return ;
}
int findxth (int x) {
int cur=root;
while (1) {
if (ch[cur][0]&&x<=siz[ch[cur][0]]) {
cur=ch[cur][0];
}else if (x>siz[ch[cur][0]]+cnt[cur]) {
x-=siz[ch[cur][0]]+cnt[cur];
cur=ch[cur][1];
}else{
return cur;
}
}
}
signed main () {
scanf("%lld%lld",&n,&m);
for (int i=1;i<=n;i++) {
int x;
scanf("%lld",&x);
insert(x);
}
while (m--) {
int op,x;
scanf("%lld%lld",&op,&x);
if (op==1) {
printf("%lld",val[findxth(tot-x+1)]);
puts("");
}else{
insert(x);
}
}
return 0;
}