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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • Bozz Nuster_Collectivum XXVIII

    Bozz Nuster_Collectivum XXVIII这篇文章主要讲的是在Libprotobuf-mutator与LibFuzzer联合使用的基础上,加上custommutator功能。首先需要明确的是为什么要这么做,那么假设b字段只有为”FUZZ”或”PWN”两个字符的时候才能进入下一个程序分支的情况,当然LibFuzzer也可以在代码覆盖率的加持下进入下一个程序分支,但如果你通过逆向的方式已经知道了这个关键点,难道还需要等LibFuzzer跑出这两个字符串吗?

  • 关于AjaxPro用法[通俗易懂]

    关于AjaxPro用法[通俗易懂]特点是前后台传输数据特别方便,可以直接跟后台方法进行访问,中间用数据JASON数据传输这一切她都已经帮你做了。一、配置AjaxPro:1.下载AjaxPro.2.dll并添加到工程里,如图:在web.config中添加注册信息在后台Page_Load注册下,如下图:这样就配置好了。二、后台代码:[AjaxPro.AjaxMethod]加上此标记,前台可以直接调用此方法三、

  • 差分进化算法之Matlab实现「建议收藏」

    差分进化算法之Matlab实现「建议收藏」一、介绍差分进化算法是模拟自然界生物种群以“优胜劣汰,适者生存”为原则的进化发展规律而形成的一种随机启发式搜索算法。其保留了基于种群的全局搜索策略,采用实数编码,基于差分的简单变异操作和一对一的竞争生存策略,比遗传算法更简单。同时,差分进化算法独特的记忆能力使其可以动态的跟踪当前的搜索情况,及时调整搜索测量,因此具有较强的全局收敛能力。目前为止,差分进化算法已经成为一种求解非线性,不可微,多极…

    2022年10月24日
  • VC中获取窗体句柄的各种方法

    VC中获取窗体句柄的各种方法

    2021年12月10日
  • 锁文件夹怎么锁_密码锁有没有开锁记录

    锁文件夹怎么锁_密码锁有没有开锁记录1.文件锁可以对将要修改文件的某个部分进行加锁,精确控制到字节通过fcntl()函数来进行设置文件锁fcntl(intfd,intcmd,………);参数:fd:文件描述符cmd

  • QT QStringList 与 QString 常用方法

    QT QStringList 与 QString 常用方法本文汇集了QString与(QStringList|QByteArray)之间的转换,以及QString、QStringList的一些常用方法。

发表回复

您的电子邮箱地址不会被公开。

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