zoj3635-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 ktickets 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 n integers 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 m integers, 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

 地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3635

跟上道题一样,什么都没变,就是输入输出注意一下格式,感觉比上道还简单,不过题意好难理解,各种翻译和解释,终于明白了。。。。。。。

5             //n:几个人和几个数
2 3 3 2 1     //a[0]~a[n-1]表示第i个人坐到第a[i]个空座位上(空座位!)
5            //m:输出几个人
2 3 4 5 1    //输出的几个人的编号,如2坐在了哪里,3坐在了哪里

理解了题目就好了,还是建线段树,初始为1,然后跟上题一样,不写了。。。。。。。。代码
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <queue>
#include <algorithm>
#define lson l , m , rt << 1//左儿子,rt/2
#define rson m + 1 , r , rt << 1 | 1//右儿子,rt/2+1
using namespace std;
const int maxn = 55555;
int sum[maxn<<2];
void PushUP(int rt) {
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];//root=lson+rson
}
void build(int l,int r,int rt) {
    sum[rt]=1;//0,1替换,1的和可以算出个数
    if (l == r) {
        return ;
    }
    int m = (l + r) >> 1;//右移一位相当于除以二
    build(lson);
    build(rson);
    PushUP(rt);
}
void update(int p,int l,int r,int rt)//单点替换
 {
    if (l == r) {
        sum[rt] =0;
        return ;
    }
    int m = (l + r) >> 1;
    if (p <= m) update(p , lson);
    else update(p , rson);
    PushUP(rt);
}
int query(int p ,int l,int r,int rt)//区间求和
 {
    if (l==r) {
        return l;
    }
    int m = (l + r) >> 1;
    if (sum[rt<<1]<p) return query(p -sum[rt<<1], rson);
    else return query(p , lson);

}

int main()
{
    int n,m,a[50010],q[50010],b[50010];
    while(scanf("%d",&n)!=EOF){
            build(1,n,1);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            b[i+1]=query(a[i],1,n,1);
            update(b[i+1],1,n,1);
        }
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d",&q[i]);
            if(i<m-1)printf("%d ",b[q[i]]);
            else printf("%d\n",b[q[i]]);
        }

    }
    return 0;
}

就这样吧,虽然还没学完,但明天开始学KMP&tire了,线段树先告一段落吧。。。。。。。。。开学我还会再做线段树的。。。。。等我啊。。。。。。。

posted @ 2013-08-01 00:25  SunnySnail  阅读(156)  评论(0编辑  收藏  举报