为什么连样例都过不去?
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct st
{
ll l,r,key,val,cnt,size;
};
const ll N=100010,INF=0x3f3f3f3f3f3f3f3f;
ll n,m,a[N],op,x,tmp,root,idx;
st treap[N];
void pushup(ll u)
{
treap[u].size=treap[treap[u].l].size+treap[treap[u].r].size+treap[u].cnt;
}
ll get(ll key)
{
idx++;
treap[idx].key=key;
treap[idx].val=rand();
treap[idx].cnt=treap[idx].size=1;
return idx;
}
void build()
{
get(-INF);
get(INF);
root=1;
treap[1].r=2;
pushup(root);
}
void zig(ll &u)
{
tmp=treap[u].l;
treap[u].l=treap[tmp].r;
treap[tmp].r=u;
u=tmp;
pushup(treap[u].r);
pushup(u);
}
void zag(ll &u)
{
tmp=treap[u].r;
treap[u].r=treap[tmp].l;
treap[tmp].l=u;
u=tmp;
pushup(treap[u].l);
pushup(u);
}
void insert(ll &u,ll key)
{
if(!u)
{
u=get(key);
}
else if(treap[u].key==key)
{
treap[u].cnt++;
}
else if(treap[u].key>key)
{
insert(treap[u].l,key);
if(treap[treap[u].l].val>treap[u].val)
{
zig(u);
}
}
else
{
insert(treap[u].r,key);
if(treap[treap[u].r].val>treap[u].val)
{
zag(u);
}
}
pushup(u);
}
void remove(ll &u,ll key)
{
if(!u)
{
return;
}
if(treap[u].key==key)
{
if(treap[u].cnt>1)
{
treap[u].cnt--;
}
else if(treap[u].l||treap[u].r)
{
if(!treap[u].r||treap[treap[u].l].val>treap[treap[u].r].val)
{
zig(u);
remove(treap[u].r,key);
}
else
{
zag(u);
remove(treap[u].l,key);
}
}
else
{
u=0;
}
}
else if(treap[u].key>key)
{
remove(treap[u].l,key);
}
else
{
remove(treap[u].r,key);
}
pushup(u);
}
ll get_rank_by_key(ll u,ll key)
{
if(!u)
{
return 0;
}
if(treap[u].key==key)
{
return treap[treap[u].l].size+1;
}
if(treap[u].key>key)
{
return get_rank_by_key(treap[u].l,key);
}
return treap[treap[u].l].size+treap[u].cnt+get_rank_by_key(treap[u].r,key);
}
ll get_key_by_rank(ll u,ll rank)
{
if(!u)
{
return INF;
}
if(treap[treap[u].l].size>=rank)
{
return get_key_by_rank(treap[u].l,rank);
}
if(treap[treap[u].l].size+treap[u].cnt>=rank)
{
return treap[u].key;
}
return get_key_by_rank(treap[u].r,rank-treap[treap[u].l].size-treap[u].cnt);
}
ll get_prev(ll u,ll key)
{
if(!u)
{
return -INF;
}
if(treap[u].key>key)
{
return get_prev(treap[u].l,key);
}
return max(treap[u].key,get_prev(treap[u].r,key));
}
ll get_next(ll u,ll key)
{
if(!u)x
{
return INF;
}
if(treap[u].key<key)
{
return get_next(treap[u].r,key);
}
return min(treap[u].key,get_next(treap[u].l,key));
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
build();
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
insert(root,a[i]);
}
while(m--)
{
cin>>op>>x;
if(op==1)
{
cout<<get_rank_by_key(root,x)<<"\n";
}
else
{
insert(root,x);
}
}
return 0;
}