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;
}