ZOJ 3635 Cinema in Akba (line segment tree), zoj3635
Cinema in Akba (CIA)Is a small but very popular cinema inAkihabara. Every night the cinema is full of people. The layoutCIAIs very interesting, as there is only one row so that every audience can enjoy the wonderful movies without any annoyance by other audiences sitting in front of him/her.
The ticketCIAIs strange, too. There areNSeats inCIAAnd they are numbered from 1NIn order. Apparently,NTickets will be sold everyday. When buying a ticket, if there areKTickets left, your ticket number will be an integerI(1 ≤I≤K), And you shoshould chooseIthEmpty seat (not occupied by others) and sit down for the film.
On November, 11th,NGeeks goCIATo celebrate their anual festival. The ticket number ofIthGeek isAi. Can you help them find out their seat numbers?
Input
The input contains multiple test cases. Process to end of file.
The first line of each case is an integerN(1 ≤N≤ 50000), the number of geeks as well as the number of seats inCIA. Then follows a line containingNIntegersA1,A2,...,An(1 ≤Ai≤N-I+ 1), as described above. The third line is an integerM(1 ≤M≤ 3000), the number of queries, and the next line isMIntegers,Q1,Q2,...,Qm(1 ≤Qi≤N), Each represents the geek's number and you shoshould help him find his seat.
Output
For each test case, printMIntegers in a line, seperated by one space.IthInteger is the seat number ofQithGeek.
Sample Input
31 1 131 2 352 3 3 2 152 3 4 5 1
Sample Output
1 2 34 5 3 1 2
It is a row of people watching movies. the number of seats assigned to a person on the ticket is the first. Let you find the corresponding seat number of some people.
The number of reserved parts stored in the line segment tree is processed.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<limits.h>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int maxn=50000+100;int sum[maxn<<2];int ans[maxn];int n,m;void pushup(int rs){ sum[rs]=sum[rs<<1]+sum[rs<<1|1];}void build(int rs,int l,int r){ sum[rs]=r-l+1; if(l==r) return ; int mid=(l+r)>>1; build(rs<<1,l,mid); build(rs<<1|1,mid+1,r); pushup(rs);}int update(int rs,int l,int r,int x){ // cout<<"2333 "<<l<<" "<<r<<endl; if(l==r) { sum[rs]=0; return l; } int ret; int mid=(l+r)>>1; if(x<=sum[rs<<1]) ret=update(rs<<1,l,mid,x); else ret=update(rs<<1|1,mid+1,r,x-sum[rs<<1]); pushup(rs); return ret;}int main(){ int x; while(~scanf("%d",&n)) { CLEAR(sum,0); CLEAR(ans,0); build(1,1,n); REPF(i,1,n) { scanf("%d",&x); ans[i]=update(1,1,n,x); } scanf("%d",&m); while(m--) { scanf("%d",&x); printf(m==0?"%d\n":"%d ",ans[x]); } } return 0;}