Hihocoder 1325 (splay)

Problem 平衡树 Treap

题目大意

  维护一个数列,支持两种操作。

    操作1:添加一个数x。

    操作2:询问不超过x的最大的数。

解题分析

  splay 模板题。

参考程序

  1 #include <map>
  2 #include <set>
  3 #include <stack>
  4 #include <queue>
  5 #include <cmath>
  6 #include <ctime>
  7 #include <string>
  8 #include <vector>
  9 #include <cstdio>
 10 #include <cstdlib>
 11 #include <cstring>
 12 #include <iostream>
 13 #include <algorithm>
 14 #pragma comment(linker,"/STACK:102400000,102400000")
 15 using namespace std;
 16 
 17 #define     N             100008             
 18 #define     V             1008
 19 #define        E            60008    
 20 #define        lson        l,m,rt<<1
 21 #define     rson        m,r+1,rt<<1|1
 22 #define        clr(x,v)    memset(x,v,sizeof(x));
 23 #define        LL            long long 
 24 
 25 const int    mo    =    1000000007;
 26 const int     inf =    0x3f3f3f3f;
 27 const int     INF =    2000000000;
 28 /**************************************************************************/ 
 29 int Q;
 30 int ls[N],rs[N],v[N],fa[N],size[N],root,tmp=0;
 31 
 32 void pushup(int x){
 33     size[x]=size[ls[x]]+size[rs[x]]+1;
 34 }
 35 void left(int x,int &rt){
 36     int y=fa[x],z=fa[y];
 37     if (rt==y) rt=x;
 38     else if (ls[z]==y) ls[z]=x; else rs[z]=x;
 39     fa[x]=z; fa[y]=x; fa[ls[x]]=y;
 40     rs[y]=ls[x]; ls[x]=y;
 41     pushup(y); pushup(x);
 42 }
 43 void right(int x,int &rt){
 44     int y=fa[x],z=fa[y];
 45     if (rt==y) rt=x;
 46     else if (ls[z]==y) ls[z]=x; else rs[z]=x;
 47     fa[x]=z; fa[y]=x; fa[rs[x]]=y;
 48     ls[y]=rs[x]; rs[x]=y;
 49     pushup(y); pushup(x);
 50 }
 51 
 52 void spaly(int x,int &rt){
 53     while (x!=rt){
 54         int y=fa[x],z=fa[y];
 55         if (ls[y]==x){
 56             if (y!=rt&&ls[z]==y) right(y,rt);
 57             right(x,rt);
 58         }
 59         else{
 60             if (y!=rt&&rs[z]==y) left(y,rt);
 61             left(x,rt);
 62         }
 63     }
 64 }
 65 
 66 void insert(int x,int &k,int last){
 67     if (k==0){
 68         k=++tmp;
 69         fa[k]=last;
 70         v[k]=x;
 71         size[k]=1;
 72         spaly(k,root);
 73         return;
 74     }
 75     if (x<v[k]) insert(x,ls[k],k); else insert(x,rs[k],k);
 76 }
 77 
 78 void find(int x,int k){
 79     int res=-INF;
 80     while (k!=0){
 81         if (v[k]>x) k=ls[k];
 82         else{
 83             res=max(res,v[k]);
 84             k=rs[k];
 85         }
 86     }
 87     printf("%d\n",res);
 88 }
 89 
 90 void dfs(int rt){
 91     printf("k = %d , lson = %d , rson = %d ,v =  %d , size =  %d , fa = %d\n",rt,ls[rt],rs[rt],v[rt],size[rt],fa[rt] );
 92     if (ls[rt]) dfs(ls[rt]);
 93     if (rs[rt]) dfs(rs[rt]);
 94 }
 95 void debug(){
 96     printf("================================\n");
 97     printf("%d\n",root );
 98     dfs(root);
 99 }
100 
101 int main(){
102     scanf("%d",&Q);
103     root=0;
104     while (Q--){
105         char s[3];
106         int x;
107         scanf("%s %d",s,&x);
108         if (s[0]=='I') insert(x,root,0);
109             else find(x,root);
110     //    debug();
111     }
112 }
View Code

 

posted @ 2016-08-06 22:52  rpSebastian  阅读(129)  评论(0)    收藏  举报