poj2828 Buy Tickets 单点更新线段树

来源:互联网 发布:诺基亚3500c软件 编辑:程序博客网 时间:2024/05/19 19:35

Buy Tickets
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 14377 Accepted: 7183

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 nextN lines contain the pairs of values Posi and Vali in the increasing order ofi (1 ≤ iN). For each i, the ranges and meanings ofPosi and Vali are as follows:

  • Posi ∈ [0, i − 1] — The i-th person came to the queue and stood right behind thePosi-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 valueVali.

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

  类似于排队,从前往后的顺序把value值插到pos位置,后面的相当于都往后移一个。

  关键在于逆向思维,最后插入的某一位置的值肯定就在那个位置,然后忽略它,倒数第二个插入的pos就是忽略了最后插入的那个位置的第pos个位置,也就是说在它之前的只有pos-1个能插在它前面,所以留pos-1个空位。从后往前以此类推。

  用cnt表示这棵树还空闲的位置的个数,按输入顺序倒过来更新线段树,每次找到第pos个空位插入,更新cnt。

#include<iostream>#include<queue>#include<cstring>#include<cstdio>#include<cmath>#include<set>#include<map>#include<vector>#include<stack>#include<algorithm>using namespace std;typedef long long LL;typedef pair<int,int> pii;const int MAXN=200010;const int MAXNODE=4*MAXN;int N,ans[MAXN];struct st{    int pos,val;}a[MAXN];struct SegmentTree{    int cnt[MAXNODE];    void maintain(int o){        cnt[o]=cnt[o<<1]+cnt[o<<1|1];    }    void build(int o,int L,int R){        if(L>=R){            cnt[o]=1;            return;        }        int mid=L+(R-L)/2;        build(o<<1,L,mid);        build(o<<1|1,mid+1,R);        maintain(o);    }    void update(int o,int L,int R,int pos,int val){        cnt[o]--;        if(L>=R){            ans[L]=val;            return;        }        int mid=L+(R-L)/2;        if(cnt[o<<1]>=pos) update(o<<1,L,mid,pos,val);        else update(o<<1|1,mid+1,R,pos-cnt[o<<1],val);    }}tree;int main(){    freopen("in.txt","r",stdin);    while(scanf("%d",&N)!=EOF){        for(int i=0;i<N;i++) scanf("%d%d",&a[i].pos,&a[i].val);        tree.build(1,1,N);        for(int i=N-1;i>=0;i--) tree.update(1,1,N,a[i].pos+1,a[i].val);        for(int i=1;i<N;i++) printf("%d ",ans[i]);        printf("%d\n",ans[N]);    }    return 0;}



0 0
原创粉丝点击