描述

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

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

思路

先说题意,电影院里面有n个座位,作为的序号是从1~n,现在有n个人要坐座位,每个票上面有一个数字a[i],这个数字代表这个人应该坐在剩下的空位中的第几个,按照题目给出的顺序依次坐,最后有m组询问,问谁坐在哪个位置。

我们用线段树来位数剩下的座位的数量,初始化的时候线段树每个节点为1,更新的时候查看左儿子有没有位置,然后再看右儿子,更新即可

代码

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <sstream>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define inf 0x3f3f3f3f
typedef long long ll;
const int N = 50000 + 20;
int sum[N << 2], ans[N];
void pushup(int rt)
{sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void build(int l, int r, int rt)
{if (l == r){sum[rt] = 1;return;}int m = (l + r) >> 1;build(lson);build(rson);pushup(rt);
}
int update(int l, int r, int rt, int x)
{if (l == r){sum[rt] = 0;return l;}int ret, m = (l + r) >> 1;if (x <= sum[rt << 1])ret = update(lson, x);elseret = update(rson, x - sum[rt << 1]);pushup(rt);return ret;
}
int main()
{//freopen("in.txt", "r", stdin);int n, x, m;while (~scanf("%d", &n)){mem(ans, 0);build(1, n, 1);for (int i = 1; i <= n; i++){scanf("%d", &x);ans[i] = update(1, n, 1, x);}scanf("%d", &m);for (int i = 1; i <= m; i++){scanf("%d", &x);if (i == 1)printf("%d", ans[x]);elseprintf(" %d", ans[x]);}puts("");}return 0;
}

ZOJ3635 Cinema in Akiba(线段树)相关推荐

  1. ZOJ3635 Cinema in Akiba

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3635 题意:有n个geek,每个人一张票,每个票号ai表示这个geek要坐 ...

  2. 浙大月赛C题(2012/8)Cinema in Akiba(线段树)

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=4791 (1)第一次写浙大的题目,这题让我十分意外,基本的线段树类 ...

  3. 二逼平衡树——树套树(线段树套Splay平衡树)

    题面 Bzoj3196 解析 线段树和Splay两棵树套在一起,常数直逼inf,但最终侥幸过了 思路还是比较简单, 在原数组维护一个下标线段树,再在每一个线段树节点,维护一个对应区间的权值Splay. ...

  4. 线段树——HDU - 1698

    题目含义 就是初始化一堆数为1 可以经过操作把一个区间的数都改变 并求这堆数的总大小 题目分析 有一个 #include<iostream> #include<stdio.h> ...

  5. BZOJ.1558.[JSOI2009]等差数列(线段树 差分)

    BZOJ 洛谷 首先可以把原序列\(A_i\)转化成差分序列\(B_i\)去做. 这样对于区间加一个等差数列\((l,r,a_0,d)\),就可以转化为\(B_{l-1}\)+=\(a_0\),\(B ...

  6. 【线段树分治 线性基】luoguP3733 [HAOI2017]八纵八横

    不知道为什么bzoj没有HAOI2017 题目描述 Anihc国有n个城市,这n个城市从1~n编号,1号城市为首都.城市间初始时有m条高速公路,每条高速公路都有一个非负整数的经济影响因子,每条高速公路 ...

  7. [bzoj1582][Usaco2009 Hol]Holiday Painting 节日画画_线段树

    Holiday Painting 节日画画 bzoj-1582 Usaco-2009 Hol 题目大意:给定两个n*m的01网格图.q次操作,每次将第二个网格图的子矩阵全部变成0或1,问每一次操作后两 ...

  8. codefores 786B. Legacy(最短路,线段树优化拆点,好题)

    题目链接 B. Legacy time limit per test2 seconds memory limit per test256 megabytes inputstandard input o ...

  9. 【题解】BZOJ 3065: 带插入区间K小值——替罪羊树套线段树

    题目传送门 题解 orz vfk的题解 3065: 带插入区间K小值 系列题解 一 二 三 四 惨 一开始用了一种空间常数很大的方法,每次重构的时候merge两颗线段树,然后无限RE(其实是MLE). ...

最新文章

  1. 网站外链优化需要注意哪些事项?
  2. Java基础笔记18
  3. IT技术文章示例(附源码)
  4. Adobe Acrobat Reader 快捷键
  5. SLF4j、log4j管理系统日志(Maven)
  6. python中class __str__怎么用_python中下划线的作用
  7. macOS 10.13 安装Virtualbox失败
  8. android horizontalscrollview 动画,Android HorizontalScrollView左右滑动效果
  9. GBT19056精要
  10. 超分辨率技术,随机噪声
  11. django-模型类操作-初期阶段-小结
  12. 微信小程序 --- 动态获取input的value
  13. 区块链100问:区块链到底能不能篡改?
  14. python之调用科大讯飞的在线语音识别
  15. 关于《JavaScript百炼成仙》电子版,在线阅读地址~
  16. 蔡勒公式与Python
  17. 让人们久等了的TCP BBR v2.0快要出炉了!
  18. 字节编程题 雀魂启动
  19. Qt5软键盘实现中文拼音输入法
  20. 剑指Offer题目详解(CPP、JAVA)

热门文章

  1. 搜索之线性搜索和二分搜索
  2. 关于光速(c)测定的故事
  3. springboot kafka 怎样关闭kafka的在控制台打印的日志
  4. HTTP 502状态码
  5. 钉钉企业内部H5应用IOS点击下载pdf附件后乱码问题
  6. Java项目实战:根据出生日期计算(判断)星座
  7. 【新番尝鲜】超越宇宙的少女——不明生物参见
  8. 使用Python和OpenCV在线打乒乓球!
  9. 小波变换-全部matlab函数
  10. ‘StringBuilder‘ can be replaced with ‘String‘