原题链接: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. win8或win8.1修改注册表失败的原因

    win8 and win8.1 modify the registry need compiled to be different versions according to the os bits.

  2. EXT学习之——Extjs 文本框 TextField 添加点击(onclick)事件方法

    { xtype:'textfield', listeners: { render: function(p) { // Append the Panel to the click handler's a ...

  3. python3的文件操作

    open的原型定义在bultin.py中,是一种内建函数,用于处理文件 open(file, mode='r', buffering=None, encoding=None, errors=None, ...

  4. 【MVC】自定义ASP.NET MVC Html辅助方法

    在ASP.NET MVC中,Html辅助方法给我们程序员带来很多方便,其重要性也就不言自明.有时候,我们不想重复地写一些HTML代码,或者MS没有提供我们想要的那个HTML标签的Html辅助方法,那么 ...

  5. HTTP MIME类型即HttpResponse.ContentType属性值列表

    MIME-Typ Dateiendung(en) Bedeutung application/acad *.dwg AutoCAD-Dateien (nach NCSA) application/ap ...

  6. QQ聊天信息提取

    先前在iOS 8.x版时,往往未能顺利取出QQ的聊天信息,即使顺利取出数据库,却发现聊天信息已被加密处理,仅只能得知是与哪些QQ号进行聊天,而未能顺利得知聊天内容. 但这个情况到后来有了变化,以下情境 ...

  7. Android代码写View

    1.Java Code package com.fish.helloworld; import android.app.Activity; import android.content.Context ...

  8. RedHat安装VMwareTools出现解压压缩包时无法打开文件的现象

    出现这种情况的原因是因为解压命令没有加—C参数,使用的命令为:tat -xvzf VMware Tools: 正确的解压命令应该是: tar -xvzf VMware Tools -C /opt,加上 ...

  9. javaSE第二十四天

    第二十四天    363 1:多线程(理解)    363 (1)JDK5以后的Lock锁    363 A:定义    363 B:方法:    364 C:具体应用(以售票程序为例)    364 ...

  10. app开发版面设计原则

    (1) 单纯:形象和色彩必须简单明了(也就是简洁性). (2) 统一:造型与色彩必须和谐,要具有统一的协调效果. (3) 均衡:整个画面须要具有魄力感与均衡效果. (4) 展现重点:构成要素必须化繁为 ...