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的更多相关文章

  1. HDU4585 Shaolin (STL和treap)

    Shaolin HDU - 4585       Shaolin temple is very famous for its Kongfu monks.A lot of young men go to ...

  2. 【HDU4585 Shaolin】map的经典运用

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 题意大意:很多人想进少林寺,少林寺最开始只有一个和尚,每个人有有一个武力值,若这个人想进少林,必 ...

  3. hdu 4585 Shaolin treap

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Problem ...

  4. 平衡二叉树---Shaolin

    Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temp ...

  5. A -- HDU 4585 Shaolin

    Shaolin Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d , %I64u Java clas ...

  6. hdu 4585 Shaolin(STL map)

    Problem Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shao ...

  7. HDU 4585 Shaolin (STL)

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  8. HDU 4585 Shaolin(水题,STL)

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  9. HDU 4585 Shaolin(Treap找前驱和后继)

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Su ...

随机推荐

  1. 学习笔记28—Python 不同数据类型取值方法

    1.array数据类型 1)-------> y[i,] 或者 y[i] 2.遍历目录下所有文件夹: def eachFile(filepath):     pathDir =  os.list ...

  2. 【java】Comparator的用法

    文章转载自: http://blog.csdn.net/u012250875/article/details/55126531 1.为什么写? comparator 是javase中的接口,位于jav ...

  3. ZOJ 3965 Binary Tree Restoring

    Binary Tree Restoring 思路: 递归 比较a序列和b序列中表示同一个子树的一段区间,不断递归 代码: #include<bits/stdc++.h> using nam ...

  4. Notepad++安装json插件

    安装  : 1.下载插件压缩包并解压出dll:NPPJSONViewer.dll(64位) 下载地址:https://pan.baidu.com/s/1JeBzrovb-GHRo14vO-AnJA 提 ...

  5. boke练习: freemarker对空变量报错 (classic_compatible设置true,解决报空错误)

    我有一个变量: commentModel 默认只是为空, 在freemarker模板中使用<#if>判断是报错 <#if commentModel> ..... </#i ...

  6. “安利”一个CDN服务商网站

    一.CDN简介 CDN的全称是Content Delivery Network,即内容分发网络.CDN是构建在网络之上的内容分发网络,依靠部署在各地的边缘服务器,通过中心平台的负载均衡.内容分发.调度 ...

  7. python基础之字符串以及切片等操作

    1.字符类型 1.整型 int  2. str 字符串 3.bool 布尔值 4.list 表格,常用于大量数据的存储 用[ ]表示  5.tuple 元祖 ,不能发生改变()表示  6.dict 字 ...

  8. 将本地 项目文件托管到 github

    1.新建一个本地 repository文件夹 2.将想要 托管的项目或文件 复制到repository 文件夹下 2. 右键 git bash here 输入命令 git init 生成本地仓库 4. ...

  9. 让你明白kvm是什么

    参考:https://blog.csdn.net/bbwangj/article/details/80465320 KVM 工具集合: libvirt:操作和管理KVM虚机的虚拟化 API,使用 C ...

  10. spring cloud服务发现注解之@EnableDiscoveryClient与@EnableEurekaClient区别

    在使用服务发现的时候有两种注解, 一种为@EnableDiscoveryClient, 一种为@EnableEurekaClient, 用法上基本一致,下文是从stackoverflow上面找到的对这 ...