平衡树肯定无锅,毕竟刚刚A了板子就将std复制了一下
求助!
#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
const int MAXN=100010;
int n,root,a[MAXN],tot,m;
struct Splay_Tree
{
int father,ch[2],key,cnt,siz;
};
Splay_Tree tree[MAXN];
int lc(int p){return tree[p].ch[0];}
int rc(int p){return tree[p].ch[1];}
void update(int x)
{
if(x)
{
tree[x].siz=tree[x].cnt;
if(lc(x)) tree[x].siz+=tree[lc(x)].siz;
if(rc(x)) tree[x].siz+=tree[rc(x)].siz;
}
return;
}
void rotate(int x)
{
int father=tree[x].father;
int grandfather=tree[father].father;
int k=rc(father)==x;
tree[father].ch[k]=tree[x].ch[k^1];
tree[tree[x].ch[k^1]].father=father;
tree[x].ch[k^1]=father;
tree[father].father=x;
if(grandfather) tree[grandfather].ch[rc(grandfather)==father]=x;
tree[x].father=grandfather;
update(father),update(x);
return;
}
void splay(int x)
{
int father=tree[x].father;
int grandfather=tree[father].father;
while(father)
{
if(grandfather)
{
if((rc(grandfather)==father)^(rc(father)==x)) rotate(x);
else rotate(father);
}
rotate(x);
father=tree[x].father;
grandfather=tree[father].father;
}
root=x;
return;
}
void insert(int x)
{
if(!root)
{
root=++tot;
tree[root].father=tree[root].ch[0]=tree[root].ch[1]=0;
tree[root].siz=tree[root].cnt=1;
tree[root].key=x;
return;
}
int now=root,lst=0;
while(true)
{
if(tree[now].key==x)
{
tree[now].cnt++;
update(now);
update(lst);
splay(now);
return;
}
lst=now;
now=tree[lst].ch[x>tree[lst].key];
if(!now)
{
tot++;
tree[lst].ch[x>tree[lst].key]=tot;
tree[tot].father=lst;tree[tot].ch[0]=tree[tot].ch[1]=0;
tree[tot].key=x;
tree[tot].cnt=tree[tot].siz=1;
update(lst);
splay(tot);
return;
}
}
}
int find(int x)
{
int ans=1,now=root;
while(now)
{
if(x<tree[now].key) now=lc(now);
else
{
if(lc(now)) ans+=tree[lc(now)].siz;
if(x==tree[now].key)
{
splay(now);
return ans;
}
ans+=tree[now].cnt;
now=rc(now);
}
}
return ans;
}
int find_kth(int x)
{
int now=root;
while(true)
{
if(lc(now)&&x<=tree[lc(now)].siz) now=lc(now);
else
{
if(lc(now)) x-=tree[lc(now)].siz;
if(x<=tree[now].cnt)
{
splay(now);
return now;
}
x-=tree[now].cnt;
now=rc(now);
}
}
return false;
}
int pre()
{
int now=lc(root);
while(rc(now)) now=rc(now);
return now;
}
int nxt()
{
int now=rc(root);
while(lc(now)) now=lc(now);
return now;
}
void del(int x)
{
find(x);
if(tree[root].cnt>1)
{
tree[root].cnt--;
return;
}
if(!lc(root)&&!rc(root))
{
root=0;
return;
}
if(!lc(root))
{
root=rc(root);
tree[root].father=0;
return;
}
if(!rc(root))
{
root=lc(root);
tree[root].father=0;
return;
}
int last_root=root;
splay(pre());
tree[root].ch[1]=rc(last_root);
tree[rc(last_root)].father=root;
update(root);
return;
}
int main()
{
scanf("%d",&n);
for(register int i=1,x;i<=n;i++)
{
scanf("%d",&x);
insert(x);
}
scanf("%d",&m);
for(register int i=1;i<=m;i++)
{
char opt[5];
scanf("%s",opt);
if(opt[0]=='m')
{
if(n%2==1) printf("%d\n",find_kth(n/2+1));
else printf("%d\n",find_kth(n>>1));
}
else
{
int x;
scanf("%d",&x);
insert(x);
n++;
}
}
return 0;
}
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/