Buy Tickets(线段树单点更新)

来源:互联网 发布:电脑抓阄软件 编辑:程序博客网 时间:2024/05/19 17:25

Buy Tickets

Time Limit : 8000/4000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 43   Accepted Submission(s) : 21
Problem 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
<p>There will be several test cases in the input. Each test case consists of <i>N</i> + 1 lines where <i>N</i> (1 ≤ <i>N</i> ≤ 200,000) is given in the first line of the test case. The next <i>N</i> lines contain the pairs of values <i>Pos<sub>i</sub></i> and <i>Val<sub>i</sub></i> in the increasing order of <i>i</i> (1 ≤ <i>i</i> ≤ <i>N</i>). For each <i>i</i>, the ranges and meanings of <i>Pos<sub>i</sub></i> and <i>Val<sub>i</sub></i> are as follows:</p><ul><li><i>Pos<sub>i</sub></i> ∈ [0, <i>i</i> − 1] — The <i>i</i>-th person came to the queue and stood right behind the <i>Pos<sub>i</sub></i>-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.</li><li><i>Val<sub>i</sub></i> ∈ [0, 32767] — The <i>i</i>-th person was assigned the value <i>Val<sub>i</sub></i>.</li></ul><p>There no blank lines between test cases. Proceed to the end of input.</p>
 

Output
<p>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.</p>
 

Sample Input
40 771 511 332 6940 205231 192431 38900 31492
 

Sample Output
77 33 69 5131492 20523 3890 19243
 

Source
PKU
 

Statistic | Submit | Back

题意:给一个整数n接下来n行每行两个数a,b表示b这个数字插在第a个数字后边,问最后形成的序列?

题解:此题建树的时候需要逆序输入数据,这样将后续的人员在对应位置向后插入即可,运用线段树来处理和操作

代码:

#include<cstdio>#include<cstring>#include<algorithm>#define maxn 200005using namespace std;int sum[maxn*4]; //注意此处定义是最大值的4倍int ans[maxn];void pushup(int pos)  //求左右子树和{    sum[pos]=sum[pos*2]+sum[pos*2+1];}void buildtree(int pos,int l,int r)  //建树{    if(l==r)    {        sum[pos]=1;//起始时刻,让所有的位置都站上人        return ;    }    int mid=(l+r)/2;    buildtree(pos*2,l,mid);    buildtree(pos*2+1,mid+1,r);    pushup(pos);//建成的树最底端每个位置都是第一个越往根部,人的编号越大,根处编号为n} void update(int o,int l,int r,int pos,int v){    if(l==r)    {        sum[o]=0;//当位于这个位置的时候        ans[r]=v;             return ;//结束    }    int mid=(l+r)/2;    if(pos<=sum[o*2])        update(o*2,l,mid,pos,v);//搜索左子树时    else        update(o*2+1,mid+1,r,pos-sum[o*2],v);//搜索右子树因为右子树左端的值是左子树右端值+1所以查找右子树位置时查找到pos-左子树长度就可以了    pushup(o);                               }                          int main(){    int n,m,j,i;    int a[maxn],b[maxn];    while(scanf("%d",&n)!=EOF)    {        buildtree(1,1,n);        for(i=1;i<=n;i++)            scanf("%d%d",&a[i],&b[i]);        for(i=n;i>0;i--)   //注意是逆序            update(1,1,n,a[i]+1,b[i]);//+1是因为题目数据是从0开始的 我们输数据的时候是从第一位而不是第0位输的  所以要+1,让位置对应        for(i=1;i<=n;i++)            printf("%d ",ans[i]);        printf("\n");    }    return 0;}




阅读全文
0 0
原创粉丝点击