poj2828(线段树应用)

来源:互联网 发布:船舶数据库 编辑:程序博客网 时间:2024/05/22 08:13

地址:http://poj.org/problem?id=2828

Buy Tickets
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 10466 Accepted: 5071

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
题意:pos为当此人进队列时他前方有多少人,val是他的编号。按照队列的顺序输出人的编号。

#include<cstdio>#include<cstring>#include<iostream>using namespace std;#define M 200010struct node{int l,r,sum,num;}tree[3*M];   //树int ans[M];   //输出的答案struct nodd{int pos,val;}a[M];        //记录的数据void ori_tree(int i,int l,int r){int m=(l+r)/2;tree[i].sum=r-l+1;    //将区间内空位数目记录进去tree[i].l=l;tree[i].r=r;if(l!=r){ori_tree(i*2,l,m);ori_tree(i*2+1,m+1,r);}}void updata(int i,int j,int num){if(tree[i].l==tree[i].r)   //如果搜到了底层,直接将数据赋值进去{tree[i].sum--;tree[i].num=num;ans[tree[i].l]=num;}else                       //没有搜到底层则进行判断{if(tree[i*2].sum>=j)   updata(i*2,j,num);else{j-=tree[i*2].sum;updata(i*2+1,j,num);}tree[i].sum--;}}int main(){int t,i;while(scanf("%d",&t)>0){for(i=1;i<=t;i++)scanf("%d%d",&a[i].pos,&a[i].val);ori_tree(1,1,t);for(i=t;i>=1;i--)    //从后向前更新,线段树中记录的是所剩空位数目updata(1,a[i].pos+1,a[i].val);printf("%d",ans[1]);for(i=2;i<=t;i++)printf(" %d",ans[i]);printf("\n");}return 0;}

从后向前遍历,将pos的值视为其前方所有的空位数。


原创粉丝点击