Topic Link: Click to open the link
Dynamic Inquiry K Small, only insert and query two operations, the first balance tree. Memorial (Sad, no delete operation, the subject is not required). The main is not discretized with line tree will not write. Two days of desperate knocks Treap
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < string> #include <cctype> #include <vector> #include <cstdio> #include <cmath> #include < queue> #include <stack> #include <map> #include <set> #define MAXN 1005#define _ll __int64#define ll Long long#define INF 0x3f3f3f3f#define Mod 1<<40+10#define pp pair<int,int> #define ull unsigned long longusin G namespace Std;int n;struct node{node *ch[2];int r,v,s;node () {}node (int v) {ch[0]=null;ch[1]=null;r=rand (); this->v =v;s=1;} BOOL operator < (const node& c) Const{return R<C.R;} int cmp (int x) const {if (X==V) Return-1;return x<v?0:1;} void maintain () {s=1;if (ch[0]!=null) s+=ch[0]->s;if (ch[1]!=null) s+=ch[1]->s;}}; void Rotate (node* &o,int D) {node *k=o->ch[d^1];o->ch[d^1]=k->ch[d];k->ch[d]=o;o->maintain (); k >maintain (); o=k;} void Insert (node* &o,int x) {if (O==null) o=new node (x); Else{int D=o-> CMP (x); insert (o->ch[d],x); if (o->ch[d]->r>o->r) rotate (o,d^1);} O->maintain ();} BOOL Find (node* O,int x) {while (o!=null) {int d=o->cmp (x); if (d==-1) return 1;else o=o->ch[d];} return 0;} int find_kth (node* o,int k) {if (o==null| | K>o->s) Return-1;int s= (O->ch[0]==null?0:o->ch[0]->s), if (k==s+1) return O->v;else if (k<=s) Return find_kth (o->ch[0],k), Else return find_kth (o->ch[1],k-s-1); void Solve () {node *root=null;char op[2];int x;while (n--) {scanf ("%s%d", op,&x), if (op[0]== ' P ') {if (Find (root,x)) Continue;insert (root,x);} elseprintf ("%d\n", Find_kth (Root,x));}} int main () {while (~SCANF ("%d", &n)) solve (); return 0;}
Sdut 3043-The Fan container (treap k decimal)