bzoj 1012 BST 支持插入,区间最大

 

水。。。

  1 /**************************************************************
  2     Problem: 1012
  3     User: idy002
  4     Language: C++
  5     Result: Accepted
  6     Time:1372 ms
  7     Memory:5960 kb
  8 ****************************************************************/
  9  
 10 #include <cstdio>
 11 #include <iostream>
 12 #define fprintf(...)
 13 #define oo 0x3f3f3f3f
 14 #define maxn 200010
 15 using namespace std;
 16  
 17 namespace S {
 18     int pre[maxn], son[maxn][2], siz[maxn], val[maxn], mv[maxn], root, ntot;
 19  
 20     void update( int nd ) {
 21         siz[nd] = siz[son[nd][0]]+siz[son[nd][1]]+1;
 22         mv[nd] = max( val[nd], max( mv[son[nd][0]], mv[son[nd][1]] ) );
 23     }
 24     void rotate( int nd, int d ) {
 25         int p = pre[nd];
 26         int s = son[nd][!d];
 27         int ss = son[s][d];
 28  
 29         son[nd][!d] = ss;
 30         son[s][d] = nd;
 31         if( p ) son[p][ nd==son[p][1] ] = s;
 32         else root = s;
 33  
 34         pre[s] = p;
 35         pre[nd] = s;
 36         if( ss ) pre[ss] = nd;
 37  
 38         update( nd );
 39         update( s );
 40     }
 41     void splay( int nd, int top=0 ) {
 42         while( pre[nd]!=top ) {
 43             int p = pre[nd];
 44             int nl = nd==son[p][0];
 45             if( pre[p]==top ) {
 46                 rotate( p, nl );
 47             } else {
 48                 int pp = pre[p];
 49                 int pl = p==son[pp][0];
 50                 if( nl==pl ) {
 51                     rotate( pp, pl );
 52                     rotate( p, nl );
 53                 } else {
 54                     rotate( p, nl );
 55                     rotate( pp, pl );
 56                 }
 57             }
 58         }
 59     }
 60     int newnode( int p, int v ) {
 61         int nd = ++ntot;
 62         pre[nd] = p;
 63         son[nd][0] = son[nd][1] = 0;
 64         siz[nd] = 1;
 65         mv[nd] = val[nd] = v;
 66         return nd;
 67     }
 68     int find( int pos ) {
 69         int nd = root;
 70         while( 1 ) {
 71             int ls = siz[son[nd][0]];
 72             if( pos<=ls ) {
 73                 nd = son[nd][0];
 74             } else if( pos>=ls+2 ) {
 75                 nd = son[nd][1];
 76                 pos -= ls+1;
 77             } else return nd;
 78         }
 79     }
 80     void init() {
 81         root = ntot = 0;
 82         val[0] = mv[0] = -oo;
 83     }
 84     void add_val( int v ) {
 85         fprintf( stderr, "add_val( %d )\n", v );
 86         if( !root ) {
 87             root = newnode( 0, v );
 88             return;
 89         }
 90         int nd = root;
 91         while( son[nd][1] ) nd=son[nd][1];
 92         son[nd][1] = newnode( nd, v );
 93         splay( son[nd][1] );
 94     }
 95     int qu_max( int pos ) {
 96         fprintf( stderr, "qu_max( %d )\n", pos );
 97         int nd = find(pos);
 98         splay( nd );
 99         return max( val[nd], mv[son[nd][1]] );
100     }
101 };
102  
103 int main() {
104     int T, mod;
105     int lastans = 0;
106  
107     scanf( "%d%d", &T, &mod );
108     S::init();
109     while( T-- ) {
110         char opt[10];
111         int v;
112         scanf( "%s%d", opt, &v );
113         if( opt[0]=='A' ) {
114             v += lastans;
115             v %= mod;
116             S::add_val( v );
117         } else {
118             printf( "%d\n", lastans=S::qu_max( S::siz[S::root]-v+1 ) );
119         }
120     }
121 }
View Code

 

posted @ 2015-02-19 15:34  idy002  阅读(222)  评论(0编辑  收藏  举报