POJ - 2828 Buy Tickets (Splay模拟)

Buy Tickets

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 27051   Accepted: 12920

Description

Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue…

The Lunar New Year was approaching, but unluckily the Little Cat still had schedules going here and there. Now, he had to travel by train to Mianyang, Sichuan Province for the winter camp selection of the national team of Olympiad in Informatics.

It was one o’clock a.m. and dark outside. Chill wind from the northwest did not scare off the people in the queue. The cold night gave the Little Cat a shiver. Why not find a problem to think about? That was none the less better than freezing to death!

People kept jumping the queue. Since it was too dark around, such moves would not be discovered even by the people adjacent to the queue-jumpers. “If every person in the queue is assigned an integral value and all the information about those who have jumped the queue and where they stand after queue-jumping is given, can I find out the final order of people in the queue?” Thought the Little Cat.

Input

There will be several test cases in the input. Each test case consists of N + 1 lines where N (1 ≤ N ≤ 200,000) is given in the first line of the test case. The next N lines contain the pairs of values Posi and Vali in the increasing order of i (1 ≤ i ≤ N). For each i, the ranges and meanings of Posi and Vali are as follows:

  • Posi ∈ [0, i − 1] — The i-th person came to the queue and stood right behind the Posi-th person in the queue. The booking office was considered the 0th person and the person at the front of the queue was considered the first person in the queue.
  • Vali ∈ [0, 32767] — The i-th person was assigned the value Vali.

There no blank lines between test cases. Proceed to the end of input.

Output

For each test cases, output a single line of space-separated integers which are the values of people in the order they stand in the queue.

Sample Input

4
0 77
1 51
1 33
2 69
4
0 20523
1 19243
1 3890
0 31492

Sample Output

77 33 69 51
31492 20523 3890 19243

Hint

The figure below shows how the Little Cat found out the final order of people in the queue described in the first test case of the sample input.

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define ls(x) T[x].ch[0]
#define rs(x) T[x].ch[1]
#define fa(x) T[x].fa
#define root T[0].ch[1]
using namespace std;
inline bool sc(int &num)
{
	char in; bool IsN = false;
	in = getchar();
	if (in == EOF) return false;
	while (in != '-' && (in<'0' || in>'9')) in = getchar();
	if (in == '-') { IsN = true; num = 0; }
	else num = in - '0';
	while (in = getchar(), in >= '0'&&in <= '9') {
		num *= 10, num += in - '0';
	}
	if (IsN) num = -num;
	return true;
}
const int MAXN = 2e5 + 10, mod = 10007, INF = 1e9 + 10;
struct node {
	int fa, ch[2], sum, id;
}T[MAXN];
int tot = 0, pointnum = 0;
void update(int x) { T[x].sum = T[ls(x)].sum + T[rs(x)].sum + 1; }
int ident(int x) { return T[fa(x)].ch[0] == x ? 0 : 1; }
void connect(int x, int fa, int how) { T[fa].ch[how] = x; T[x].fa = fa; }
void rotate(int x) {
	int Y = fa(x), R = fa(Y);
	int Yson = ident(x), Rson = ident(Y);
	connect(T[x].ch[Yson ^ 1], Y, Yson);
	connect(Y, x, Yson ^ 1);
	connect(x, R, Rson);
	update(Y); update(x);
}
void splay(int x, int to) {
	to = fa(to);
	while (fa(x) != to) {
		int y = fa(x);
		if (T[y].fa == to) rotate(x);
		else if (ident(x) == ident(y)) rotate(y), rotate(x);
		else rotate(x), rotate(x);
	}
}
int newnode(int f, int id) {
	T[++tot].fa = f;
	T[tot].sum = 1;
	T[tot].id = id;
	T[tot].ch[0] = T[tot].ch[1] = 0;
	return tot;
}
int kth(int x)//排名为x的数 
{
	int now = root;
	while (1) {
		int used = T[now].sum - T[T[now].ch[1]].sum;
		if (T[T[now].ch[0]].sum < x&&x <= used) { splay(now, root); return 1; }
		if (x < used) now = T[now].ch[0];
		else now = T[now].ch[1], x -= used;
	}
}

void Insert(int x, int id) {
	if (x == 0) {
		if (root == 0) {
			newnode(0, id); root = tot;
		}
		else {
			int r = root; T[r].sum++;
			while (T[r].ch[0]) {
				r = T[r].ch[0];
				T[r].sum++;
			}
			newnode(r, id);
			T[r].ch[0] = tot;
			splay(tot, root);
		}
		return;
	}
	kth(x);
	int r = root;
	int rt = newnode(0, id);
	root = rt;
	T[rt].ch[1] = T[r].ch[1];
	T[T[r].ch[1]].fa = rt;
	T[r].ch[1] = 0;
	T[r].sum -= T[T[rt].ch[1]].sum;
	T[rt].ch[0] = r; T[r].fa = rt;
	T[rt].sum = 1 + T[T[rt].ch[1]].sum + T[T[rt].ch[0]].sum;
}
void dfs(int r, int& d) {
	if (T[r].ch[0]) {
		dfs(T[r].ch[0], d);
	}
	printf("%d%s", T[r].id, d == tot ? "\n" : " ");
	d++;
	if (T[r].ch[1]) {
		dfs(T[r].ch[1], d);
	}
}
int main() {
	int opt, x, sz = 0, n;
	while (scanf("%d", &n) != EOF) {
		root = tot = 0;
		while (n--) {
			int a, b;
			sc(a); sc(b);
			Insert(a, b);
		}
		int i = 1;
		dfs(root, i);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/chenshibo17/article/details/102108781
今日推荐