6018: Buy Tickets(线段树单点更新)
6018: Buy Tickets ![分享至QQ空间](http://www.tzcoder.cn/acmhome/forum/images/ico_qzone.gif)
总提交: 18 测试通过:6
描述
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.
输入
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 Valiin 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.
输出
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.
样例输入
4
0 77
1 51
1 33
2 69
4
0 20523
1 19243
1 3890
0 31492
样例输出
77 33 69 51
31492 20523 3890 19243
提示
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.
题目来源
解题思路: 题目意思动态的插入 从后往前 如果左边还有位置往左 不行往右
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 5 inline int read(){ 6 int x=0,f=1; 7 char ch=getchar(); 8 while(ch<'0'||ch>'9'){ 9 if(ch=='-') f=-1; 10 ch=getchar(); 11 } 12 while(ch>='0'&&ch<='9'){ 13 x=(x<<1)+(x<<3)+(ch^48); 14 ch=getchar(); 15 } 16 return x*f; 17 } 18 19 inline void write(int x){ 20 if(x<0){ 21 putchar('-'); 22 x=-x; 23 } 24 if(x>9){ 25 write(x/10); 26 } 27 putchar(x%10+'0'); 28 } 29 30 const int maxn=2e5+5; 31 int sum[maxn<<2],res[maxn]; 32 int n,arr[maxn],brr[maxn]; 33 34 void push_up(int rt){ 35 sum[rt]=sum[rt<<1]+sum[rt<<1|1]; //下标可以放的数量 36 } 37 38 void build(int rt,int left,int right){ 39 if(left==right){ 40 sum[rt]=1; 41 return; 42 } 43 int mid=left+right>>1; 44 build(rt<<1,left,mid); 45 build(rt<<1|1,mid+1,right); 46 push_up(rt); 47 } 48 49 void query(int rt,int left,int right,int ee,int num){ 50 if(left==right){ 51 // printf("%d %d \n",left,right); 52 sum[rt]=0; //不能种了 53 res[left]=num; 54 return; 55 } 56 int mid=left+right>>1; 57 if(sum[rt<<1]>=ee){ 58 query(rt<<1,left,mid,ee,num); 59 } 60 else query(rt<<1|1,mid+1,right,ee-sum[rt<<1],num); 61 push_up(rt); 62 } 63 64 65 66 int main(){ 67 68 while(scanf("%d",&n)!=EOF){ 69 build(1,1,n); 70 for(int i=1;i<=n;i++){ 71 arr[i]=read(),brr[i]=read(); 72 arr[i]++; 73 } 74 for(int i=n;i>=1;i--){ 75 query(1,1,n,arr[i],brr[i]); 76 } 77 for(int i=1;i<=n;i++){ 78 if(i!=1) putchar(' '); 79 write(res[i]); 80 } 81 putchar('\n'); 82 } 83 return 0; 84 }