hdu 4585 Shaolin
原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=46093
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstdio>
const int Max_N = ;
struct Node {
int v, s;
Node *ch[];
inline void set(int _v, int _s , Node *p) {
ch[] = ch[] = p;
v = _v, s = _s;
}
inline void push_up() {
s = ch[]->s + ch[]->s + ;
}
inline int cmp(int x) const {
return x > v;
}
};
struct SizeBalanceTree {
Node *tail, *root, *null;
Node stack[Max_N];
void init(){
tail = &stack[];
null = tail++;
null->set(, , NULL);
root = null;
}
inline Node *newNode(int v) {
Node *p = tail++;
p->set(v, , null);
return p;
}
inline void rotate(Node* &x, int d) {
Node *k = x->ch[!d];
x->ch[!d] = k->ch[d];
k->ch[d] = x;
k->s = x->s;
x->push_up();
x = k;
}
inline void Maintain(Node* &x, int d) {
if (x->ch[d] == null) return;
if (x->ch[d]->ch[d]->s > x->ch[!d]->s) {
rotate(x, !d);
} else if (x->ch[d]->ch[!d]->s > x->ch[!d]->s) {
rotate(x->ch[d], d), rotate(x, !d);
} else {
return;
}
Maintain(x, ), Maintain(x, );
}
inline void insert(Node* &x, int v) {
if (x == null) {
x = newNode(v);
return;
} else {
x->s++;
int d = x->cmp(v);
insert(x->ch[d], v);
x->push_up();
Maintain(x, d);
}
}
inline void insert(int v) {
insert(root, v);
}
inline int count(int v) {
Node *x = root;
int res = , t = ;
for (; x->s;) {
t = x->ch[]->s;
if (v < x->v) x = x->ch[];
else res += t + , x = x->ch[];
}
return res;
}
inline int kth(int k) {
int t = ;
Node *x = root;
if (x->s < k) return -;
for (; x->s;) {
t = x->ch[]->s;
if (k == t + ) break;
else if (k <= t) x = x->ch[];
else k -= t + , x = x->ch[];
}
return x->v;
}
inline int operator[](int k) {
return kth(k);
}
}sbt;
int id[];
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n, t, x, a, b, k;
while (~scanf("%d", &n) && n) {
sbt.init();
scanf("%d%d", &t, &x);
sbt.insert(x), id[x] = t;
printf("%d 1\n", t);
for (int i = ; i <= n; i++) {
scanf("%d%d", &t, &x);
id[x] = t, k = sbt.count(x);
if (!k) a = sbt[];
else if (k == i - ) a = sbt[i - ];
else {
a = sbt[k];
b = sbt[k + ];
if (x - a > b - x) a = b;
}
printf("%d %d\n", t, id[a]);
sbt.insert(x);
}
}
return ;
}
hdu 4585 Shaolin的更多相关文章
- HDU 4585 Shaolin(Treap找前驱和后继)
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Su ...
- HDU 4585 Shaolin(STL map)
Shaolin Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit cid= ...
- [HDU 4585] Shaolin (map应用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 题目大意:不停的插入数字,问你跟他相距近的ID号.如果有两个距离相近的话选择小的那个. 用map ...
- hdu 4585 Shaolin treap
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Problem ...
- 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 (STL map)
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
随机推荐
- kubernetes学习笔记1
安装篇 Downloading Kubernetes You can either build a release from sources or download a pre-built relea ...
- [drp 6]接口和抽象类的区别,及其应用场景
导读:在很多时候,接口和抽象类可以替换.发现这个问题,还是之前学习设计模式的时候,看到那个UML图发现的.那么,究竟在什么时候使用接口,什么时候使用抽象类呢?现在结合这个项目,做一个总结. 一.接口 ...
- kickstrat
定制部分 硬盘分区 root密码 网络地址 公共部分 %pre% %post% tcp调优 集中配置程序(ansible) sudo配置
- IDL与C#混合编程技术
C# (C Sharp)是微软公司在2000年6月发布的一种新的编程语言.C#与Java有很多的相似之处,包括了诸如单一继承.界面.与Java几乎同样的语法,和编译成中间代码再运行的过程.它又借鉴了D ...
- 对像转成 和 byte 互转类库方法
using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serializatio ...
- Windows phone 8 学习笔记(1) 触控输入(转)
Windows phone 8 的应用 与一般的Pc应用在输入方式上最大的不同就是:Windows phone 8主要依靠触控操作.因此在输入方式上引入一套全新的触控操作方式,我们需要重新定义相关的事 ...
- RPC通信(Windows版、Android版)
1.RPC通信模型 2.调用截图 服务器端 PC客户端: Android客户端: 3.remotetea jrpcgen.jar:生成Java源文件 oncrpc.jar:框架通信调用 portmap ...
- Android IOS WebRTC 音视频开发总结(四八)-- 从商业和技术的角度看视频行业的机会
本文主要从不同角度介绍视频行业的机会,文章来自博客园RTC.Blacker,支持原创,转载必须说明出处,欢迎关注个人微信公众号blacker ----------------------------- ...
- SQL笔记-第二章,数据表的创建和管理
数据类型 分5类:整数.数值.字符相关.日期时间以及二进制 1.整数 数据库系统 类型 说明 MYSQL tinyint [unsigned] 一个很小的整数.有符号的范围是-128 到127,无符号 ...
- ASP.NET验证控件二
RequiredFieldValidator 验证控件 页面布局: <div> <h1>RequiredFieldValidator 验证控件</h1> 用户名 ...