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 provided by IBM Romania, and using modern information technologies. As usual, each client of the bank is identified by a positive integer K and, upon arriving to the bank for some services, he or she receives a positive integer priority P. One of the inventions of the young managers of the bank shocked the software engineer of the serving system. They proposed to break the tradition by sometimes calling the serving desk with the lowest priority instead of that with the highest priority. Thus, the system will receive the following types of request:

0 The system needs to stop serving
K P Add client K to the waiting list with priority P
2 Serve the client with the highest priority and drop him or her from the waiting list
3 Serve the client with the lowest priority and drop him or her from the waiting list

Your task is to help the software engineer of the bank by writing a program to implement the requested serving policy.

Input

Each line of the input contains one of the possible requests; only the last line contains the stop-request (code 0). You may assume that when there is a request to include a new client in the list (code 1), there is no other request in the list of the same client or with the same priority. An identifier K is always less than 106, and a priority P is less than 107. The client may arrive for being served multiple times, and each time may obtain a different priority.

Output

For each request with code 2 or 3, the program has to print, in a separate line of the standard output, the identifier of the served client. If the request arrives when the waiting list is empty, then the program prints zero (0) to the output.

Sample Input

  1. 2
  2. 1 20 14
  3. 1 30 3
  4. 2
  5. 1 10 99
  6. 3
  7. 2
  8. 2
  9. 0

Sample Output

  1. 0
  2. 20
  3. 30
  4. 10
  5. 0

题目链接:POJ 3481

看评论区好像有一种叫双端堆的数据结构可以搞定这题,然而还是不会还是用Treap吧,因为Treap本身是一颗BST,因此一直往左找可以找到最小值和其id,一直往右找可以找到最大值和其id,然后按照其对应的优先值删除一下就好了。

代码:

  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstdlib>
  5. #include <sstream>
  6. #include <numeric>
  7. #include <cstring>
  8. #include <bitset>
  9. #include <string>
  10. #include <deque>
  11. #include <stack>
  12. #include <cmath>
  13. #include <queue>
  14. #include <set>
  15. #include <map>
  16. using namespace std;
  17. #define INF 0x3f3f3f3f
  18. #define LC(x) (x<<1)
  19. #define RC(x) ((x<<1)+1)
  20. #define MID(x,y) ((x+y)>>1)
  21. #define fin(name) freopen(name,"r",stdin)
  22. #define fout(name) freopen(name,"w",stdout)
  23. #define CLR(arr,val) memset(arr,val,sizeof(arr))
  24. #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
  25. typedef pair<int, int> pii;
  26. typedef long long LL;
  27. const double PI = acos(-1.0);
  28. const int N = 1e6 + 7;
  29. struct Treap
  30. {
  31. int ls, rs, w, v, id, sz;
  32. int rnd;
  33. };
  34. Treap T[N];
  35. int rt, tot;
  36.  
  37. void init()
  38. {
  39. rt = tot = 0;
  40. }
  41. void pushup(int k)
  42. {
  43. T[k].sz = T[T[k].ls].sz + T[T[k].rs].sz;
  44. }
  45. void lturn(int &k)
  46. {
  47. int rs = T[k].rs;
  48. T[k].rs = T[rs].ls;
  49. T[rs].ls = k;
  50. T[rs].sz = T[k].sz;
  51. pushup(k);
  52. k = rs;
  53. }
  54. void rturn(int &k)
  55. {
  56. int ls = T[k].ls;
  57. T[k].ls = T[ls].rs;
  58. T[ls].rs = k;
  59. T[ls].sz = T[k].sz;
  60. pushup(k);
  61. k = ls;
  62. }
  63. void ins(int &k, int v, int id)
  64. {
  65. if (!k)
  66. {
  67. k = ++tot;
  68. T[k].ls = T[k].rs = 0;
  69. T[k].id = id;
  70. T[k].rnd = rand();
  71. T[k].v = v;
  72. T[k].w = 1;
  73. T[k].sz = 1;
  74. }
  75. else
  76. {
  77. ++T[k].sz;
  78. if (v == T[k].v)
  79. ++T[k].w;
  80. else if (v < T[k].v)
  81. {
  82. ins(T[k].ls, v, id);
  83. if (T[T[k].ls].rnd < T[k].rnd)
  84. rturn(k);
  85. }
  86. else
  87. {
  88. ins(T[k].rs, v, id);
  89. if (T[T[k].rs].rnd < T[k].rnd)
  90. lturn(k);
  91. }
  92. }
  93. }
  94. void del(int &k, int v)
  95. {
  96. if (!k)
  97. return ;
  98. if (v == T[k].v)
  99. {
  100. if (T[k].w > 1)
  101. {
  102. --T[k].w;
  103. --T[k].sz;
  104. }
  105. else
  106. {
  107. if (T[k].ls * T[k].rs == 0)
  108. k = T[k].ls + T[k].rs;
  109. else if (T[T[k].ls].rnd < T[T[k].rs].rnd)
  110. {
  111. rturn(k);
  112. del(k, v);
  113. }
  114. else
  115. {
  116. lturn(k);
  117. del(k, v);
  118. }
  119. }
  120. }
  121. else if (v < T[k].v)
  122. {
  123. --T[k].sz;
  124. del(T[k].ls, v);
  125. }
  126. else
  127. {
  128. --T[k].sz;
  129. del(T[k].rs, v);
  130. }
  131. }
  132. int getMin(int k)
  133. {
  134. if (!k)
  135. return 0;
  136. return T[k].ls ? getMin(T[k].ls) : k;
  137. }
  138. int getMax(int k)
  139. {
  140. if (!k)
  141. return 0;
  142. return T[k].rs ? getMax(T[k].rs) : k;
  143. }
  144. int main(void)
  145. {
  146. int ops, k, p;
  147. init();
  148. srand(987321654);
  149. while (~scanf("%d", &ops) && ops)
  150. {
  151. if (ops == 1)
  152. {
  153. scanf("%d%d", &k, &p);
  154. ins(rt, p, k);
  155. }
  156. else if (ops == 2)
  157. {
  158. int indx = getMax(rt);
  159. printf("%d\n", T[indx].id);
  160. del(rt, T[indx].v);
  161. }
  162. else if (ops == 3)
  163. {
  164. int indx = getMin(rt);
  165. printf("%d\n", T[indx].id);
  166. del(rt, T[indx].v);
  167. }
  168. }
  169. return 0;
  170. }

