码迷,mamicode.com
首页 > 其他好文 > 详细

HDU1754 I Hate It splay

时间:2015-04-03 23:39:20      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

题意:单等更新,区间求最值。

解题思路:splay 。

解题代码:

技术分享
  1 // File Name: hdu1754.cpp
  2 // Author: darkdream
  3 // Created Time: 2015年04月03日 星期五 22时06分59秒
  4 
  5 #include<vector>
  6 #include<list>
  7 #include<map>
  8 #include<set>
  9 #include<deque>
 10 #include<stack>
 11 #include<bitset>
 12 #include<algorithm>
 13 #include<functional>
 14 #include<numeric>
 15 #include<utility>
 16 #include<sstream>
 17 #include<iostream>
 18 #include<iomanip>
 19 #include<cstdio>
 20 #include<cmath>
 21 #include<cstdlib>
 22 #include<cstring>
 23 #include<ctime>
 24 #define LL long long
 25 
 26 using namespace std;
 27 const int maxn = 200005;
 28 struct SplayTree{
 29     int sz[maxn];
 30     int ch[maxn][2];
 31     int pre[maxn];
 32     int root ,top1,top2;
 33     int ss[maxn],que[maxn];
 34     
 35     inline void Rotate(int x,int f){
 36         int y = pre[x];
 37         push_down(y);
 38         push_down(x);
 39         ch[y][!f] = ch[x][f];
 40         pre[ch[x][f]] = y ; 
 41         pre[x] = pre[y];
 42         if(pre[x]) ch[pre[y]][ch[pre[y]][1] == y] = x; 
 43         ch[x][f] = y ; 
 44         pre[y] = x ; 
 45         push_up(y);
 46     }
 47     inline void Splay(int x, int goal){
 48          push_down(x);
 49          while(pre[x] != goal){
 50             if(pre[pre[x]] == goal){
 51                 Rotate(x, ch[pre[x]][0] == x);
 52             }else{
 53                int y = pre[x],z = pre[y];
 54                int f = (ch[z][0] == y);
 55                if(ch[y][f] == x){
 56                     Rotate(x,!f) , Rotate(x,f);
 57                }else{
 58                     Rotate(y,f) , Rotate(x,f);
 59                }
 60             }
 61          }
 62          push_up(x);
 63          if(goal ==  0) root = x; 
 64     }
 65     inline void RotateTo(int k ,int goal){
 66         int x = root;
 67         push_down(x);
 68         while(sz[ch[x][0]] != k ){
 69             if(k < sz[ ch[x][0] ]){
 70                 x = ch[x][0];
 71             }else{
 72                 k -= (sz[ch[x][0]] + 1);
 73                 x = ch[x][1];
 74             }
 75             push_down(x);
 76         }
 77         Splay(x,goal);
 78     }
 79     inline void erase(int x){
 80         int father = pre[x];
 81         int head = 0 , tail = 0 ; 
 82         for(que[tail ++] = x ; head < tail ;head ++){
 83             ss[top2++] = que[head];
 84             if(ch[que[head] ][0]) que[tail ++] = ch[que[head]][0];
 85             if(ch[que[head] ][1]) que[tail ++] = ch[que[head]][1];
 86         }
 87         ch[father][ch[father][1] == x] = 0 ;
 88         push_up(father);
 89     }
 90     inline void NewNode(int &x,int c){
 91          if(top2) x = ss[--top2];
 92          else x = ++top1;
 93          ch[x][0] = ch[x][1] = pre[x] = 0 ; 
 94          sz[x] = 1; 
 95          val[x] = mx[x] = c;
 96     }
 97     inline void push_down(int x){
 98 
 99     }
100     inline void push_up(int x){
101         sz[x] = 1 + sz[ch[x][0]] + sz[ch[x][1]]; 
102         mx[x] = max(mx[ch[x][0]],val[x]);
103         mx[x] = max(mx[ch[x][1]],mx[x]);    
104     }
105     inline void makeTree(int &x,int l ,int r,int f){
106         if(l > r) return ; 
107         int m = (l + r ) >> 1; 
108         NewNode(x,num[m]);
109         makeTree(ch[x][0],l,m-1,x);
110         makeTree(ch[x][1],m+1,r,x);
111         pre[x] = f;
112         push_up(x);
113     }
114     inline void init(int n){
115         ch[0][0] = ch[0][1] = pre[0] = sz[0] = 0 ; 
116         mx[0] = 0 ; 
117         root = top1 = 0 ; 
118         NewNode(root,-1);
119         NewNode(ch[root][1],-1);
120         pre[top1] = root;
121         sz[root] = 2; 
122     //    printf("%d***\n",mx[root]);
123 
124         for(int i =  1 ;i <= n;i ++) scanf("%d",&num[i]);
125         makeTree(ch[ch[root][1]][0],1,n,ch[root][1]);
126         push_up(ch[root][1]);
127         push_up(root);
128         
129     //    printf("%d***\n",mx[root]);
130 
131     }
132     inline void update(int l,int v){
133       RotateTo(l,0);
134       val[root] = v;
135     //  push_up(root);
136     }
137     inline void query(int l , int r){
138        RotateTo(l-1,0);
139        RotateTo(r+1,root);
140        printf("%d\n",mx[ch[ch[root][1]][0]]);
141     } 
142     int num[maxn];
143     int mx[maxn];
144     int val[maxn];
145 }spt;
146 char str[10];
147 int ta,tb;
148 int n , q; 
149 int main(){
150     while(scanf("%d %d",&n,&q) != EOF)
151     {
152         spt.init(n);
153         for(int i = 1;i <= q; i++)
154         {
155           scanf("%s %d %d",str,&ta,&tb);
156           if(str[0] == Q)
157           {
158             spt.query(ta,tb);
159           }else{
160             spt.update(ta,tb);
161           }
162         }
163     }
164 return 0;
165 }
View Code

 

HDU1754 I Hate It splay

标签:

原文地址:http://www.cnblogs.com/zyue/p/4391197.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!