[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 ...
随机推荐
- 使用pipeline减少与redis交互次数
1.redis_pipeline=redis_cli.pipeline() 2.redis_pipeline.setex()此语句可写多条 3.redis_pipeline.execute() # # ...
- DOCTYPE的作用以及标准模式和兼容模式的区别
<!doctype>声明必须处于HTML文档的头部,在<html>标签之前,告知浏览器的解析器用什么文档标准解析这个文档.DOCTYPE不存在或格式不正确会导致文档以兼容模式呈 ...
- python + lisp hy的新手注记2 eval, HyModel and python AST
来自我在Stack Overflow上的提问,https://stackoverflow.com/questions/51675355/how-to-eval-a-cond-case-and-retu ...
- P499 usebrass2
有两种方式可以实现多态公有继承 1) 在派生类中重新定义基类的方法 2) 使用虚方法 如下是使用虚方法 brass.h #ifndef BRASS_H #define BRASS_H #include ...
- python sshtunnel 简单介绍
背景,公司的很多服务包括数据库访问都需要通过跳板机访问,为日常工作及使用带来了麻烦,特别数python直接操作数据更是麻烦了,所以一直想实现python 通过跳板机访问数据库的操作. 安装 pip3. ...
- Fetch的使用; Yarn命令集; NVM的管理;VueCLi3的使用;
如果喜欢使用lower-level,使用标准的 fetch API. 好处是无需额外的加载一个外部资源.但没有被浏览器完全支持,需要使用polyfill.因此使用Axios的更多一些. 参考Axios ...
- JS实现继承的6种方式
使用pretotype,call实现完美继承 父类: fuction Animal(name){ this.name=name||"Animal"; this.sl ...
- android ------- 运行官方NDK 案例HelloJNI
下载案例 HelloJNI ,导入工程到Eclipse, 可以直接下载我的案例, 源码下载:https://github.com/DickyQie/android-ndk 目录图 使用命令生成 . ...
- dedecms自定义表单时间时间戳值类型的转换方法
找网站找的别人的方法,记录一下 修改/dede/templets/diy_list.htm,在第42行else前面加上以下代码: else if($fielddata[1]=='datetime') ...
- 网络基础之 tcp/ip五层协议 socket
1 网络通信协议(互联网协议) 1.1 互联网的本质就是一系列的网络协议 1.2 osi七层协议 1.3 tcp/ip五层模型讲解 1.3.1 物理层 1.3.2 数据链路层 1.3.3 网络层 1. ...