您的位置:首页 > 其它

POJ 2828 Buy Tickets ( Splay tree && Segment tree )

2017-02-26 10:04 225 查看
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 orde
4000
r 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

题目大意就是几个人插队,最后输出队伍的最终状态。多组数据。

可以用线段树写,不过我为了练习splay,写的splay。

线段树的做法比较奇特&&麻烦,但是普遍都是线段树写法。

这里给出splay,除了常数大之外好像没什么。

Splay做法其实很简单,首先以队伍顺序为关键字构造树(没有必要保存的),然后维护一个size数组即可。

下面给出代码,中间数组的奇怪写法请参阅immortalCO爷的文章

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <cmath>
#include <algorithm>
#include <ctime>
using namespace std;
bool Read ( int &x ) { char c = getchar() ; x = 0 ; bool f = 0 ; while ( !isdigit(c) ) { if ( c == '-' ) f = 1 ; if ( c == EOF ) return false ; c = getchar() ; } while ( isdigit(c) ) { x = 10 * x + c - '0' ; c = getchar() ; } if (f) x = -x ;return true ; }
void Print ( int x ) { int len = 0, a[30] ; if ( x == 0 ) { putchar('0') ; return ; } if ( x < 0 ) { putchar('-') ; x = -x ; } while (x) { a[++len] = x%10 ; x /= 10 ; } while (len) putchar(a[len--]+'0') ;}
const int maxn = 200010 ;
int n, m, cnt, root, ch[maxn][2], fa[maxn], size[maxn], val[maxn] ;
#define L ch][0
#define R ch][1

void push_up ( int x ) { x[size] = x[L][size] + x[R][size] + 1 ; }
int new_node ( int x ) {
val[++cnt] = x ;
size[cnt] = 1 ;
cnt[fa] = cnt[L] = cnt[R] = 0 ;
return cnt ;
}

void out ( int x ) {
if ( x[L] ) out(x[L]) ;
Print(x[val]) ; putchar(' ') ;
if ( x[R] ) out(x[R]) ;
}

void Rotate ( int x ) {
register int y = fa[x], z = fa[y], T = y[R] == x, son = ch[x][T^1] ;
if ( y == root ) root = x ;
if ( y != root ) ch[z][y==ch[z][1]] = x ;
fa[x] = z ;
fa[y] = x ;
if ( son ) fa[son] = y ;
ch[y][T] = son ;
ch[x][T^1] = y ;
push_up(y) ;
}

void splay ( int x, int tar = 0 ) {
while ( fa[x] && fa[x] != tar ) {
int y = x[fa], z = y[fa], f1 = y[R]==x, f2 = z[R]==y ;
if ( !z || z == tar ) ;
else if ( f1^f2 ) Rotate(x) ;
else Rotate(y) ;
Rotate(x) ;
}
push_up(x) ;
}

int find ( int t, int x ) {
if ( !x || t > size[x] ) return 0 ;
if ( t == x[L][size]+1 ) return x ;
if ( t <= x[L][size] )
return find ( t, x[L] ) ;
return find ( t-1-x[L][size], x[R] ) ;
}

void insert ( int t, int x ) {
if ( !root ) {
root = new_node(x) ;
return ;
}
if ( !t ) {
(root[fa] = new_node(x)) [R] = root ;
root = root[fa] ;
}
else {
splay ( find(t,root) ) ;
if ( t == cnt ) {
(root[R] = new_node(x)) [fa] = root ;
} else {
splay( find(t+1,root), root ) ;
(root[R][L] = new_node(x)) [fa] = root[R] ;
push_up(root[R]) ;
}
}
push_up(root) ;
}

int main() {
register int i, j, k, x, t ;
while ( Read(n) ) {
cnt = root = 0 ;
for ( i = 1 ; i <= n ; i ++ ) {
Read(t) ; Read(x) ;
insert ( t, x ) ;
}
out(root) ; putchar('\n') ;
}
return 0 ;
}


在这里,对小学生sxy表示感谢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: