[POJ3481]Double Queue
Problem
0 结束操作
1 K P 将一个数K以优先级P加入
2 取出优先级最高的那个数
3 取出优先级最低的那个数
Solution
Splay模板题
Notice
是输出数而不是输出优先级。
Code
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 1000000;
const double eps = 1e-6, phi = acos(-1.0);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
int point = 0, root;
struct node
{
int val[N + 5], son[2][N + 5], parent[N + 5], label[N + 5];
void Rotate(int x, int &rt)
{
int y = parent[x], z = parent[y];
int l = (son[1][y] == x), r = 1 - l;
if (y == rt) rt = x;
else if (son[0][z] == y) son[0][z] = x;
else son[1][z] = x;
parent[x] = z;
parent[son[r][x]] = y, son[l][y] = son[r][x];
parent[y] = x, son[r][x] = y;
}
void Splay(int x, int &rt)
{
while (x != rt)
{
int y = parent[x], z = parent[y];
if (y != rt)
{
if ((son[0][z] == y) ^ (son[0][y] == x))
Rotate(x, rt);
else Rotate(y, rt);
}
Rotate(x, rt);
}
}
void Insert(int &u, int x, int y, int last)
{
if (u == 0)
{
u = ++point;
val[u] = y, parent[u] = last, label[u] = x;
Splay(u, root);
}
else
{
if (y > val[u]) Insert(son[1][u], x, y, u);
else if (y < val[u]) Insert(son[0][u], x, y, u);
}
}
void Delete(int x)
{
Splay(x, root);
if (son[0][x] * son[1][x] == 0) root = son[0][x] + son[1][x];
else
{
int t = son[1][x];
while (son[0][t] != 0) t = son[0][t];
Splay(t, root);
son[0][t] = son[0][x], parent[son[0][x]] = t;
}
parent[root] = 0;
}
int Find_max()
{
int t = root;
while (son[1][t] != 0) t = son[1][t];
return t;
}
int Find_min()
{
int t = root;
while (son[0][t] != 0) t = son[0][t];
return t;
}
}Splay_tree;
int main()
{
int H_H;
while (~scanf("%d", &H_H) && H_H)
{
int x, y;
switch (H_H)
{
case 1:
x = read(), y = read();
Splay_tree.Insert(root, x, y, 0);
break;
case 2:
x = Splay_tree.Find_max();
printf("%d\n", Splay_tree.label[x]);
Splay_tree.Delete(x);
break;
case 3:
y = Splay_tree.Find_min();
printf("%d\n", Splay_tree.label[y]);
Splay_tree.Delete(y);
break;
}
}
}
[POJ3481]Double Queue的更多相关文章
- POJ-3481 Double Queue,Treap树和set花式水过!
Double Queue 本打算学二叉树,单纯的二叉树感觉也就那几种遍历了, 无意中看到了这个题,然后就 ...
- POJ-3481 Double Queue (splay)
The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped w ...
- poj 3841 Double Queue (AVL树入门)
/****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...
- hdu 1908 Double Queue
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1908 Double Queue Description The new founded Balkan ...
- POJ 3481 Double Queue STLmap和set新学到的一点用法
2013-08-08 POJ 3481 Double Queue 这个题应该是STL里较简单的吧,用平衡二叉树也可以做,但是自己掌握不够- -,开始想用两个优先队列,一个从大到小,一个从小到大,可是 ...
- 【Map】Double Queue
Double Queue Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13258 Accepted: 5974 Des ...
- 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(Treap模板题)
Double Queue Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15786 Accepted: 6998 Des ...
随机推荐
- x1c 2017 安装mint18的坑——grub2
折腾一天,死活安装不上.用U盘安装,能进入pe,但是安装时提示无法将grub2安装到/target/ 不论如何分区.如何修改BIOS 安全启动和 启动模式.都是这个问题. ubuntu16.04.3 ...
- Codeforces 920G - List Of Integers
920G - List Of Integers 思路:容斥+二分 代码: #include<bits/stdc++.h> using namespace std; #define ll l ...
- Unity---关于游戏小包的记录
最近因为需求,出了一个pc版的游戏小包,遇到一些坑,在此做一下记录. 首先需要明白的是出小包的意义所在,其实就是为了压缩包体,游戏需要的大部分资源,在第一次运行游戏的时候通过热更新去FTP资源服务器上 ...
- 获取解码字符串指定位置的数值 Decoded String at Index
2018-10-04 12:53:06 问题描述: 问题求解: 首先本题给出了问题的规模,从Note中我们可以看到解码后的字符串长度甚至可以达到2^63的长度,这个长度已经远远超过整型数的范围,因此如 ...
- 【消息队列】从各方面比较下kafka、activemq、rabbitmq、rocketmq之间的区别
一.单机吞吐量ActiveMQ:万级,吞吐量比RocketMQ和Kafka要低了一个数量级RabbitMQ:万级,吞吐量比RocketMQ和Kafka要低了一个数量级RocketMQ:10万级,Roc ...
- MVC,MVVM,MVP的区别/ Vue中忽略的知识点!
按照顺序学习: https://scotch.io/courses/build-an-online-shop-with-vue/hello-world Vue Authentication And R ...
- java,sort函数的深刻理解
先来看看题目吧: 链接:https://www.nowcoder.com/questionTerminal/97b6a49a85944650b2e3d0660b91c324来源:牛客网 [编程题]德才 ...
- kernel_thread简析
1.3.100static inline pid_t kernel_thread(int (*fn)(void *), void * arg, unsigned long flags){ lon ...
- 『C++』STL容器入门
最近在学习opencv,因为C++基础很烂,所以遇到了不少问题,其中STL模块也是没少接触,特此简单了解一下STL的容器类型(主要是Vector)和迭代器的简单用法. C++ STL(标准模板库)是一 ...
- TSFDEVTY
TSFDEVTY 用BAPI_OUTB_DELIVERY_CONFIRM_DEC做的发货过账,会VL09无法冲销需要UPDATE LIKP-VLSTK为空UPDATE likp SET vlstk = ...