标签:
这道题是之前一场比赛碰到的题目,当时看到题时以为是一道用链表优化的水题,交了几遍一直超时,简直不能再感人(┬_┬)今天有空突然想起去查了下题解,是用线段树做的。。。。。完全想不到啊有木有~思路大概就是每个节点存当前节点下还剩多少空位,然后倒序查找该人所需要的位置,具体细节看代码吧。
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 15801 | Accepted: 7876 |
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
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #include<stack> 7 #include<string> 8 using namespace std; 9 #define N 100005 10 #define inf 0x7fffffff 11 int node[N*8],num[N*2]; 12 struct ss 13 { 14 int p,v; 15 } pos[N*2]; 16 void buildtree(int root,int l,int r) 17 { 18 node[root]=r-l+1; 19 if(l==r) return ; 20 buildtree(root*2,l,(l+r)/2); 21 buildtree(root*2+1,(l+r)/2+1,r); 22 } 23 int query(int p,int l,int r,int root) 24 { 25 node[root]--; 26 if(l==r)return l; 27 int m=(l+r)/2; 28 if(node[root*2]>=p) 29 return query(p,l,m,root*2); 30 else 31 return query(p-node[root*2],m+1,r,root*2+1); 32 } 33 int main() 34 { 35 int n,i,j; 36 while(~scanf("%d",&n)) 37 { 38 for(i=0;i<n;i++) 39 scanf("%d%d",&pos[i].p,&pos[i].v); 40 buildtree(1,1,n); 41 for(i=n-1;i>=0;i--) 42 num[query(pos[i].p+1,1,n,1)]=pos[i].v; 43 for(i=1;i<n;i++) 44 printf("%d ",num[i]); 45 printf("%d\n",num[n]); 46 } 47 return 0; 48 }
标签:
原文地址:http://www.cnblogs.com/zero-zz/p/4701888.html