ZOJ 3635 Cinema in Akiba(线段树)

ZOJ 3635 Cinema in Akiba(线段树)

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

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 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
题意:就是一排人看电影,票上安排给某人的位置是第几个空位。让你找出某些人的相应座位号。
线段树保存空位个数进行处理。

#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.net/116482.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • vsftpd安装包下载_vsftp搭建

    vsftpd安装包下载_vsftp搭建1、在profile文件中设置相关环境变量,允许使用代理上网,如果能联网则不需要配置主要为了能够使用第一种安装方式,如果不可行则可以使用第二种安装方式vi/etc/profile##在文件中加上以下配置信息,该配置为代理服务器地址,根据实际情况定义,该服务器地址必须可联网exporthttp_proxy=http://192.168.1.1:3128exporthttps_pro…

    2022年9月25日
    4
  • 7个免费的Linux FTP客户端工具[通俗易懂]

    7个免费的Linux FTP客户端工具[通俗易懂]在Dropbox、YouSendIt、idrive以及许多这样云存储和共享工具的帮助下,我们在互联网上发送和共享大型文件变得容易起来。所有这些网站都可以帮助你在互联网上传…

    2022年6月2日
    37
  • 关于widthStep造成的问题

    关于widthStep造成的问题最近遇到一个很奇怪的问题,一直没有解决,就是在A图像中设置一个ROI,将其clone给B,然后对B进行二值化,输入为B,输出为C,这时二值化完后的图像C跟ROI区域的图像区域不同。通过查看发现罪魁祸首是widthStep变了。无意中解决了这个问题,做法如下:方法1:就是在A图像中设置一个ROI,将其clone给B,新建一个C,大小、位数和通道数同B,将Bclone给C,然后对C进行

    2022年6月9日
    31
  • 截取一段电波,一不小心全变成了泡泡。你能够解密吗? “oooo0。000。ooo。o000。0oooo。0o。0o00。00o。00ooo。o00o。0000o。0oo。0oo。oo000。00oo。「建议收藏」

    截取一段电波,一不小心全变成了泡泡。你能够解密吗? “oooo0。000。ooo。o000。0oooo。0o。0o00。00o。00ooo。o00o。0000o。0oo。0oo。oo000。00oo。「建议收藏」看上面的符号像是我们遇到的莫斯电码,但是我们怎么把它转化为莫斯电码呢,仔细观察的o代表的是"."、0代表的是"-"、。代表的是"",所以我们可以写一个脚本将上面的符号转化为莫斯电码#特殊的莫斯电码importre#正则表达式s="oooo0。000。ooo。o000。0oooo。0o。0o00。00o。00ooo。"\"o00o。0000o。0oo。0oo。oo000…

    2022年6月24日
    79
  • php 替换字符串中的所有url 为a标签「建议收藏」

    php 替换字符串中的所有url 为a标签「建议收藏」functionformatUrlsInText($str){preg_match_all(‘/((http|ftp|https):\/\/)?([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/’,$str,$arr);if(!$arr[0]){…

    2022年5月10日
    35
  • 51单片机实现流水灯

    51单片机实现流水灯文章目录51单片机实现流水灯一、点亮第一个LED灯二、流水灯1.总线型控制2.延时函数3._crol_函数使用4.实现流水灯51单片机实现流水灯以下是本篇文章正文内容,下面案例可供参考一、点亮第一个LED灯#include<reg52.h>#defineuintunsignedint//简化定义#defineucharunsignedchar//同上sbitD1=P2^1;voidmain(){ D1=0;}代码中D1代表着位定义,相.

    2022年5月9日
    54

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号