poj 2828 Buy Tickets

来源:互联网 发布:淘宝crm软件有哪些 编辑:程序博客网 时间:2024/05/24 04:21

Buy Tickets
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 13277 Accepted: 6595

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

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.


题解及代码:


#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <set>#include <map>#include <queue>#include <string>#define maxn 200010#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define ALL %I64dusing namespace std;typedef long long  ll;struct node          //记录题目给的节点的信息{    int pos,value;}nd[200010];int pos[200010];   //记录叶子节点的位置,便于最后的输出                   //因为最后所有的元素都是插入到叶子节点的struct segment{    int l,r;    int value;    //用来线段中剩余空间的数目    int nd;       //叶子节点中用来记录插入的节点在nd结构体中的位置} son[maxn<<2];void PushUp(int rt){    son[rt].value=son[rt<<1].value+son[rt<<1|1].value;}void Build(int l,int r,int rt){    son[rt].l=l;    son[rt].r=r;    if(l==r)    {        pos[l]=rt;        son[rt].value=1;        return;    }    int m=(l+r)/2;    Build(lson);    Build(rson);    PushUp(rt);}void Update(int w,int nd,int rt)   //查找插入位置并维护线段树{    if(son[rt].l==son[rt].r)    {        son[rt].value=0;           //查找到插入位置,设置次空间被占用        son[rt].nd=nd;             //记录插入节点的位置        return;    }    int m=(son[rt].l+son[rt].r)/2;    if(son[rt<<1].value>w)        Update(w,nd,rt<<1);    else    {        w-=son[rt<<1].value;        Update(w,nd,rt<<1|1);    }    PushUp(rt);}int main(){   int n;   while(scanf("%d",&n)!=EOF)   {      Build(0,n-1,1);      for(int i=n-1;i>=0;i--)      {          scanf("%d%d",&nd[i].pos,&nd[i].value);      }      for(int i=0;i<n;i++)      {          Update(nd[i].pos,i,1);      }      for(int i=0;i<n;i++)      {          if(i==0)          printf("%d",nd[son[pos[i]].nd].value);          else printf(" %d",nd[son[pos[i]].nd].value);      }      puts("");   }    return 0;}/*对于第一个例子,我们分析一下,会发现:40 205231 192431 38900 31492它的最终结果是:31492 20523 3890 19243当我们从后向前扫描的时候:每一个元素所占的位置就是后一个元素插入之后,所剩空间中的绝对位置,为什么这么说?当我们把31492插入之后,它的最终位置就是0,也就是1号位置,当我们插入3890时,我们发现他最后的位置不是1,也就是2号位置,而是三号位置,这是为什么呢?因为后面还有一个20523,它的优先级比3890要高,所以当我们插入3890的时候,计算的是剩余空间的绝对位置,而不是所有空间的据对位置。这里我们使用线段树维护每插入一个节点后,每条线短里面升入的空间,便于下次查找插入的位置,每来一个数查找,插入,维护就可以了。*转载请注明出处,谢谢。*/






0 0
原创粉丝点击