hdu 4585 Shaolin

时间:2023-03-29 09:24:38

原题链接: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 ;
}