20161111
BZOJ3224 General Balance Tree |treap
Bzoj 3224//by cydiater//2016.11.11#include <iostream> #include <cstring> #include <string># Include <algorithm> #include <queue> #include <map> #include <ctime> #include <cmath># Include <cstdlib> #include <cstdio> #include <iomanip> #include <bitset> #include <set> using namespace std; #define LL Long long#define up (i,j,n) for (int. i=j;i<=n;i++) #define DOWN (i,j,n) for (int i=j;i>=n ; i--) #define Cmax (A, B) A=max (A, b) #define Cmin (A, B) A=min (A, b) const int MAXN=1E5+5;CONST int Oo=0x3f3f3f3f;inline int Read () {char ch=getchar (); int x=0,f=1;while (ch> ' 9 ' | | ch< ' 0 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} int n,root=0,opt,tol=0;struct tree{int leftt,rightt,rnd,v,siz,cnt;} T[maxn];namespace solution{inline void Updata (int k) {t[k].siz=t[t[k].leftt].siz+t[t[k].rightt].siz+t[k].cnt;} void Lefturn (int &k) {int tmp=t[k].rightt;t[k].rightt=t[tmp].leftt;t[tmp].leftt=K;t[tmp].siz=t[k].siz;updata (k); k=tmp;} void Righturn (int &k) {int Tmp=t[k].leftt;t[k].leftt=t[tmp].rightt;t[tmp].rightt=k;t[tmp].siz=t[k].siz;updata ( k); k=tmp;} void insert (int &k,int num) {if (k==0) {K=++tol;t[tol].leftt=t[tol].rightt=0;t[tol].rnd=rand (); t[tol].v=num;t[ Tol].siz=t[tol].cnt=1;return;} T[k].siz++;if (NUM==T[K].V) T[k].cnt++;else if (NUM>T[K].V) {insert (t[k].rightt,num); if (t[t[k].rightt].rnd<t[ K].rnd) Lefturn (k);} Else{insert (T[k].leftt,num); if (T[t[k].leftt].rnd<t[k].rnd) Righturn (k);}} void del (int &k,int num) {if (k==0) return;if (t[k].v==num) {if (t[k].cnt>1) {T[k].cnt--;t[k].siz--;return;} if (t[k].leftt*t[k].rightt==0) K=t[k].leftt+t[k].rightt;else if (t[t[k].rightt].rnd>t[t[k].leftt].rnd) {Lefturn ( k);d El (K,num);} Else{righturn (k);d El (K,num);}} else if (NUM>T[K].V) {t[k].siz--;d El (t[k].rightt,num);} else{t[k].siz--;d El (T[k].leftt,num);}} int getrank (int k,int num) {if (t[k].v==num) return 1+t[t[k].leftt].siz;else if (NUM>T[K].V) return t[t[k].leftt].siz+ T[k].cnt+getrank (t[K].rightt,num]; else return GetRank (t[k].leftt,num);} int getnum (int k,int rnk) {if (rnk>t[t[k].leftt].siz+t[k].cnt) return Getnum (t[k].rightt,rnk-(t[t[k].leftt].siz+t[ K].CNT)); else if (Rnk>t[t[k].leftt].siz) return t[k].v;else return Getnum (T[K].LEFTT,RNK);} int pre (int k,int num) {if (k==0) return-1;if (T[k].v>=num) return pre (t[k].leftt,num); else return Max (t[k].rightt , num), T[K].V);} int NXT (int k,int num) {if (k==0) return oo;if (T[k].v<=num) return NXT (T[k].rightt,num), Else return min (NXT (T[K].LEFTT , num), T[K].V);} void Slove () {root=0; N=read (); while (n--) {opt=read (); int num=read (); if (opt==1) insert (root,num); if (opt==2) del (root,num); if (opt==3) printf ("%d\n", GetRank (Root,num)), if (opt==4) printf ("%d\n", Getnum (Root,num)), if (opt==5) printf ("%d\n", Pre (root, num)); if (opt==6) printf ("%d\n", NXT (Root,num));}}} int main () {//freopen ("input.in", "R", stdin),//freopen ("Out.out", "w", stdout), using namespace Solution;slove (); return 0;}
Template Review Plan