标签:des style blog class code java
Buy Tickets Time Limit:4000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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:
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
4 0 77 1 51 1 33 2 69 4 0 20523 1 19243 1 3890 0 31492
Sample Output
77 33 69 51 31492 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<cstring> #include<cstdio> #include<cmath> #include<cstdlib> #include<algorithm> #include<iostream> #define MAX 200010 using namespace std; struct Node { int p,v; } node[MAX]; struct TNode { int l,r; int c; } tree[4*MAX]; int N; int ans[MAX]; void build(int l,int r,int x) { tree[x].c=r-l+1; //该区间的大小,开始写反了,wa了两次 tree[x].l=l; tree[x].r=r; if(l==r) //到底了,返回 return; int mid=(l+r)>>1; build(l,mid,2*x); build(mid+1,r,2*x+1); } void Insert(int x,int pos,int val) //下标 位置(position) 价值(value) { tree[x].c--; //该区间的空格数减少1 if(tree[x].l==tree[x].r) //直到找到了一个合适的空格来存放该位置 { ans[tree[x].l]=val; //将价值映射到ans中 return; } if(tree[2*x].c>=pos) //该区间的剩余空格数大于位置的坐标,往前探寻 Insert(2*x,pos,val); else //该区间剩余空格数小于位置的坐标,往后找空格 Insert(2*x+1,pos-tree[2*x].c,val); } int main() { while(~scanf("%d",&N)) { build(1,N,1); int i; for(i=1;i<=N;i++) { scanf("%d%d",&node[i].p,&node[i].v); node[i].p++; //输入是从0开始的,加1变为从1开始 } for(i=N;i>=1;i--) //重点:逆序更新 { Insert(1,node[i].p,node[i].v); } for(i=1;i<N;i++) printf("%d ",ans[i]); printf("%d\n",ans[N]); } return 0; }
线段树 --- (区间维护+逆推),布布扣,bubuko.com
标签:des style blog class code java
原文地址:http://www.cnblogs.com/acmer-jsb/p/3705444.html