[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 ...
随机推荐
- yum update软件包冲突
因升级系统过程中断,重新进入系统继续升级出现如下问题(内容太多,只粘部分内容) # yum update 正在解决依赖关系 --> 正在检查事务 ---> 软件包 alsa-firmwar ...
- AtCoder Grand Contest 025 B - RGB Coloring
B - RGB Coloring 求ax + by = k (0<=x<=n && 0<=y<=n)的方案数,最后乘上C(n, x)*C(n,y) 代码: #i ...
- nodejs导出excel
//导出Excel var nodeExcel = require("excel-export"); var fs = require("fs"); var c ...
- python中的break\return\pass\continue用法
continue: def func(): for i in range(1,11): if i % 2 == 0: continue # 作用是当符合上面的if判语句后,就直接跳过之后的语句,也就是 ...
- django-simple-captcha 验证码插件介绍 django-simple-captcha 使用 以及添加动态ajax刷新验证
django-simple-captcha作为一款django的验证码插件,使用方法非常简单,能够快速应用到web应用中. 文档官网地址:django-simple-captcha 参考博客:http ...
- 十分钟带你理解Kubernetes核心概念
什么是Kubernetes? Kubernetes(k8s)是自动化容器操作的开源平台,这些操作包括部署,调度和节点集群间扩展.如果你曾经用过Docker容器技术部署容器,那么可以将Docker看成K ...
- Getting Started with Processing 第五章的总结
Getting Started with Processing 第五章:响应 一次与永久 setup()函数 Processing 中,setup()函数只运行一次,用于设置一些初始的值,比如画布的大 ...
- 解释变量(Explanatory Variable)
转自:http://www.statisticshowto.com/explanatory-variable/ What is an Explanatory Variable? An explanat ...
- 静默安装/ 普通安装与root权限获取相关
静默安装 有时候使用第三方的插件时我们需要静默安装其提供的apk包,静默安装时我们需要获取root权限,如下代码 Process process = Runtime.getRuntime().exec ...
- drf 认证、权限、限流、过滤、排序、分页器
认证Authentication 准备工作:(需要结合权限用) 1. 需要使用到登陆功能,所以我们使用django内置admin站点并创建一个管理员. python manage.py creates ...