程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> ZOJ 3635 Cinema in Akiba[ 樹狀數組 ]

ZOJ 3635 Cinema in Akiba[ 樹狀數組 ]

編輯:C++入門知識

ZOJ 3635 Cinema in Akiba[ 樹狀數組 ]


傳送門:ZOJ 3635

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 ≤ 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 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 a1, a2, ..., an (1 ≤ ain - 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, 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. 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


思路:鏈表模擬一排座位1-n,不斷刪除,然後記錄座位號。用普通的鏈表模擬,必然TLE,so上塊狀鏈表,來彌補鏈表索引太慢的缺點。


代碼:


#include
#include
#include
#include
#include
#include
#include
#define INF INT_MAX
using namespace std;

struct node{
    int num[1000];
    int sz;
    node* next;
    node(int sz){
        this->sz=sz;
        next=0;
    }
}*head=NULL;

int local[50010];
node *get_pos(int &x)
{
    node *t=head->next;
    while(t->next&&x>t->sz)
    {
        x-=t->sz;
        t=t->next;
    }
    return t;
}

void del(node *pos,int x,int ii)
{
    local[ii]=(pos->num)[x-1];
    for(int i=x;isz;i++)
        (pos->num)[i-1]=(pos->num)[i];
    (pos->sz)--;
}

void build_bl(int n)
{
    node *ret=head;
    int sz=ceil(sqrt(n));
    int cur=0;
    for(int i=0;inum)[j]=(++cur);

        ret->next=t;
        ret=t;
    }

    if(n%sz)
    {
        node *t=new node(n%sz);
        for(int i=0;inum)[i]=(++cur);
        ret->next=t;
    }
}

void free_head()
{
    node *p=head;
    while(head->next)
    {
        head=head->next;
        free(p);
        p=head;
    }
}

int main()
{
    int n,m;
    while(scanf("%d",&n)!=EOF)
    {
        head=new node(0);
        build_bl(n);
        for(int i=1;i<=n;i++)
        {
            //printf("__hear\n");
            int x;
            scanf("%d",&x);
            node *pos=get_pos(x);
            del(pos,x,i);
        }
        free_head();
        scanf("%d",&m);
        int q;
        scanf("%d",&q);
        printf("%d",local[q]);
        for(int i=1;i



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved