1. 程式人生 > >Cinema in Akiba(線段樹求解位置)

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 n integers a1, a2, …, 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, 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. 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個人的票a[i],代表這個人要坐第幾個空位置(是空位置),有m個詢問q[i],問你q[i]坐在了幾號位置上。

思路:線段樹,求解第i個空位置在哪裡。

程式碼:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include<string.h>
#include <map>
#include<stack>
#include<queue>
#include <vector>
#include <set>
#include <math.h>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int maxn=5e4+9;

struct node
{
    int l,r;
    int sum;
} edge[maxn*4];
int a[maxn];

void build(int num,int l,int r)//建樹
{
    int mid=(l+r)/2;
    edge[num].l=l;
    edge[num].r=r;
    edge[num].sum=1;
    if(l==r)
        return ;
    build(num<<1,l,mid);
    build(num<<1|1,mid+1,r);
    edge[num].sum=edge[num<<1].sum+edge[num<<1|1].sum;
}
void update(int num,int x,int i)//查詢第x個空位在哪
{
    int l=edge[num].l,r=edge[num].r,mid=(l+r)/2;
    if(l==r)
    {
        edge[num].sum--;
        a[i]=l;
        return ;
    }
    if(edge[num<<1].sum>=x)
        update(num<<1,x,i);
    else update(num<<1|1,x-edge[num<<1].sum,i);
    edge[num].sum=edge[num<<1].sum+edge[num<<1|1].sum;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        build(1,1,n);
        for(int i=0; i<n; i++)
        {
            int x;
            scanf("%d",&x);
            update(1,x,i+1);
        }
        int m;
        int x;
        scanf("%d",&m);
        for(int i=0; i<m-1; i++)
        {
            scanf("%d",&x);
            printf("%d ",a[x]);
        }
        scanf("%d",&x);
        printf("%d\n",a[x]);
    }

    return 0;
}