Cinema in Akiba
Time Limit: 3 Seconds
Memory Limit: 65536 KB
Cinema in Akiba (CIA) is a small but very popular cinema in Akihabara. Every night the cinema is full of people. The layout ofCIA is 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 ticket for CIA is strange, too. There are n seats in CIA and they are numbered from 1 to n in order. Apparently, n tickets will be sold everyday. When buying a ticket, if there are k tickets left, your ticket number will be an integeri (1 ≤i ≤ k), and you should choose the ith empty seat (not occupied by others) and sit down for the film.
On November, 11th, n geeks go to CIA to celebrate their anual festival. The ticket number of theith geek 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 integer n (1 ≤ n ≤ 50000), the number of geeks as well as the number of seats inCIA. Then follows a line containingn integers a1,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 ism integers,q1, q2, ..., qm (1 ≤qi ≤ n), each represents the geek's number and you should help him find his seat.
Output
For each test case, print m integers in a line, seperated by one space. Theith integer is the seat number of theqith geek.
Sample Input
代码语言:javascript复制3
1 1 1
3
1 2 3
5
2 3 3 2 1
5
2 3 4 5 1
Sample Output
代码语言:javascript复制1 2 3
4 5 3 1 2
代码语言:javascript复制题意:一群人到电影院看电影,该电影的门票计算比较特殊,如:甲第一个拿到1号门票则位置为1,乙第二个拿票,票号也是1,则位置为2,因为1号位置已经被甲占了,乙的位置为剩下位置中的1号位置。
代码语言:javascript复制思路:线段树入门,套模板。
代码语言:javascript复制
代码语言:javascript复制#include<stdio.h>
#include<cstdio>
#define inf 55555
#define lson l,m,rt<<1
#define rson m 1,r,rt<<1|1
int sum[inf];
int a[inf];
int b[inf];
int c;
void build(int l,int r, int rt)
{
sum[rt]=r-l 1;
if(l==r)
return;
int m=(l r)>>1;
build(lson);
build(rson);
}
int update(int p,int l,int r,int rt)
{
sum[rt]--;
if(l==r)
return l;
int m=(l r)>>1;
if(p<=sum[rt<<1])
return update(p,lson);
else
return update(p-sum[rt<<1],rson);
}
int main()
{
int m,i,n;
while(scanf("%d",&n)!=EOF)
{
build(1,n,1);
for(i=1;i<=n;i )
{
scanf("%d",&c);
a[i]=update(c,1,n,1);
}
scanf("%d",&m);
for(i=1;i<=m;i )
{
scanf("%d",&b[i]);
}
for(i=1;i<m;i )
printf("%d ",a[b[i]]);
printf("%dn",a[b[m]]);
}
return 0;
}