首页 > 代码库 > BNUOJ 2947 Buy Tickets
BNUOJ 2947 Buy Tickets
Buy Tickets
64-bit integer IO format: %lld Java class name: Main
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
40 771 511 332 6940 205231 192431 38900 31492
Sample Output
77 33 69 5131492 20523 3890 19243
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 200010;18 struct node{19 int lt,rt,sum;20 };21 node tree[maxn<<2];22 int ans[maxn],ps[maxn],w[maxn],n;23 void build(int lt,int rt,int v){24 tree[v].lt = lt;25 tree[v].rt = rt;26 if(lt == rt){27 tree[v].sum = 1;28 return;29 }30 int mid = (lt+rt)>>1;31 build(lt,mid,v<<1);32 build(mid+1,rt,v<<1|1);33 tree[v].sum = tree[v<<1].sum + tree[v<<1|1].sum;34 }35 void update(int p,int val,int v){36 int mid = (tree[v].lt+tree[v].rt)>>1;37 if(tree[v].lt == tree[v].rt){38 tree[v].sum = 0;39 ans[tree[v].lt] = val;40 return;41 }42 if(tree[v<<1].sum >= p) update(p,val,v<<1);43 else update(p-tree[v<<1].sum,val,v<<1|1);44 tree[v].sum--;45 }46 int main() {47 int i;48 while(~scanf("%d",&n)){49 for(i = 0; i < n; i++)50 scanf("%d %d",ps+i,w+i);51 build(1,n,1);52 for(i--; i >= 0; i--)53 update(ps[i]+1,w[i],1);54 for(i = 1; i < n; i++)55 printf("%d ",ans[i]);56 printf("%d\n",ans[i]);57 }58 return 0;59 }