[HDU4585]Shaolin
Problem
问你一个数的前驱和后继
Solution
Treap模板题
Notice
注意输出那个人的编号
Code
#include<cmath>
#include<cstdio>
#include<cstdlib>
#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 = 100001;
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, pre, suf, root;
struct node
{
int Val[N + 5], Level[N + 5], Size[N + 5], Son[2][N + 5], Label[N + 5];
inline void up(int u)
{
Size[u] = Size[Son[0][u]] + Size[Son[1][u]] + 1;
}
inline void Newnode(int &u, int v, int t)
{
u = ++point;
Level[u] = rand(), Val[u] = v, Label[u] = t;
Size[u] = 1, Son[0][u] = Son[1][u] = 0;
}
inline void Lturn(int &x)
{
int y = Son[1][x]; Son[1][x] = Son[0][y], Son[0][y] = x;
Size[y] = Size[x]; up(x); x = y;
}
inline void Rturn(int &x)
{
int y = Son[0][x]; Son[0][x] = Son[1][y], Son[1][y] = x;
Size[y] = Size[x]; up(x); x = y;
}
void Insert(int &u, int t, int tt)
{
if (u == 0)
{
Newnode(u, t, tt);
return;
}
Size[u]++;
if (t < Val[u])
{
Insert(Son[0][u], t, tt);
if (Level[Son[0][u]] < Level[u]) Rturn(u);
}
else if (t > Val[u])
{
Insert(Son[1][u], t, tt);
if (Level[Son[1][u]] < Level[u]) Lturn(u);
}
}
int Find_num(int u, int t)
{
if (!u) return 0;
if (t <= Size[Son[0][u]]) return Find_num(Son[0][u], t);
else if (t <= Size[Son[0][u]] + 1) return u;
else return Find_num(Son[1][u], t - Size[Son[0][u]] - 1);
}
void Find_pre(int u, int t)
{
if (!u) return;
if (t > Val[u])
{
pre = u;
Find_pre(Son[1][u], t);
}
else Find_pre(Son[0][u], t);
}
void Find_suf(int u, int t)
{
if (!u) return;
if (t < Val[u])
{
suf = u;
Find_suf(Son[0][u], t);
}
else Find_suf(Son[1][u], t);
}
}Treap;
int sqz()
{
int n;
while (~scanf("%d", &n) && n)
{
root = point = 0;
int x = read(), y = read();
printf("%d 1\n", x);
Treap.Insert(root, y, x);
rep(i, 2, n)
{
pre = suf = -1;
x = read(), y = read();
Treap.Find_pre(root, y);
Treap.Find_suf(root, y);
Treap.Insert(root, y, x);
printf("%d ", x);
if (pre == -1) printf("%d\n", Treap.Label[suf]);
else if (suf == -1) printf("%d\n", Treap.Label[pre]);
else if (y - Treap.Val[pre] <= Treap.Val[suf] - y) printf("%d\n", Treap.Label[pre]);
else printf("%d\n", Treap.Label[suf]);
}
}
}
[HDU4585]Shaolin的更多相关文章
- HDU4585 Shaolin (STL和treap)
Shaolin HDU - 4585 Shaolin temple is very famous for its Kongfu monks.A lot of young men go to ...
- 【HDU4585 Shaolin】map的经典运用
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 题意大意:很多人想进少林寺,少林寺最开始只有一个和尚,每个人有有一个武力值,若这个人想进少林,必 ...
- hdu 4585 Shaolin treap
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Problem ...
- 平衡二叉树---Shaolin
Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temp ...
- A -- HDU 4585 Shaolin
Shaolin Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d , %I64u Java clas ...
- hdu 4585 Shaolin(STL map)
Problem Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shao ...
- HDU 4585 Shaolin (STL)
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- HDU 4585 Shaolin(水题,STL)
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- HDU 4585 Shaolin(Treap找前驱和后继)
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Su ...
随机推荐
- Lua --- 输入一个数字,输出阶乘
function fact(n) == n then else ) end end print("Enter a number : ") a = io.read("*nu ...
- 第 7 章 多主机管理 - 045 - 安装 Docker Machine
安装 Docker Machine 先安装docker 官方安装docker-machine的文档地址:https://docs.docker.com/machine/install-machine/ ...
- 编写脚本,出现 TypeError: exceptions must be old-style classes or derived from BaseException, not unicode怎样解决?
小编使用robot framework,在编写安卓自动化脚本时,出现这样的情况: 在网上搜了好久,发现都是python的解决方法,到底怎样解决robot里面的问题呢?最终发现: (1)代码中我是这样写 ...
- 学了vue和webpack的笔记
首先把package.json贴出来,这里很多插件存在版本区别,因此要特别注意版本,不是所有的安装最新的都行 { "name": "life_manager", ...
- 守护进程函数——内部的小范围try catch 增强了 while死循环执行的 可靠性
void Watch() { try { LogHelper.WriteLog("WatchServi ...
- 【消息队列】kafka是如何保证高可用的
一.kafka一个最基本的架构认识 由多个broker组成,每个broker就是一个节点:创建一个topic,这个topic可以划分为多个partition,每个partition可以存在于不同的br ...
- 20171104xlVBA制作联合成绩条
Dim dGoal As Object Dim dCls As Object Sub 制作联合成绩条() Dim sht As Worksheet Dim HeadRng As Range Dim H ...
- 赵炯博士《Linux内核完全注释》
赵炯:男,1963年10月5日出生,江苏苏州人,汉族. 同济大学机械工程学院机械电子教研室副教授,从事教学和科研工作. 现在主要为硕士和博士研究生开设<计算机通信技术>.<计算机控制 ...
- AT2112 Non-redundant Drive
题目:https://www.luogu.org/problemnew/show/AT2112 对于这种找路径的就直接上点分治就好. 分治时,算出每一个点到分治重心的后能剩多少油,从分治重心走到每个点 ...
- 宽度优先搜索BFS(Breadth-First-Search)
Breadth-First-Search 1. 与DFS的异同 相同点:搜索所有可能的状态. 不同点:搜索顺序. 2. BFS总是先搜索距离初始状态近的状态,它是按照:开始状态->只需一次转移就 ...