原来是照着动态的模板写的,有几个地方还真不太好改,在这儿记录一下免得下次又纠结半天……
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<time.h>
using namespace std;
int t;
int n;
struct ntype
{
int num; //值
int fix;//修正值
int left, right;//左右指针
};
ntype node[500010];
int p_node=0;
int ans=0;
int root=0; //root这么开
void debug(int p) //这个debug函数似乎还挺好用的
{
if(p==0)
return;
cout<<node[p].num<<" left: "<<node[node[p].left].num<<" right: "<<node[node[p].right].num<<endl;
debug(node[p].left);
debug(node[p].right);
}
void init()
{
memset(node,0,sizeof(ntype)*500010);
p_node=0;
ans=0;
root=0;
return;
}
void l_rotate(int &p)
{
int y=node[p].right;
node[p].right= node[y].left;
node[y].left=p;
p=y;
}
void r_rotate(int &p)
{
int y=node[p].left;
node[p].left=node[y].right;
node[y].right=p;
p=y;
}
void add(int &p, int x)
{
if(p==0)
{
p_node++;
p=p_node;
node[p].num=x;
node[p].left = node[p].right = 0;
node[p].fix=rand()*rand();
}
else if(x < node[p].num)
{
add(node[p].left, x);
if(node [ node[p].left ] . fix < node[p]. fix)
r_rotate(p);
}
else
{
add(node[p].right, x);
if(node [ node[p].right ] . fix < node[p]. fix )
l_rotate(p);
}
}
void del(int &p, int x) //连查带删,必须,不能指定一个删,否则它爸爸会不知道它已经被删了
{
if(p==0)
return;
if(x<node[p].num)
del(node[p].left, x);
else if(x>node[p].num)
del(node[p].right, x);
else if(node[p].left==0 || node[p].right==0) // 一定要有else,或者在上面就return,不然一高兴整棵树就没了
{
int y=p;
p= (node[p].left !=0)? node[p].left : node[p].right;
node[y].num= node[y].left=node[y].right=0;
}
else
{
if(node [ node[p].left ].fix < node[node[p].right] .fix)
{
r_rotate(p);
del(node[p].right, x);
}
else
{
l_rotate(p);
del(node[p].left, x);
}
}
}
int srch(int p, int nowbest, int x) //这是用于找 最接近x但是不比x大的数 用的
{
if(p==0)
{
if(nowbest!=0)
{
del(root, node[nowbest].num);
}
return nowbest;
}
if(node[p].num<=x)
return srch(node[p].right, p , x);
else
return srch(node[p].left, nowbest, x);
}
int main()
{
scanf("%d", &t);
srand(unsigned(time(0)));
//新建:add(root,x);
//找前驱:srch(root,0,x);
system("pause");
return 0;
}