POJ 3481 Double Queue(Treap模板题)
| 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 |
| 1 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
2
1 20 14
1 30 3
2
1 10 99
3
2
2
0
Sample Output
0
20
30
10
0
题目链接:POJ 3481
看评论区好像有一种叫双端堆的数据结构可以搞定这题,然而还是不会还是用Treap吧,因为Treap本身是一颗BST,因此一直往左找可以找到最小值和其id,一直往右找可以找到最大值和其id,然后按照其对应的优先值删除一下就好了。
代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1e6 + 7;
struct Treap
{
int ls, rs, w, v, id, sz;
int rnd;
};
Treap T[N];
int rt, tot; void init()
{
rt = tot = 0;
}
void pushup(int k)
{
T[k].sz = T[T[k].ls].sz + T[T[k].rs].sz;
}
void lturn(int &k)
{
int rs = T[k].rs;
T[k].rs = T[rs].ls;
T[rs].ls = k;
T[rs].sz = T[k].sz;
pushup(k);
k = rs;
}
void rturn(int &k)
{
int ls = T[k].ls;
T[k].ls = T[ls].rs;
T[ls].rs = k;
T[ls].sz = T[k].sz;
pushup(k);
k = ls;
}
void ins(int &k, int v, int id)
{
if (!k)
{
k = ++tot;
T[k].ls = T[k].rs = 0;
T[k].id = id;
T[k].rnd = rand();
T[k].v = v;
T[k].w = 1;
T[k].sz = 1;
}
else
{
++T[k].sz;
if (v == T[k].v)
++T[k].w;
else if (v < T[k].v)
{
ins(T[k].ls, v, id);
if (T[T[k].ls].rnd < T[k].rnd)
rturn(k);
}
else
{
ins(T[k].rs, v, id);
if (T[T[k].rs].rnd < T[k].rnd)
lturn(k);
}
}
}
void del(int &k, int v)
{
if (!k)
return ;
if (v == T[k].v)
{
if (T[k].w > 1)
{
--T[k].w;
--T[k].sz;
}
else
{
if (T[k].ls * T[k].rs == 0)
k = T[k].ls + T[k].rs;
else if (T[T[k].ls].rnd < T[T[k].rs].rnd)
{
rturn(k);
del(k, v);
}
else
{
lturn(k);
del(k, v);
}
}
}
else if (v < T[k].v)
{
--T[k].sz;
del(T[k].ls, v);
}
else
{
--T[k].sz;
del(T[k].rs, v);
}
}
int getMin(int k)
{
if (!k)
return 0;
return T[k].ls ? getMin(T[k].ls) : k;
}
int getMax(int k)
{
if (!k)
return 0;
return T[k].rs ? getMax(T[k].rs) : k;
}
int main(void)
{
int ops, k, p;
init();
srand(987321654);
while (~scanf("%d", &ops) && ops)
{
if (ops == 1)
{
scanf("%d%d", &k, &p);
ins(rt, p, k);
}
else if (ops == 2)
{
int indx = getMax(rt);
printf("%d\n", T[indx].id);
del(rt, T[indx].v);
}
else if (ops == 3)
{
int indx = getMin(rt);
printf("%d\n", T[indx].id);
del(rt, T[indx].v);
}
}
return 0;
}
POJ 3481 Double Queue(Treap模板题)的更多相关文章
- POJ 3481 Double Queue (treap模板)
Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest ...
- POJ 3481 Double Queue STLmap和set新学到的一点用法
2013-08-08 POJ 3481 Double Queue 这个题应该是STL里较简单的吧,用平衡二叉树也可以做,但是自己掌握不够- -,开始想用两个优先队列,一个从大到小,一个从小到大,可是 ...
- POJ 3481 Double Queue(STL)
题意 模拟银行的排队系统 有三种操作 1-加入优先级为p 编号为k的人到队列 2-服务当前优先级最大的 3-服务当前优先级最小的 0-退出系统 能够用stl中的map 由于map本身 ...
- POJ 3481 Double Queue(set实现)
Double Queue The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Buchares ...
- POJ 3481 Double Queue
平衡树.. 熟悉些fhq-Treap,为啥我在poj读入优化不能用啊 #include <iostream> #include <cstdio> #include <ct ...
- poj 3841 Double Queue (AVL树入门)
/****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...
- POJ-3481 Double Queue,Treap树和set花式水过!
Double Queue 本打算学二叉树,单纯的二叉树感觉也就那几种遍历了, 无意中看到了这个题,然后就 ...
- POJ 3068 运送危险化学品 最小费用流 模板题
"Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1215 ...
- POJ1442-查询第K大-Treap模板题
模板题,以后要学splay,大概看一下treap就好了. #include <cstdio> #include <algorithm> #include <cstring ...
随机推荐
- 【CF739E】Gosha is hunting(WQS二分套WQS二分)
点此看题面 大致题意: 你有两种捕捉球(分别为\(A\)个和\(B\)个),要捕捉\(n\)个神奇宝贝,第\(i\)个神奇宝贝被第一种球捕捉的概率是\(s1_i\),被第二种球捕捉的概率是\(s2_i ...
- Java 发送邮件工具类
1. Mail.java package util; import java.util.Date; import java.util.Properties; import javax.mail.Au ...
- Webpack机制、原理简单小结
一.webpack的构成 entry 代表项目的入口 module 开发中,每一个文件可以看作一个module chunk 代码块 loader 模块转化器 plugin 扩展插件,自定义w ...
- PAT 乙级 1015
题目 题目地址:PAT 乙级 1015 题解 常规题,难点在于理清楚排序规则,通过比较简洁的方式进行编码: 在这里我选择使用vector进行存储,并使用sort方法排序,因为本题不是简单按照大小排序, ...
- CentOS Linux 安装IPSec+L2TP
第二层隧道协议L2TP(Layer 2 Tunneling Protocol)是一种工业标准的Internet隧道协议,它使用UDP的1701端口进行通信.L2TP本身并没有任何加密,但是我们可以使用 ...
- formpanel布局的学习
FormPanel有两种布局:form和column,form是纵向布局,column为横向布局.默认为后者.使用layout属性定义布局类型.对于一个复杂的布局表单,最重要的是正确分割,分割结果直接 ...
- windows环境下安装npm、cnpm、bower
什么是npm.cnpm.bower? 简单地说,就是帮你下载好你需要的css或者js库,而且三者功能也都是一样的.那为什么要下载这3个不同的呢?据说npm容易被墙……而cnpm是淘宝的镜像,所以通常用 ...
- 力扣题目汇总(丑数,重复N的元素,求众数)
丑数 1.题目描述 编写一个程序判断给定的数是否为丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例 1: 输入: 6 输出: true 解释: 6 = 2 × 3 示例 2: 输入: 8 ...
- 安装 ubuntu 后,使用 sed 更换国内源
cd /etc/aptsed -i "s/archive.ubuntu.com/mirrors.aliyun.com/g" /etc/apt/sources.list也可以使用 1 ...
- 南阳 ACM16 矩形嵌套 动态规划
矩形嵌套 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c, ...