hdu 2828 Buy Tickets
Buy Tickets
Time Limit : 8000/4000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 31 Accepted Submission(s) : 15
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.
#include <iostream> #include <cstdio> #include <algorithm> #include<vector> #include<cstring> using namespace std; int a[200005],b[200005],ans[200005]; int tem,t; struct node { int l,r,ksum; }tr[200005<<2]; void build(int n,int l,int r) { tr[n].l=l; tr[n].r=r; tr[n].ksum=r-l+1; if(l==r) return; int m=(l+r)>>1; build(n<<1,l,m); build(n<<1|1,m+1,r); } void f(int k,int v,int n) { int m=(tr[n].l+tr[n].r)>>1; if(tr[n].l==tr[n].r) { tr[n].ksum=0; ans[tr[n].l]=v; return; } if(tr[n<<1].ksum>=k) f(k,v,n<<1); else f(k-tr[n<<1].ksum,v,n<<1|1); tr[n].ksum=tr[n<<1].ksum+tr[n<<1|1].ksum; } int main() { while(~scanf("%d",&t)) { build(1,1,t); tem=0; for(int i=1;i<=t;i++) scanf("%d%d",&a[i],&b[i]); for(int i=t;i>0;i--) f(a[i]+1,b[i],1); for(int i=1;i<t;i++) cout<<ans[i]<<" "; cout<<ans[t]<<endl; } }