POJ 3481 Double Queue splay

题意:3个炒作,1 插入一个(值,优先级) 2  找优先级最大的输出值并删除  3,找优先值最小的输出值并删除。

解题思路:splay

解题代码:

  1 // File Name: poj3481.cpp
  2 // Author: darkdream
  3 // Created Time: 2015年04月09日 星期四 14时41分43秒
  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 #define maxn 100005
 26 using namespace std;
 27
 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     inline void Rotate(int x, int f){
 35         int y = pre[x];
 36         push_down(y);
 37         push_down(x);
 38         ch[y][!f] = ch[x][f];
 39         pre[ ch[x][f] ] = y  ;
 40         pre[x] = pre[y];
 41         if( pre[x] ) ch[ pre[y] ][ ch[pre[y]][1] == y ] = x;
 42         ch[x][f] = y ;
 43         pre[y] = x;
 44         push_up(y);
 45     }
 46     inline void Splay(int x, int goal){
 47         push_down(x);
 48         while(pre[x] != goal){
 49             if(pre[pre[x]] == goal){
 50                 Rotate(x,ch[pre[x]][0] == x);
 51             }else{
 52                 int y = pre[x],z = pre[y];
 53                 int f = (ch[z][0] == y );
 54                 if(ch[y][f] == x){
 55                     Rotate(x,!f),Rotate(x,f);
 56                 }else{
 57                     Rotate(y,f),Rotate(x,f);
 58                 }
 59             }
 60         }
 61         push_up(x);
 62         if(goal == 0) root = x;
 63     }
 64     inline void RotateTo(int k ,int goal){
 65         int x = root ;
 66         push_down(x);
 67         while(sz[ch[x][0]] != k){
 68             if(k < sz[ch[x][0]]){
 69                 x = ch[x][0];
 70             }else{
 71                 k -= (sz[ch[x][0]] + 1);
 72                 x = ch[x][1];
 73             }
 74             push_down(x);
 75         }
 76         Splay(x,goal);
 77     }
 78     inline void erase(int x)
 79     {
 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 key ,int val){
 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
 96         vals[x] = val;
 97         keys[x] = key ;
 98     }
 99
100     void init(){
101         ch[0][0] = ch[0][1] = sz[0] = pre[0] = 0 ;
102         root = top1 =0 ;
103         NewNode(root,0,0);
104         NewNode(ch[root][1],1e9,0);
105         sz[root] = 2;
106         pre[ch[root][1]] = root;
107     }
108     void push_up(int x){
109         sz[x] = 1 + sz[ch[x][0]] + sz[ch[x][1]];
110     }
111     void push_down(int x){
112
113     }
114     void insert(int key,int val){
115          int x = root ;
116          for(;;){
117              int f = (key > keys[x]);
118              if(ch[x][f] == 0 ){
119                  NewNode(ch[x][f],key,val);
120                  pre[ch[x][f]] = x;
121                  Splay(ch[x][f],0);
122                  return ;
123              }else{
124                 x = ch[x][f];
125              }
126          }
127     }
128     void Del(int rank){
129         //printf("%d***\n",rank);
130         RotateTo(rank-1,0);
131         RotateTo(rank+1,root);
132     //    printf("%d %d\n",keys[root],keys[ch[root][1]]);
133         printf("%d\n",vals[ch[ch[root][1]][0]]);
134         erase(ch[ch[root][1]][0]);
135         push_up(root);
136     //    printf("****");
137     }
138     int keys[maxn];
139     int vals[maxn];
140 }spt;
141 int n ;
142 int key,val;
143 int main(){
144       spt.init();
145       while(scanf("%d",&n) != EOF && n )
146       {
147          int sz = spt.sz[spt.root];
148          if(n == 2 )
149          {
150             if(sz> 2){
151                 spt.Del(sz-2);
152             }else{
153               printf("0\n");
154             }
155          }else if(n == 3){
156             if(sz> 2){
157                 spt.Del(1);
158             }else{
159               printf("0\n");
160             }
161          }else{
162             scanf("%d %d",&val,&key);
163             spt.insert(key,val);
164          }
165     //     sz = spt.sz[spt.root];
166     //     printf("%d\n",sz);
167       }
168 return 0;
169 }

时间: 2024-10-11 05:40:30

POJ 3481 Double Queue splay的相关文章

POJ 3481 Double Queue(Treap模板题)

Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15786   Accepted: 6998 Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provid

AVL树(模板题)—— POJ 3481 Double Queue

对应POJ题目:点击打开链接 Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11741   Accepted: 5335 Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing env

跳跃表基础——POJ 3481 Double Queue

对应POJ 题目:点击打开链接 Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11768   Accepted: 5349 Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing en

POJ - 3481 - Double Queue (STL)

Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11133   Accepted: 5056 Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provid

poj 3481 Double Queue STL中map的运用

题意: 维护一个集合,操作有1:加入一个元素,2:删除最大元素,3:删除最小元素. 分析: map本质是个容器,且具有第一个关键字有序的性质,所以用它来水水就好啦~ 代码: //poj 3481 //sep9 #include <iostream> #include <map> using namespace std; map<int,int> mymap; map<int,int>::iterator iter; int main() { int x,su

POJ 3481 Double Queue(STL)

题意  模拟银行的排队系统  有三种操作  1-加入优先级为p 编号为k的人到队列  2-服务当前优先级最大的   3-服务当前优先级最小的  0-退出系统 能够用stl中的map   由于map本身就依据key的值排了序   相应2.3  我们仅仅须要输出最大或最小即可了并从map中删除该键值 #include<cstdio> #include<map> using namespace std; map<int, int> a; int main() { map<

POJ 3481 Double Queue 堆修改标记

Enemy Double Queue! 题目大意:维护一种数据结构,支持以下操作: 1.插入一个值 2.查询最大值并删除 3.查询最小值并删除 元素的值<=1000W 这数据结构一看就是堆...不过堆结构不能同时维护最大值和最小值,于是我们开两个堆,一个大根堆,一个小根堆 其中一堆删除时,另一堆也要删除相应元素 于是删除的话有两种方法 1.映射 1000W开数组映射妥妥MLE 于是我们在两个堆之间互相映射 不太好写 pair里开自己的指针会报错,于是只能开了void* 不过速度还是很可观的 #i

POJ 3481 Double Queue(set实现)

Double Queue The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by IBM Romania, and using modern information technologies. As usual, each client of the bank

POJ 题目3481 Double Queue(SBT ro map)

Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11824   Accepted: 5385 Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provid