线段树zoj3635

来源:互联网 发布:间接网络外部性 编辑:程序博客网 时间:2024/05/14 21:39

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 ≤ ik), 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 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 inCIA. Then follows a line containing n integers a1,a2, ..., an (1 ≤ ain - 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 ≤ qin), 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 the qith geek.

Sample Input

31 1 131 2 352 3 3 2 152 3 4 5 1

Sample Output

1 2 34 5 3 1 2
很简单不多说

#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=50010;int n,m;int ans[maxn];struct IntervalTree{    int sum[maxn<<3];    void pushup(int o)    {        sum[o]=sum[o<<1]+sum[o<<1|1];    }    void build(int o,int l,int r)    {        sum[o]=0;        if(l==r)        {            sum[o]=1;            return ;        }        int mid=(l+r)>>1;        build(o<<1,l,mid);        build(o<<1|1,mid+1,r);        pushup(o);    }    void update(int o,int l,int r,int x,int val)    {        if(l==r)        {            sum[o]=0;            return ;        }        int mid=(l+r)>>1;        if(x<=mid)update(o<<1,l,mid,x,val);        else update(o<<1|1,mid+1,r,x,val);        pushup(o);    }    int query(int o,int l,int r,int k)    {        if(l==r)return l;        int mid=(l+r)>>1;        if(sum[o<<1]>=k)return query(o<<1,l,mid,k);        else return query(o<<1|1,mid+1,r,k-sum[o<<1]);    }}tree;int main(){    while(scanf("%d",&n)!=EOF)    {        tree.build(1,1,n);        for(int i=1;i<=n;i++)        {            int x;            scanf("%d",&x);            int pos=tree.query(1,1,n,x);            ans[i]=pos;            tree.update(1,1,n,pos,1);        }        scanf("%d",&m);        for(int i=0;i<m;i++)        {            int x;            scanf("%d",&x);            if(i==0)printf("%d",ans[x]);            else printf(" %d",ans[x]);        }        printf("\n");    }    return 0;}




0 0
原创粉丝点击