RT
我调了好久
应该是 update 的位置
或者 splay 操作写错了
形成的树不满足二叉树的性质
求救
Code:
#include<queue>
#include<ctime>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
namespace FastIO
{
bool sign;
char c;
template<class T>
inline void read(T &x)
{
x=0;
sign=false;
for(c=getchar();c<'0'||c>'9';c=getchar())
if(c=='-')
sign=true;
for(;c>='0'&&c<='9';c=getchar())
x=(x<<1)+(x<<3)+(c&15);
x=(sign) ? ~x+1 : x;
return;
}
char bcc[36];
int top=0;
template<class T>
inline void print(T x)
{
top=0;
if(!x) {
putchar('0');
return;
}
if(x<0) putchar('-'),x=~x+1;
for(;x>0;x=x/10)
bcc[++top]=x%10+48;
for(;top>=1;top--)
putchar(bcc[top]);
return;
}
}
using FastIO::read;
using FastIO::print;
const int INF=0x7fffffff-1;
const int N=2e6+5;
struct node
{
int val;
int father;
int size,cnt;
int child[2];
#define v(x) sp[x].val
#define fa(x) sp[x].father
#define s(x) sp[x].size
#define c(x) sp[x].cnt
#define ch(x,y) sp[x].child[y]
};
node sp[N];
int root,cnt;
#define ident(x) (ch(fa(x),1)==x)
#define mix(fat,now,s) { ch((fat),(s))=(now); fa((now))=(fat);}
#define update(now) {s(now)=s(ch(now,0))+s(ch(now,1))+c(now);}
inline void rotate(const int &now)
{
int k=ident(now),ff=fa(now);
mix(ff,ch(now,k^1),k);
mix(fa(ff),now,ident(ff));
mix(now,ff,k^1);
update(ch(now,k^1));
update(now);
return;
}
inline void splay(int now,int top) //代表把x转到top的儿子,top为0则转到根结点
{
int x,y;
while(fa(now)!=top) {
x=fa(now); y=fa(x);
if(y!=top) (ident(x)^ident(y)) ? rotate(now) : rotate(x);
rotate(now);
}
if(!top) root=now,s(0)=0;
return;
}
inline void find(const int &key)
{
int now=root;
while(now>0&&v(now)!=key&&ch(now,(key>v(now)))!=0)
now=ch(now,(key>v(now)));
splay(now,0);
return;
}
inline int newnode(const int &key,const int &fa)
{
cnt++;
v(cnt)=key;
fa(cnt)=fa;
s(cnt)=1;
c(cnt)=1;
ch(cnt,1)=ch(cnt,0)=0;
return cnt;
}
inline void insert(int &now,const int &key,const int &fa)
{
if(!now) {
now=newnode(key,fa);
splay(now,0);
return;
}
if(key==v(now)) {
c(now)++;
splay(now,0);
return;
}
else insert(ch(now,(key>v(now))),key,now);
update(now);
return;
}
inline void erase(const int &key)
{
find(key);
if(c(root)>1) { c(root)--; return; }
if(ch(root,1)) {
int now=ch(root,1);
while(ch(now,0)>0)
now=ch(now,0);
splay(now,root);
mix(ch(root,1),ch(root,0),0);
root=ch(root,0);
fa(now)=0;
update(root);
}
else root=ch(root,0),fa(root)=0;
return;
}
inline int getpre(const int &key)
{
find(key);
if(v(root)<key)
return v(root);
int now=ch(root,0);
if(now==0) return -INF;
while(ch(now,1))
now=ch(now,1);
splay(now,0);
return v(now);
}
inline int getsuf(const int &key)
{
find(key);
if(v(root)>key)
return v(root);
int now=ch(root,1);
if(now==0) return INF;
while(ch(now,0))
now=ch(now,0);
splay(now,0);
return v(now);
}
inline int getrank(const int &key)
{
getpre(key);
return (v(root)==key) ? s(ch(root,0))+1 : s(ch(root,0))+c(root)+1;
}
inline int getnum(int rank)
{
int now=root;
while(now>0&&rank>0) {
if(s(ch(now,0))>rank&&s(ch(now,0))+c(now)<=rank) {
splay(now,0);
break;
}
if(s(ch(now,0))+c(now)>rank)
now=ch(now,0);
else rank-=s(ch(now,0))+c(now),now=ch(now,1);
}
return v(root);
}
int n;
int main()
{
// freopen("bst.in","r",stdin);
// freopen("bst.out","w",stdout);
read(n);
int opt,x;
for(int i=1;i<=n;i++) {
read(opt); read(x);
switch(opt) {
case 1 : insert(root,x,0); break;
case 2 : erase(x); break;
case 3 : print(getrank(x)); break;
case 4 : print(getnum(x)); break;
case 5 : print(getpre(x)); break;
case 6 : print(getsuf(x)); break;
}
if(opt>=3&&opt<=6)
puts("");
}
return 0;
}
有一组数据
10
1 1
1 13
1 2
6 2
4 2
3 2
1 15
1 12
6 12
2 15
正确输出
13
2
2
13
我的输出
13
13
2
13
大佬救救我吧!