1. 程式人生 > >ZOJ 題目3635 Cinema in Akiba(線段樹插空)

ZOJ 題目3635 Cinema in Akiba(線段樹插空)

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 of CIA 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 integer i (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 the ith geek is ai. 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 in CIA

. Then follows a line containing nintegers a1a2, ..., an (1 ≤ ai ≤ n - i + 1), as described above. The third line is an integer m (1 ≤ m ≤ 3000), the number of queries, and the next line is mintegers, q1q2, ..., 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. The ith integer is the seat number of the qith geek.

Sample Input

3
1 1 1
3
1 2 3
5
2 3 3 2 1
5
2 3 4 5 1

Sample Output

1 2 3

4 5 3 1 2

題目大意:n個人,給出n個人要座的位置,要做的位置有人了,就往後找位置坐,下邊q次查詢

每次查詢x,第x個人坐在哪個位置

ac程式碼

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define N 50050
#define INF 0x3f3f3f3f
using namespace std;
int node[N<<2],ans[N];
struct s
{
    int p,v;
}b[N];
void pushup(int tr)
{
    node[tr]=node[tr<<1]+node[tr<<1|1];
}
void build(int l,int r,int tr)
{
    if(l==r)
    {
        node[tr]=1;
        return ;
    }
    int mid=(l+r)>>1;
    build(l,mid,tr<<1);
    build(mid+1,r,tr<<1|1);
    pushup(tr);
}
void update(int pos,int id,int l,int r,int tr)
{
    if(l==r)
    {
        node[tr]=0;
        ans[id]=l;
        return;
    }
    int mid=(l+r)>>1;
    if(pos<=node[tr<<1])
    {
        update(pos,id,l,mid,tr<<1);
    }
    else
        update(pos-node[tr<<1],id,mid+1,r,tr<<1|1);
    pushup(tr);
}
int main()
{
    int n;
    //scanf("%d",&t);
    while(scanf("%d",&n)!=EOF)
    {
        int i;
        build(1,n,1);
        for(i=1;i<=n;i++)
        {
            int pos;
            scanf("%d",&pos);
            update(pos,i,1,n,1);
        }
        int q;
        scanf("%d",&q);
        for(i=0;i<q;i++)
        {
            int x;
            scanf("%d",&x);
            if(i)
                printf(" ");
            printf("%d",ans[x]);
        }
        printf("\n");
    }
}