前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ZOJ 3635 Cinema in Akiba(线段树)

ZOJ 3635 Cinema in Akiba(线段树)

作者头像
全栈程序员站长
发布2022-07-07 17:49:47
1570
发布2022-07-07 17:49:47
举报

大家好,又见面了,我是全栈君。

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
代码语言:javascript
复制
3
1 1 1
3
1 2 3
5
2 3 3 2 1
5
2 3 4 5 1
Sample Output
代码语言:javascript
复制
1 2 3
4 5 3 1 2
代码语言:javascript
复制
题意:就是一排人看电影,票上安排给某人的位置是第几个空位。让你找出某些人的相应座位号。
代码语言:javascript
复制
线段树保存空位个数进行处理。
代码语言:javascript
复制
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int maxn=50000+100;
int sum[maxn<<2];
int ans[maxn];
int n,m;
void pushup(int rs)
{
    sum[rs]=sum[rs<<1]+sum[rs<<1|1];
}
void build(int rs,int l,int r)
{
    sum[rs]=r-l+1;
    if(l==r)  return ;
    int mid=(l+r)>>1;
    build(rs<<1,l,mid);
    build(rs<<1|1,mid+1,r);
    pushup(rs);
}
int update(int rs,int l,int r,int x)
{
 //    cout<<"2333 "<<l<<" "<<r<<endl;
     if(l==r)
     {
         sum[rs]=0;
         return l;
     }
     int ret;
     int mid=(l+r)>>1;
     if(x<=sum[rs<<1])  ret=update(rs<<1,l,mid,x);
     else            ret=update(rs<<1|1,mid+1,r,x-sum[rs<<1]);
     pushup(rs);
     return ret;
}
int main()
{
    int x;
    while(~scanf("%d",&n))
    {
        CLEAR(sum,0);
        CLEAR(ans,0);
        build(1,1,n);
        REPF(i,1,n)
        {
            scanf("%d",&x);
            ans[i]=update(1,1,n,x);
        }
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d",&x);
            printf(m==0?"%d\n":"%d ",ans[x]);
        }
    }
    return 0;
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116482.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Input
  • Output
  • Sample Input
  • Sample Output
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档