RT,Treap求调,37pts,WA。使用了随机种子,比较玄学,第4个点有时过,有时不过。
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define PII pair<int,int>
#define DB double
#define LL long long
#define int long long
//#define int __int128
using namespace std;
inline int read(long long md){
if(md==0)md=LONG_LONG_MAX;
int 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*10%md+(ch-48)%md)%md;ch=getchar();}
return x*f;
}
struct Treap{
int ls,rs;
int val,dat;
int cnt,siz;
}t[100005];
int tot,root;
int crtnew(int x){
t[++tot].val=x,t[tot].dat=rand()%inf;
t[tot].cnt=1,t[tot].siz=1;
return tot;
}
void update(int p){
t[p].siz=t[t[p].ls].siz+t[t[p].rs].siz+t[p].cnt;
}
void build(){
crtnew(-inf),crtnew(inf);
root=1,t[1].rs=2;
update(root);
}
int query1(int val,int p){
if(p==0)return 1;
if(val==t[p].val)return t[t[p].ls].siz+1;
if(val<t[p].val)return query1(val,t[p].ls);
return t[p].cnt+t[t[p].ls].siz+query1(val,t[p].rs);
}
int query2(int rnk,int p){
if(p==0)return inf;
if(rnk<=t[t[p].ls].siz)return query2(rnk,t[p].ls);
if(rnk<=t[t[p].ls].siz+t[p].cnt)return t[p].val;
return query2(rnk-t[t[p].ls].siz-t[p].cnt,t[p].rs);
}
void zig(int &p){
int q=t[p].ls;
t[p].ls=t[q].rs;
t[q].rs=p,p=q;
update(t[p].rs),update(p);
}
void zag(int &p){
int q=t[p].rs;
t[p].rs=t[q].ls;
t[q].ls=p,p=q;
update(t[p].ls),update(p);
}
int getpre(int val){
int ans=1,p=root;
while(p){
if(val==t[p].val){
if(t[p].ls>0){
p=t[p].ls;
while(t[p].rs>0)
p=t[p].rs;
ans=p;
}
break;
}
if(t[p].val<val&&t[p].val>t[ans].val)ans=p;
if(val<t[p].val)p=t[p].ls;
else p=t[p].rs;
}
return t[ans].val;
}
int getnxt(int val){
int ans=2,p=root;
while(p){
if(val==t[p].val){
if(t[p].rs>0){
p=t[p].rs;
while(t[p].ls>0)
p=t[p].ls;
ans=p;
}
break;
}
if(t[p].val>val&&t[p].val<t[ans].val)ans=p;
if(val<t[p].val)p=t[p].ls;
else p=t[p].rs;
}
return t[ans].val;
}
void insert(int val,int &p){
if(p==0){
p=crtnew(val);
return;
}
if(val==t[p].val){
++t[p].cnt,update(p);
return;
}
if(val<t[p].val){
insert(val,t[p].ls);
if(t[p].dat<t[t[p].ls].dat)zig(p);
}
else{
insert(val,t[p].rs);
if(t[p].dat<t[t[p].rs].dat)zag(p);
}
update(p);
}
void delet(int val,int &p){
if(p==0)return;
if(val==t[p].val){
if(t[p].cnt>1){
--t[p].cnt,update(p);
return;
}
if(t[p].ls||t[p].rs){
if(!t[p].ls||t[t[p].ls].dat>t[t[p].rs].dat)
zig(p),delet(val,t[p].rs);
else
zag(p),delet(val,t[p].ls);
update(p);
}
else p=0;
return;
}
if(val<t[p].val)
delet(val,t[p].ls);
else
delet(val,t[p].rs);
update(p);
}
signed main(){
srand(time(0));
int n=read(0);
build();
while(n--){
int op=read(0),x=read(0);
if(op==1)
insert(x,root);
if(op==2)
delet(x,root);
if(op==3)
printf("%lld\n",query1(x,root)-1);
if(op==4)
printf("%lld\n",query2(x+1,root));
if(op==5)
printf("%lld\n",getpre(x));
if(op==6)
printf("%lld\n",getnxt(x));
}
return 0;
}
顺便贴组数据:
Input:
20
1 964673
5 968705
4 1
3 964673
5 965257
1 915269
1 53283
3 964673
3 53283
3 53283
1 162641
5 973984
1 948119
2 915269
2 53283
6 959161
1 531821
1 967521
2 531821
1 343410
Output:
964673
964673
1
964673
3
1
1
964673
964673
这组数据最后一个输出有时候对,有时候错,求调