Test instructions do not repeat, do not understand the balance of the tree's own book repair, network repair, brain repair. Brain Tonic, clam.
Paste the SBT code directly, and then take another copy of the Splaytree tle code.
SBT:
#include <cstdio> #include <cstring> #define N 2001000 using namespace std;
struct SBT {int key,cnt;
int L,R,SIZE,SC;
}s[n];
int root,n,m;
void left_rotate (int &x) {int y=s[x].r;
S[X].R=S[Y].L;
S[y].l=x;
S[y].size=s[x].size;
S[y].sc=s[x].sc;
s[x].size=s[s[x].l].size+s[s[x].r].size+1;
s[x].sc=s[s[x].l].sc+s[s[x].r].sc+s[x].cnt;
X=y;
} void right_rotate (int &x) {int y=s[x].l;
S[X].L=S[Y].R;
S[y].r=x;
S[y].size=s[x].size;
S[y].sc=s[x].sc;
s[x].size=s[s[x].l].size+s[s[x].r].size+1;
s[x].sc=s[s[x].l].sc+s[s[x].r].sc+s[x].cnt;
X=y;
} void Keep (int &x,int flag) {s[x].size=s[s[x].l].size+s[s[x].r].size+1;
s[x].sc=s[s[x].l].sc+s[s[x].r].sc+s[x].cnt;
if (!flag) {if (s[s[s[x].l].l].size>s[s[x].r].size) right_rotate (x);
else if (s[s[s[x].l].r].size>s[s[x].r].size) left_rotate (S[X].L), right_rotate (x);
else return; } else {if (S[S[S[X].R].R]. size>s[s[x].l].size) left_rotate (x);
else if (s[s[s[x].r].l].size>s[s[x].l].size) right_rotate (S[X].R), left_rotate (x);
else return;
} keep (s[x].l,0);
Keep (s[x].r,1);
Keep (x,0);
Keep (x,1);
} void Insert (int &x,int key) {if (!x) {x=++n;
S[x].size=s[x].sc=s[x].cnt=1;
S[x].key=key;
} else {if (key==s[x].key) s[x].cnt++,s[x].sc++;
else if (key<s[x].key) insert (S[x].l,key), keep (x,0);
else Insert (S[x].r,key), keep (x,1);
}} void del (int &x,int key) {if (S[x].key==key) {if (s[x].cnt>1) s[x].cnt--, s[x].sc--;
else if (!S[X].L&&!S[X].R) x=0;
else if (!S[X].L*S[X].R) X=S[X].L+S[X].R;
else if (s[s[x].l].size>s[s[x].r].size) right_rotate (x), Del (S[x].r,key), keep (x,0);
else Left_rotate (x), Del (S[x].l,key), keep (x,1);
} else if (Key<s[x].key) del (s[x].l,key), keep (x,1);
Else del (s[x].r,key), keep (x,0); } int Find (iNT &x,int k) {if (s[s[x].l].sc<k&&k<=s[s[x].l].sc+s[x].cnt) return s[x].key;
if (K<=s[s[x].l].sc) return find (S[X].L,K);
else return find (s[x].r,k-s[s[x].l].sc-s[x].cnt);
} int rank (int &x,int key) {if (S[x].key==key) return s[s[x].l].sc+1;
if (Key<s[x].key) return rank (S[x].l,key);
else return rank (s[x].r,key) +s[x].cnt+s[s[x].l].sc;
} int getmin () {int x=root;
for (; s[x].l;x=s[x].l);
return s[x].key;
} int Getmax () {int x=root;
for (; s[x].r;x=s[x].r);
return s[x].key;
} int pred (int &x,int y,int key) {if (!x) return y;
if (Key<=s[x].key) return pred (S[x].l,y,key);
else return pred (S[x].r,x,key);
} int succ (int &x,int y,int key) {if (!x) return y;
if (Key<s[x].key) return succ (S[x].l,x,key);
else return succ (S[x].r,y,key);
} int main () {int i,t,x;
scanf ("%d", &m);
for (i=1;i<=m;i++) {scanf ("%d%d", &t,&x); Switch (t) {CAsE 1:insert (root,x);
Case 2:del (ROOT,X);
Case 3:printf ("%d\n", Rank (root,x));
Case 4:printf ("%d\n", Find (root,x));
Case 5:printf ("%d\n", s[pred (root,0,x)].key);
Case 6:printf ("%d\n", S[SUCC (root,0,x)].key);
}} return 0;
}
SPT: (Thought is right.) Rotate and splay should both be right. )
#include <cstdio> #include <algorithm> #define N 201000 #define INF 0x3f3f3f3f #define RT SON[ROOT][1] #define
LRT son[rt][0] #define LS son[x][0] #define RS son[x][1] #define IS (x) (x==son[fa[x]][1]) using namespace std;
struct SPT {int root,top;
int val[n],son[n][2],size[n],num[n],fa[n];
inline void link (int &x,int y,int d) {son[y][d]=x;fa[x]=y;}
inline void pushup (int x) {size[x]=size[ls]+size[rs]+num[x];
} inline void init () {top=0;
NewNode (Root,0,-inf);
NewNode (Son[root][1],root,inf);
size[1]=size[2]=num[1]=num[2]=0;
} inline void rotate (int x) {int y=fa[x],z=fa[y],idx=is (x), Idy=is (y);
Link (son[x][!idx],y,idx);
Link (y,x,!idx);
if (z) link (x,z,idy); fa[x]=z;
Pushup (y);
Pushup (x);
} inline void splay (int x,int k=0) {int y,z;
while (fa[x]!=k) {y=fa[x],z=fa[y];
if (z==k) {rotate (x); break;}
if (Is (x) ==is (y)) rotate (y);
else rotate (x);
Rotate (x);
} if (!k) root=x; } inline int pred (int w,int k=0) {int x=root,Y
while (x) {if (w>val[x]) y=x;
x=son[x][w>val[x]];
} splay (Y,k);
return val[y];
} inline int succ (int w,int k=0) {int x=root,y;
while (x) {if (w<val[x]) y=x;
x=son[x][w>=val[x]];
} splay (Y,k);
return val[y];
} inline void NewNode (int &x,int y,int W) {x=++top;
Val[x]=w;
Fa[x]=y;
Size[x]=num[x]=1;
} inline void Insert (int w,int k=0) {int x=root;
while (Son[x][w>val[x]]) {if (w==val[x]) {num[x]++;
Splay (X,K);
return;
} x=son[x][w>val[x]];
} newnode (SON[X][W>VAL[X]],X,W);
Splay (Son[x][w>val[x]);
} inline bool Remove (int W) {pred (w); succ (W,root);
if (!LRT) return 0;
if (num[lrt]-1) num[lrt]--;
else son[rt][0]=0;
Pushup (RT);
Pushup (root);
return 1;
} inline int rank (int w,int k=0) {int x=root;
while (Son[x][w>val[x]]) {if (w==val[x]) break;
x=son[x][w>val[x]];
} splay (X,k);
return size[son[x][0]]+1; } inline int find (int rank,int k=0)
{int x=root; while (rank<=size[son[x][0]]| |
Size[son[x][0]]+num[x]<rank) {if (rank<=size[son[x][0]]) x=son[x][0];
else x=son[x][1],rank-= (size[son[x][0]]+num[x]);
} splay (X,k);
return val[x];
} inline void Dfs (int x) {if (son[x][0]) DFS (son[x][0]);
if (Val[x]>0&&val[x]<inf) for (int i=1;i<=num[x];i++) printf ("%d\n", val[x]);
if (son[x][1]) DFS (son[x][1]);
}}SPT;
int main () {//Freopen ("Test.in", "R", stdin);
int i,t,k,n;
scanf ("%d", &n);
Spt.init ();
for (i=1;i<=n;i++) {scanf ("%d", &t);
Spt.insert (t);
} spt.dfs (Spt.root);
return 0;
}
Copy to Google Translate translation results