Buy Tickets(线段树单点更新,查询某个节点的值)

Buy Tickets
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 26008   Accepted: 12446

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] — 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

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.

 

这题的题意就是,给你一个 pos,val,你需要在pos的左边插入一个数val,(即插入在第pos个位置的后面)。把最后的队伍状态输出

这里我们可以用线段树数来做,刚开始的时候,整个队列是空的,也就是说如果我们在第i人后插入x,前面已经有i个人了,所有x的位置肯定是i+1;

所以这里我们倒着来往前面推,遇到的每一个数位置都是确定的,最后的人选定的位置不会改变,同样因为是倒叙输入,在第i个人后插队,也就是说他的前面一定要留下i个位置。

比如样例二:

第1次插入:31492

第2次插入:31492 ___ 3890

第3次插入:31492 ___ 3890  19243

第4次插入:31492 20523 3890  19243

我们在每个节点保存一个空的位置数量num,每次在插入的时候,保存查询插入的位置,并将num的数量更新;

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<string.h>
 4 using namespace std;
 5 const int maxn=200005;
 6 struct node
 7 {
 8     int l,r;
 9     int num;///前面空格的数量
10 } t[maxn*4];
11 int a[maxn],b[maxn];
12 int ans[maxn];
13 void build(int rt,int l,int r)
14 {
15     t[rt].l=l;
16     t[rt].r=r;
17     if(l==r)
18     {
19         t[rt].num=1;
20         return ;
21     }
22     int mid=(l+r)>>1;
23     build(rt<<1,l,mid);
24     build(rt<<1|1,mid+1,r);
25     t[rt].num=t[rt<<1].num+t[rt<<1|1].num;
26 }
27 void update(int rt,int num,int val)///num=空格数量,val=人的权值
28 {
29     if(t[rt].num==num&&t[rt].l==t[rt].r)
30     {
31         t[rt].num=0;
32         ans[t[rt].l]=val;
33         return ;
34     }
35     if(num<=t[rt<<1].num)
36     {
37         update(rt<<1,num,val);
38     }
39     else
40         update(rt<<1|1,num-t[rt<<1].num,val);
41     t[rt].num=t[rt<<1].num+t[rt<<1|1].num;
42 }
43 int main()
44 {
45     int n;
46     while(~scanf("%d",&n))
47     {
48         build(1,1,n);
49         for(int i=1; i<=n; i++)
50         {
51             scanf("%d %d",&a[i],&b[i]);
52             a[i]++;
53         }
54         for(int i=n; i>=1; i--)
55         {
56             update(1,a[i],b[i]);
57         }
58         for(int i=1; i<=n; i++)
59             printf("%d ",ans[i]);
60         puts("");
61     }
62     return 0;
63 }

 

posted @ 2019-05-08 21:33  Q1311605467  阅读(200)  评论(0编辑  收藏  举报