3224: Tyvj 1728 普通平衡树
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2150 Solved: 864
[ Submit][ Status]
Description
您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)
Input
第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号(1<=opt<=6)
Output
对于操作3,4,5,6每行输出一个数,表示对应答案
Sample Input
10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
Sample Output
106465
84185
492737
84185
492737
HINT
1.n的数据范围:n<=100000
2.每个数的数据范围:[-1e7,1e7]
Source
以权值为关键字的splay模板题。
有一点要注意的是题目中说getpre和getnext要求找到与w不相同的前驱或后继!!!
我们可以先假定后插入的数若与已插入的数值相同,看作他大于已插入的数。
(1)因此找前驱应当先插入w,(若有等于w的数)把等于w的数中最“小”的那一个旋到根结点,再找前驱;
(2)找后继时插入w(根据之前的假定,这个w是所有与w相同的数中最“大”的),所以他和后继一定与他不相同,而在Insert()中已经把他旋到根结点了,直接找后继即可
最后记得把插入的w删掉。
(被查找的w不一定在树中出现过)
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
struct splay
{
int data,l,r,fa,size;
}a[100005];
int q,root=0,tot=0;
void Push_up(int x)
{
a[x].size=a[a[x].l].size+a[a[x].r].size+1;
}
void zig(int x)
{
int y=a[x].fa;
int z=a[y].fa;
a[y].fa=x,a[x].fa=z;
a[y].l=a[x].r,a[a[x].r].fa=y,a[x].r=y;
if (y==a[z].l) a[z].l=x;
else a[z].r=x;
Push_up(y);
}
void zag(int x)
{
int y=a[x].fa;
int z=a[y].fa;
a[y].fa=x,a[x].fa=z;
a[y].r=a[x].l,a[a[x].l].fa=y,a[x].l=y;
if (y==a[z].l) a[z].l=x;
else a[z].r=x;
Push_up(y);
}
void splay(int x,int s)
{
while (a[x].fa!=s)
{
int y=a[x].fa;
int z=a[y].fa;
if (z==s)
{
if (x==a[y].l) zig(x);
else zag(x);
break;
}
if (y==a[z].l)
{
if (x==a[y].l) zig(y),zig(x);
else zag(x),zig(x);
}
else
{
if (x==a[y].r) zag(y),zag(x);
else zig(x),zag(x);
}
}
Push_up(x);
if (s==0) root=x;
}
int Search(int w)
{
int p,x=root;
while (x)
{
p=x;
if (a[x].data>w) x=a[x].l;
else x=a[x].r;
}
return p;
}
void New_Node(int &x,int fa,int key)
{
x=++tot;
a[x].l=a[x].r=0;
a[x].fa=fa;
a[x].data=key;
}
void Insert(int w)
{
if (root==0)
{
New_Node(root,0,w);
return;
}
int i=Search(w);
if (w<a[i].data) New_Node(a[i].l,i,w);
else New_Node(a[i].r,i,w); //插入与之前相同的数看作比之前的数大
splay(tot,0);
}
int Get(int w) //找等于w的数中最“小”的那一个
{
int x=root,ans=tot+1;
while (x)
{
if (a[x].data>w) {x=a[x].l;continue;}
if (a[x].data<w) {x=a[x].r;continue;}
if (a[x].data==w)
{
ans=x;
x=a[x].l;
}
}
if (ans==tot+1) return -1; //没找到
return ans;
}
int Getmax(int x)
{
while (a[x].r)
x=a[x].r;
return x;
}
int Getmin(int x)
{
while (a[x].l)
x=a[x].l;
return x;
}
int Getpre(int x) //找x的前一个位置的数(可相同)
{
return Getmax(a[root].l);
}
int Getne(int x) //找x后一个位置得数(可相同)
{
return Getmin(a[root].r);
}
void Delet(int w)
{
int x=Get(w);
splay(x,0);
int pp=Getpre(x),nn=Getne(x);
splay(pp,0);
splay(nn,root);
int y=a[x].fa;
a[x].fa=0;
if (x==a[y].l) a[y].l=0;
else a[x].l=0;
Push_up(y);Push_up(root);
}
int Find(int w)
{
int x=Get(w);
splay(x,0);
return a[a[x].l].size;
}
int Findkth(int x,int k)
{
int s=a[a[x].l].size;
if (k==s+1) return a[x].data;
if (s>=k) return Findkth(a[x].l,k);
else return Findkth(a[x].r,k-s-1);
}
int getpre(int w) //找小于w的最大的数(不能相同)
{
int y=Get(w);
Insert(w);
if (y!=-1) splay(y,0);
int ans=Getmax(a[root].l);
Delet(w);
return a[ans].data;
}
int getne(int w) //找大于w的最小的数(不能相同)
{
Insert(w); //因为认为当前插入的w是与w相同中最“大”的一个,所以他的后一个数一定不等于w
int ans=Getmin(a[root].r);
Delet(w);
return a[ans].data;
}
int main()
{
root=tot=0;
Insert(-50000000);Insert(50000000);
scanf("%d",&q);
while (q--)
{
int x,k;
scanf("%d%d",&x,&k);
if (x==1) Insert(k);
else if (x==2) Delet(k);
else if (x==3) printf("%d\n",Find(k));
else if (x==4) printf("%d\n",Findkth(root,k+1));
else if (x==5) printf("%d\n",getpre(k));
else if (x==6) printf("%d\n",getne(k));
}
return 0;
}