原题链接: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的更多相关文章

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

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

  2. HDU 4585 Shaolin(STL map)

    Shaolin Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit cid= ...

  3. [HDU 4585] Shaolin (map应用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 题目大意:不停的插入数字,问你跟他相距近的ID号.如果有两个距离相近的话选择小的那个. 用map ...

  4. hdu 4585 Shaolin treap

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

  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 (STL map)

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

随机推荐

  1. ASPxGridView中批量提交及个别提交的写法

    //获取chech box ID protected string GetProtoID() { string protoId = ""; //获取选中的记录Id List< ...

  2. StackExchange.Redis使用和封装小试

    StackExchange.Redis使用和封装小试 https://git.oschina.net/Ultralisk/ThorActor/blob/ThorActor/DBUtility/Redi ...

  3. JavaScript基本用法

    首次创建 $(document).ready(function () { });

  4. global.autoindex dede:global.itemindex 获取子栏目自动排序序号

    {dede:channel  row='6' typeid=7 }                  [field:global.autoindex runphp='yes']             ...

  5. select 嵌套查询

    1. SELECT语句的子查询 语法:     SELECT ... FROM (subquery) AS name ... 先创建一个表: CREATE TABLE t1 (s1 INT, s2 C ...

  6. Linux下解压命令

    .tar.xz 先用命令[xz -d ***.tar.xz]解压,然后用[tar xvf ***.tar]解包 也可以直接用命令[tar xvJf ***.tar.xz]解压

  7. Android IOS WebRTC 音视频开发总结(三二)-- WebRTC项目开发建议

    本文主要介绍WEBRTC开发过程中的一些现象,文章来自博客园RTC.Blacker,支持原创,欢迎关注微信公众号blacker,更多详见www.rtc.help 随着移动互联网和智能硬件的快速发展,音 ...

  8. (转载)Android content provider基础与使用

    android有一个独特之处就是,数据库只能被它的创建者所使用,其他的应用是不能访问到的,所以如果你想实现不同应用之间的数据共享,就不得不用content provider了.在Android中,co ...

  9. 一个用WPF做的简单计算器源代码

    一.界面设计XAML代码 <Window x:Class="fengjisuanqi.MainWindow" xmlns="http://schemas.micro ...

  10. Winform 打开下载的文件

    private void OpenFile(string filename) { ProcessStartInfo sInfo = new ProcessStartInfo(); sInfo.Wind ...