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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Linux04:(4.6k)vim编辑器「建议收藏」

    Linux04:(4.6k)vim编辑器「建议收藏」文章目录Linux_day04一.vim编辑器vim的三种模式1.命令模式2.末行模式3.编辑模式实用功能扩展内容==1.vim的配置文件==2.异常退出问题3.别名机制4.退出方式补充一些win10下的快捷键Linux_day04一.vim编辑器vim的三种模式命令模式不能对文件直接编辑,但可以通过快捷键删除行,复制,粘贴,移动光标等编辑模式-输入末行模式可以在末行输入命令:搜索,替换,保存,退出,撤销vim打开文件的方式:1.#vim 文件路径——直接打开文件(光

    2022年8月9日
    5
  • 小米手机1亿像素跟相机(2020相机新品)

    小米集团旗下品牌Redmi再度发布多款“性价比之王”手机新品。11月26日,小米发布RedmiNote9系列的Note9Pro、Note95G和Note94G三款手机新品,三款手机价格均位于“千元档”甚至低于千元,再度成为市场上同价位机型中的“性价比之王”。对此,小米集团副总裁、中国区总裁、Redmi品牌总经理卢伟冰重申,“Redmi的想法很简单,就是为用户做好产品,然后价格卖的尽量厚道,坚持高端产品大众化,大众产品品质化。”此次小米推出的Note9Pro沿袭了之前的Note8Pro在超清

    2022年4月10日
    44
  • emwin用户设置界面_强制刷新快捷键

    emwin用户设置界面_强制刷新快捷键1、在对话框回调函数中定时重绘按键_cbDialogHome(WM_MESSAGE*pMsg){ Switch(pMsg->MsgId){ CaseWM_INIT_DIALOG: WM_CreateTimer(pMsg->hWin,0,100,0);//创建窗口定时器 CaseWM_PAINT://窗口重绘 CaseWM_NOTIFY_

    2022年10月15日
    2
  • array.sort排序_javascript数组排序

    array.sort排序_javascript数组排序数组sort排序方法Array数组对象中的sort方法是根据数组中数组元素的字符编码进行排序的,所以对数字的排序,会跟想要的升序结果不一样通过设置sort()方法的参数可以按照自定义的排序方式对数组进行排序,sort()方法的参数是一个函数,需要自定义该函数,sort()方法会根据函数的返回结果对数组进行排序functioncompare(a,b){returna-b;}…

    2022年8月12日
    8
  • java ee eclipse使用教程(使用maven创建web项目)

    笔者开发javaee项目时惯用myeclipse,但由于个人笔记本性能较低,myeclipse对内存的消耗极大,所以考虑换成eclipse开发。本文介绍eclipse配置javaee开发环境的一些体会。配置tomcat与myeclipse配置tomcat的方式不同,eclipse需要先安装tomcat插件,再指定tomcat的路径。第一步:将解压后的zip文件置于eclipse/plugins目录…

    2022年4月10日
    130
  • Swift控制语句

    前言Swift提供了类似C语言的流程控制结构,包括可以多次执行任务的for和while循环。还有基于特定条件选择执行不同代码分支的if、guard和switch语句,还有控制流程跳转到其他代码的br

    2021年12月27日
    38

发表回复

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

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