Buy Tickets
Time Limit: 4000MS | Memory Limit: 65536K |
Total Submissions: 18675 | Accepted: 9277 |
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] — Thei-th person came to the queue and stood right behind thePosi-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] — Thei-th person was assigned the valueVali.
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.
Source
POJ Monthly--2006.05.28, Zhu, Zeyuan
题意:给你n个人,然后这n个人就开始插队啦。Posi ∈ [0, iVali ∈ [0, 32767]表示这个人的信息。最后让你输出这n个人排好队的信息。
题解:倒序查询。线段树O(nlogn):
线段树节点中保存这一段中的空位数,然后倒序对pos插入:
例如: 0 77
1 51
1 33
2 69
先取: 2 69 —— —— —69— —— (需要前面有3个空位才能插入)
然后取: 1 33 —— —33— —69— —— (需要前面有2个空位才能插入)
然后取: 1 51 —— —33— —69— —51— (需要前面有2个空位才能插入) 前面只有1个空位 故插入后面空格
最后取: 0 77 —77— —33— —69— —51— (需要前面有1个空位才能插入)
具体看代码:
#pragma comment(linker, "/STACK:102400000,102400000")
//#include<bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <bitset>
#include <iomanip>
#include <list>
#include <stack>
#include <utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
const double eps = 1e-8;
const int INF = 1e9+7;
const ll inf =(1LL<<62) ;
const int MOD = 1e9 + 7;
const ll mod = (1LL<<32);
const int N =1e6+6;
const int M=100010;
const int maxn=200010;
#define mst(a) memset(a, 0, sizeof(a))
#define M_P(x,y) make_pair(x,y)
#define in freopen("in.txt","r",stdin)
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
const int lowbit(int x) { return x&-x; }
//const int lowbit(int x) { return ((x)&((x)^((x)-1))); }
int read(){ int v = 0, f = 1;char c =getchar();
while( c < 48 || 57 < c ){if(c=='-') f = -1;c = getchar();}
while(48 <= c && c <= 57) v = v*10+c-48, c = getchar();
return v*f;}
int h,w,n;
int MAX[maxn<<2];
int ans[maxn<<2];
int value;
void pushup(int rt)//把当前结点的信息更新到父结点
{
//线段树是用数组来模拟树形结构
//对于每一个节点rt,左子节点为 2*rt (一般写作rt<<1)右子节点为 2*rt+1(一般写作rt<<1|1)
MAX[rt] = MAX[rt<<1] + MAX[rt<<1|1];
}
void build(int l,int r,int rt)
{
if(l==r){
MAX[rt] = 1; //空位数
return ;
}
int mid=(l+r)>>1;
build(lson);//递归构造左子树
build(rson);//递归构造右子树
pushup(rt); //更新
}
void query(int x, int l,int r,int rt)
{
MAX[rt] -- ; //空位数减1
if(l == r){
ans[l]=value;
return ;
}
int mid = (l+r) >> 1;
if( MAX[rt<<1] >= x) query(x,lson);//当左孩子的空格大于等于插入位置 x 时往左边插入
else query(x-MAX[rt<<1],rson);//当左边的空格小于 x ,则插入右边,插入右边位置x应该减左边的空格数
}
int a[maxn][2];
int main()
{
int n;
//freopen("in.txt","r",stdin);
while(~scanf("%d",&n))
{
build(1,n,1);
for(int i=0;i<n;i++) scanf("%d%d",&a[i][0],&a[i][1]);
for(int i=n-1;i>=0;--i)
{
value= a[i][1];
query( a[i][0] + 1, 1, n, 1 );
}
for(int i=1;i<n;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[n]);
}
return 0;
}