POJ 3481 Double Queue(Treap模板题)的更多相关文章

  1. POJ 3481 Double Queue (treap模板)

    Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest ...

  2. POJ 3481 Double Queue STLmap和set新学到的一点用法

    2013-08-08 POJ 3481  Double Queue 这个题应该是STL里较简单的吧,用平衡二叉树也可以做,但是自己掌握不够- -,开始想用两个优先队列,一个从大到小,一个从小到大,可是 ...

  3. POJ 3481 Double Queue(STL)

    题意  模拟银行的排队系统  有三种操作  1-加入优先级为p 编号为k的人到队列  2-服务当前优先级最大的   3-服务当前优先级最小的  0-退出系统 能够用stl中的map   由于map本身 ...

  4. POJ 3481 Double Queue(set实现)

    Double Queue The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Buchares ...

  5. POJ 3481 Double Queue

    平衡树.. 熟悉些fhq-Treap,为啥我在poj读入优化不能用啊 #include <iostream> #include <cstdio> #include <ct ...

  6. poj 3841 Double Queue (AVL树入门)

    /****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...

  7. POJ-3481 Double Queue,Treap树和set花式水过!

                                                    Double Queue 本打算学二叉树,单纯的二叉树感觉也就那几种遍历了, 无意中看到了这个题,然后就 ...

  8. POJ 3068 运送危险化学品 最小费用流 模板题

    "Shortest" pair of paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1215 ...

  9. POJ1442-查询第K大-Treap模板题

    模板题,以后要学splay,大概看一下treap就好了. #include <cstdio> #include <algorithm> #include <cstring ...

随机推荐

  1. 六、react添加多个className报错解决方法

    例如<div className={style.calss1,style.class2}></div> 该方法会报错 想得到最终渲染的结果:<div class='cla ...

  2. 2018.6.16 PHP小实验

    PHP实验 实验一 <?php /** * Created by PhpStorm. * User: qichunlin * Date: 2018/5/17 * Time: 下午5:35 */ ...

  3. python_43_移动文件指针补充

    #移动文件指针补充 ''' 文件对象.seek((offset,where)) offset:移动的偏移量,单位为字节.等于正数时向文件尾方向移动,等于负数时向文件头方向移动文件指针 where:指针 ...

  4. pymysql 简单操作数据库

    #!/usr/bin/env python #-*- coding:utf-8 -*- # author:leo # datetime:2019/4/24 15:22 # software: PyCh ...

  5. install cmake,install torch7

    cmake http://blog.csdn.net/jesse__zhong/article/details/21290675 torch7 http://wanghaitao8118.blog.1 ...

  6. 操作系统(4)_进程同步_李善平ppt

    生产者进程count++是它的临界区,消费者count--是它的临界区. 经典同步问题,死锁问题,略.

  7. 1042: [HAOI2008]硬币购物

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3209  Solved: 2001[Submit][Status][Discuss] Descript ...

  8. Spring Boot 前世今生

    Spring Boot 2.0 的推出又激起了一阵学习 Spring Boot 热,就单从我个人的博客的访问量大幅增加就可以感受到大家对学习 Spring Boot 的热情,那么在这么多人热衷于学习 ...

  9. springboot中加入druid对sql进行监控

    springboot作为现在十分流行的框架,简化Spring应用的初始搭建以及开发过程,现在我们就使用springboot来进行简单的web项目搭建并对项目sql进行监控. 项目的搭建就省略了,spr ...

  10. 背景透明度处理 兼容IE

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...