线段树单点更新 poj2828 buy tickets

来源:互联网 发布:ubuntu jdk 64位下载 编辑:程序博客网 时间:2024/05/19 17:23

点击打开链接

Buy Tickets
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 20199 Accepted: 9980

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.

Source

这个题的题意是按时间顺序输入一些数据 代表有人来排队 可以随便排哪个位置(插队)并且每个人有个数字来表示他 求最后的位置

最后一个人肯定能站在他想站的位置  所以逆序插入线段树 每个叶节点的值初始化为 1 表示1个位置 

#include <iostream>
#include<cstdio>
using namespace std;
const int maxn=200005;
struct node
{
    int l,r,v;
}tree[maxn*4];
int ans[maxn];            //  ans数组保存那个位置的人的值 tree[i].l 就是那个位置 
struct line
{
    int p,v;
}a[maxn];
void build(int i,int l,int r)
{
    tree[i].l=l,tree[i].r=r;
    if(l==r)
    {
        tree[i].v=1;
        return ;
    }
    int m=(l+r)/2;
    build(i*2,l,m);
    build(i*2+1,m+1,r);
    tree[i].v=tree[i*2].v+tree[i*2+1].v;
}
void update(int i,int x,int v)
{
    if(tree[i].l==tree[i].r)
    {
        tree[i].v=0;
        ans[tree[i].l]=v;
        //cout<<tree[i].l<<" "<<v<<endl;
        return ;
    }
    if(x<=tree[i*2].v)      // 这里和那个ants类似 进左子树的时候x值不变 因为他就是从1开始的 但是进右子树的时候要减去左子树的值
    update(i*2,x,v);
    else update(i*2+1,x-tree[i*2].v,v);
    tree[i].v=tree[i*2].v+tree[i*2+1].v;
}


int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&a[i].p,&a[i].v);
            a[i].p++;                     //建的是从1到n的线段树 其实从0开始也行
        }
        build(1,1,n);
        for(int i=n-1;i>=0;i--)
        {
            update(1,a[i].p,a[i].v);
        }
        printf("%d",ans[1]);
        for(int i=2;i<=n;i++)
        printf(" %d",ans[i]);
        cout<<endl;
    }
      return 0;
}



原创粉丝点击