# bzoj 3224,tyvj 1728普通平衡树

#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<string>
#include<cctype>
#include<cmath>
#include<vector>
const int N = 1e5 + 7,INF = 0x7fffffff;
#define Ran1221() ((rand() << 13) + (rand() << 6) + rand ())
#define Ran12() (Ran1221() + Ran1221() + Ran1221() + 1)

class Treap {
private :
struct Node {
Node *son[2];
int size,num,data,hr;
Node () {}
Node (int data,Node *fl) : data(data) { size = num = 1; son[0] = son[1] = fl; hr = Ran12(); }
void update () { size = son[0] -> size + son[1] -> size + num; }
}*pool,*Null,*root,meme[N];

int ball;

void rotate (Node *&T,bool v) {
Node *Tt = T -> son[v];
T -> son[v] = Tt -> son[v^1];
Tt -> son[v^1] = T;
T -> update(); Tt -> update();
T = Tt;
}

int Precursor (Node *&T) {
if(T == Null) return T -> data;
if(T -> data >= ball) return Precursor (T -> son[0]);
int k = Precursor (T -> son[1]);
if(k == Null -> data) return T -> data;
return k;
}

int Subsequent (Node *&T) {
if(T == Null) return T -> data;
if(T -> data <= ball) return Subsequent (T -> son[1]);
int k = Subsequent (T -> son[0]);
if(k == Null -> data) return T -> data;
return k;
}

void Print (Node *&T) {
if(T == Null) return ;
Print (T -> son[0]);
for(int i=1;i<=T->num;++i) printf("%d ",T -> data);
Print (T -> son[1]);
}

void Insert (Node *&T) {
if(T == Null) { T = new (pool++) Node (ball,Null); return; }
if(T -> data == ball) {
++ T -> num; ++ T -> size; return ;
}
int v = ball > T -> data;
Insert (T -> son[v]);
if(T -> son[v] -> hr < T -> hr) rotate (T,v);
else T -> update();
}

int sum;

void Rank (Node *&T) {
if(T == Null) return ;
if(T -> data > ball) Rank (T -> son[0]);
else {
sum += T -> son[0] -> size;
if(T -> data < ball) sum += T -> num , Rank (T -> son[1]);
}
}

void Delete (Node *&T) {
if(T == Null) return ;
if(T -> data == ball) {
if(T -> num > 1) {
-- T -> num;
-- T -> size;
return ;
}
if(T -> son[0] == Null || T -> son[1] == Null) {
int v = T -> son[1] != Null;
T = T -> son[v];
return ;
}
int v = T -> son[1] -> hr < T -> son[0] -> hr;
rotate (T,v);
Delete (T -> son[v^1]);
} else {
int v = ball > T -> data;
Delete (T -> son[v]);
}
T -> update();
}

int TheNo (Node *&T) {
if(T == Null) return 0;
if(ball <= T -> son[0] -> size) return TheNo (T -> son[0]);
if(ball <= T -> son[0] -> size + T -> num) return T -> data;
ball -= T -> son[0] -> size + T -> num; return TheNo (T -> son[1]);
}

public :
Treap () { Null = new Node (); Null -> size = Null -> num = 0; Null -> data = -INF;}
void Pri () { Print(root); }
void clear () { pool = meme; root = Null; }
void Ins (int xxx) { ball = xxx; Insert(root); }
int Ran (int xxx) { ball = xxx; sum = 0; Rank (root); return sum; }
void Del (int xxx) { ball = xxx; Delete(root); }
int The (int xxx) { ball = xxx; return TheNo(root); }
int Pre (int xxx) { ball = xxx; return Precursor(root);}
int Sub (int xxx) { ball = xxx; return Subsequent(root); }

}treap;

int opt,w,Q;

int main () {
freopen("3224.in","r",stdin);
freopen("3224.out","w",stdout);
scanf("%d",&Q); treap.clear();
while (Q--) {
scanf("%d%d",&opt,&w);
if(opt == 1) treap . Ins(w);
if(opt == 2) treap . Del(w);
if(opt == 3) printf("%d\n",treap . Ran(w) + 1);
if(opt == 4) printf("%d\n",treap . The(w));
if(opt == 5) printf("%d\n",treap . Pre(w));
if(opt == 6) printf("%d\n",treap . Sub(w));
}
return 0;
}

That is all ， thank you for watching.

posted @ 2016-03-22 15:21  DraZxlnDdt  阅读(170)  评论(0编辑  收藏  举报