BZOJ[3224] Tyvj 1728 普通平衡树
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3224
Treap模板
代码如下:
#include<algorithm>
#include<cstdlib>
#include<ctype.h>
#include<cstdio>
#define INF 2147483647
using namespace std;
const int Root=19260817;
inline int read(){
int x=0,f=1;char c;
do c=getchar(),f=c=='-'?-1:f; while(!isdigit(c));
do x=(x<<3)+(x<<1)+c-'0',c=getchar(); while(isdigit(c));
return x*f;
}
struct Node{
Node *ch[2];
int cnt,siz;
int x,pri;
Node(int);
inline void maintain(){
siz=ch[0]->siz+ch[1]->siz+cnt;
}
inline int cmp(int k){
if(x==k) return -1;
return k<x?0:1;
}
}*null,*root;
Node::Node(int _):x(_){
pri=rand();
siz=cnt=(null?1:0);
ch[0]=ch[1]=null;
}
int n,x,y;
inline void Rotate(Node*& x,int d){
Node *k=x->ch[d^1];
x->ch[d^1]=k->ch[d];
k->ch[d]=x;
x->maintain();k->maintain();
x=k;
}
void Insert(Node *&x,int k){
if(x==null){
x=new Node(k);
return;
}
int d=x->cmp(k);
if(!~d) ++x->siz,++x->cnt;
else{
Insert(x->ch[d],k); x->maintain();
if(x->ch[d]->pri > x->pri) Rotate(x,d^1);
}
}
void Delete(Node *&x,int k){
int d=x->cmp(k);
if(~d){
Delete(x->ch[d],k);
x->maintain();
return;
}
else{
if(x->cnt>1) x->cnt--;
else{
int p=x->ch[0]==null?1:(x->ch[1]==null?0:-1);
if(!~p){
p=x->ch[0]->pri > x->ch[1]->pri ? 1 : 0;
Rotate(x,p);Delete(x->ch[p],k);
}
else{
Node* cach=x;
x=x->ch[p];
delete cach;
}
}
x->maintain();
}
}
int _Rank(Node *x,int k){
int d=x->cmp(k);
if(!~d) return x->ch[0]->siz+1;
return _Rank(x->ch[d],k)+(d==1 ? x->ch[0]->siz+x->cnt : 0);
}
int K_th(Node *x,int k){
if(x==null) return 0;
if(k>x->ch[0]->siz && k<=x->ch[0]->siz+x->cnt) return x->x;
int d= k<=x->ch[0]->siz ? 0 : 1;
return K_th(x->ch[d],k-(d ? x->cnt+x->ch[0]->siz : 0));
}
int Lower(Node* x,int k){
if(x==null) return -INF;
if(k<=x->x) return Lower(x->ch[0],k);
return max(x->x,Lower(x->ch[1],k));
}
int Upper(Node* x,int k){
if(x==null) return INF;
if(k>=x->x) return Upper(x->ch[1],k);
return min(x->x,Upper(x->ch[0],k));
}
int main(){
null=new Node(0);
null->ch[0]=null->ch[1]=null;
null->pri=INF;
root=null;
srand(Root);
n=read();
for(int i=1;i<=n;i++){
x=read();y=read();
switch(x){
case 1: Insert(root,y); break;
case 2: Delete(root,y); break;
case 3: printf("%d\n",_Rank(root,y)); break;
case 4: printf("%d\n",K_th(root,y)); break;
case 5: printf("%d\n",Lower(root,y)); break;
case 6: printf("%d\n",Upper(root,y)); break;
}
}
return 0;
}