ZOJ-3635-Cinema in Akba
Http://acm.zju.edu.cn/onlinejudge/showProblem.do? ProblemId = 4803
There are n slave 1 .. n indicates the number of seats in the order of time. When each guest arrives, he or she is seated in the first few empty seats. Finally, he/she asks a number of people where he/she is seated.
Line tree, And POJ-2828-Buy Tickets
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>using namespace std;#define N 50005struct cam{int x;int y;int count;}list[N*4];void build(int k,int x,int y){list[k].x=x;list[k].y=y;list[k].count=y-x+1;if(x==y)return;int mid=(x+y)/2;build(k<<1,x,mid);build(k<<1|1,mid+1,y);}int update(int k,int x){int ans;if(list[k].x==list[k].y){list[k].count=0;return list[k].x;}if(x<=list[k<<1].count)ans=update(k<<1,x);elseans=update(k<<1|1,x-list[k<<1].count);list[k].count=list[k<<1].count+list[k<<1|1].count;return ans;}int main(){int i,n,m,cur;int query[3005],ans[50005];while(scanf("%d",&n)!=EOF){build(1,1,n);for(i=1;i<=n;i++){scanf("%d",&cur);ans[i]=update(1,cur);}scanf("%d",&m);for(i=0;i<m;i++)scanf("%d",&query[i]);for(i=0;i<m;i++)printf(i==m-1?"%d\n":"%d ",ans[query[i]]);}return 0;}