POJ2828 Buy Tickets(线段树,单点更新)

来源:互联网 发布:邱少云违背生理学知乎 编辑:程序博客网 时间:2024/05/28 09:33

题目:

Buy Tickets
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 19959 Accepted: 9860

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 andVali 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

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

[Submit]   [Go Back]   [Status]   [Discuss]

思路:

先说题意,看着题目中给的图理解,要排队买票,刚开始77插入到了第0个位置,接下来51插入到了第1个位置,然后又有33插入到了第一个位置,所以51往后退一位,紧接着69插入到了2号位置,本来51在二号位置,所以往后退一位,那么最终的序列就是:77 33 69 51.


跟着题意,我们可以逆推(把位置从1开始计),最后一个人插入的位置肯定是它想要的位置,所以69一定在3号位置,然后继续往上推,33一定在2号位置,然后51想要到二号位置,但是二号位置已经有33了,那么他就要按顺序推后,就推到了最后一个位置,接下来77号插入了1号位置,符合题意。


我们利用线段树,先建立一棵空树,tree[i]里面存储当前节点里面还有几个空位,用sum[i]来记录当前节点的人是谁,然后从最后一个开始插入,然后更新线段树就好了,然后转载一个博客写的用于理解插入过程的图:

图片来自:http://www.cnblogs.com/CheeseZH/archive/2012/04/29/2476134.html

初始状态

首先是插入3 69

14结点有4个位置,

12结点有2个位置,小于3,因此放到14结点右孩子,且14结点空位置减1

到了14右孩子后,只要找到第3-2=1个位置即可,而34结点的左孩子33含有1个空位置,1>=1,所以放到33位置了。

 

插入2 33

 

★关键是这里如何处理★

插入2 51

此时14的左孩子只有1个位置,1<2,所以只能放到14的右孩子34

34的左孩子有0个位置,所以只能放在34的右孩子44上。

 

插入1 77

代码:

#include <cstdio>#include <cstring>#include <cctype>#include <string>#include <set>#include <iostream>#include <stack>#include <cmath>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define mod 10000007#define debug() puts("what the fuck!!!")#define N 200005#define M 1000000#define ll long longusing namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int tree[4*N];//存储当前空间的空位置数int sum[N];//存放对应位置的数void pushup(int rt){    tree[rt]=tree[rt<<1]+tree[rt<<1|1];//更新空位置数}void build(int l,int r,int rt){    if(l==r)    {        tree[rt]=1;//刚开始子节点的空位置数都为1        return;    }    int m=(l+r)>>1;    build(lson);    build(rson);    pushup(rt);}void update(int p,int val,int l,int r,int rt){    if(l==r)    {        sum[l]=val;        tree[rt]=0;        return;    }    int m=(l+r)>>1;    //printf("p=%d,rt=%d,tree[rt<<1(%d)]=%d\n",p,rt,rt<<1,tree[rt<<1]);    if(tree[rt<<1]>=p)//当前节点的左子树的空格数大于等于要放的位置数,就搜索左子树,反之搜索右子树        update(p,val,lson);    else        update(p-tree[rt<<1],val,rson);    pushup(rt);}int n;struct node{    int x,y;} zz[N];int main(){    while(~scanf("%d",&n))    {        build(1,n,1);        for(int i=1; i<=n; i++)            scanf("%d%d",&zz[i].x,&zz[i].y);        for(int i=n; i>=1; i--)        {            update(zz[i].x+1,zz[i].y,1,n,1);//从后往前更新,建树是从1开始建的没有0,所以要给位置+1        }        for(int i=1;i<=n;i++)        {            if(i>1)printf(" ");            printf("%d",sum[i]);        }        puts("");    }    return 0;}


0 0