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 ...
随机推荐
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 09_platform-tools简介&常见adb指令
SDK下面的文件夹说明add-ons 附加的附属的一些信息.docs Android开发的帮助文件.extras 支持的jar包,高版本兼容底版本.google usb的驱动.platforms 存放 ...
- ASPxTreeList控件去根节点的新增修改操作(写在onCommandColumnButtonInitialize()事件中)
treelist去掉根节点按钮效果图: //去掉父节点及子节点旁的新增.修改.删除操作(写在onCommandColumnButtonInitialize事件中) protected void Tre ...
- Hibernate 问题,在执行Query session.createQuery(hql) 报错误
在配置文件中加入 <prop key="hibernate.query.factory_class">org.hibernate.hql.internal.classi ...
- Secure your iPhone with 6 digit passcode by upgrading to iOS9
IP-Box could crack 4 digit passcode, what about 6 digit passcode??? All you need to do is to upgrade ...
- CentOS下通过rdesktop连接Windows远程桌面
众所周知,微软的Windows提供了一种远程桌面系统(Remote Desktop),该服务的默认端口是3389,可使用户远程登录进行系统管理或作为终端服务器运行各种应用软件. 而要连接Windows ...
- js动态改变图片热区坐标,手机端图片热区自适应
<img id='banner1' src="images/banner.jpg" usemap="#banner" border="0&quo ...
- Count and Say [LeetCode 38]
1- 问题描述 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211 ...
- GUI_DOWNLOAD参数说明
对FUNCTION: GUI_DOWNLOAD中某些参数的用法. call function 'GUI_DOWNLOAD' exporting * BIN_FILESIZE ...
- Stop PeopleCode Processing with Error
A blunt, but useful method for debugging PeopleCode is to use the inbuilt "Error" function ...