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


相关推荐

  • smalldatetime数据类型「建议收藏」

    smalldatetime数据类型「建议收藏」smalldatetime共需要4个字节,其中两个字节表示1900-1-1之后的所有天数,另外两个字节表示午夜后一分钟为单位的时间,支持范围从1900-1-1到2079-6-6转载于:https://www.cnblogs.com/Junelee1211/archive/2011/07/08/2100694.html…

    2022年5月19日
    34
  • java8以后字符串常量池的位置,以及元空间的探秘,使用VisualVM进行实战验证

    java8以后字符串常量池的位置,以及元空间的探秘,使用VisualVM进行实战验证  在网上看了很多博客,解释也比较多,关于字符串常量池的具体位置难以分辨谁真谁假。  对于jdk8以后的版本有人说字符串常量池在元空间中,也有人说字符串常量池存在堆中。  到底谁说的对?他们的说法有依据吗?  今天让我们来一起探讨一下这个问题有人说字符串常量池在java堆中,可又有人说常量池存在元空间中。分享几篇知乎文章关于jvm运行时数据区的模型:1、面试官|JVM为什么使用元空间替换了永久代?2、Java方法区与元空间为了解决这个问题,下面我们通过Idea、VisualVm

    2022年7月28日
    18
  • Java设计模式简介(一):创建型模式

    Java设计模式简介(一):创建型模式

    2021年4月8日
    150
  • c#语言_c# ref

    c#语言_c# refStringBuilder用于大量的字符串的修改的地方,比如要大量的连接字符串时,使用它能节省内存空间。StringBuildertestStr=newStringBuilder(“abcdef:ggg”);//testStr.AppendFormat($”{s}”);testStr.Append($”{s}”);intlen=testStr.Length;

    2022年10月21日
    4
  • phpstorm安装+新建项目+phpstorm中文版

    phpstorm安装+新建项目+phpstorm中文版一 安装 phpstorm1 运行安装包 2 点击 next3 选择安装路径点击 next4 我的电脑系统是 windows64 位所以选择 64 bit5 点击 INSTALL 安装 6 安装成功后运行 phpstorm 选择 evaluateforf 然后点击 evaluate 二 新建项目 1 点击 newproject2 选择项目路径点击 create 创建项目三 中文 1 file gt settings2 plugins gt 搜索 chin

    2025年7月5日
    2
  • HDU1069_Monkey and Banana【LCS】

    HDU1069_Monkey and Banana【LCS】

    2022年1月8日
    43

发表回复

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

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