线段树zoj3635
来源:互联网 发布:间接网络外部性 编辑:程序博客网 时间:2024/05/14 21:39
Cinema in Akiba (CIA) is a small but very popular cinema in Akihabara. Every night the cinema is full of people. The layout ofCIA 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 integeri (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 theith 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 inCIA. Then follows a line containing n integers a1,a2, ..., an (1 ≤ ai ≤n - i + 1), as described above. The third line is an integerm (1 ≤ m ≤ 3000), the number of queries, and the next line ism 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. Theith integer is the seat number of the qith geek.
Sample Input
31 1 131 2 352 3 3 2 152 3 4 5 1
Sample Output
1 2 34 5 3 1 2很简单不多说
#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=50010;int n,m;int ans[maxn];struct IntervalTree{ int sum[maxn<<3]; void pushup(int o) { sum[o]=sum[o<<1]+sum[o<<1|1]; } void build(int o,int l,int r) { sum[o]=0; if(l==r) { sum[o]=1; return ; } int mid=(l+r)>>1; build(o<<1,l,mid); build(o<<1|1,mid+1,r); pushup(o); } void update(int o,int l,int r,int x,int val) { if(l==r) { sum[o]=0; return ; } int mid=(l+r)>>1; if(x<=mid)update(o<<1,l,mid,x,val); else update(o<<1|1,mid+1,r,x,val); pushup(o); } int query(int o,int l,int r,int k) { if(l==r)return l; int mid=(l+r)>>1; if(sum[o<<1]>=k)return query(o<<1,l,mid,k); else return query(o<<1|1,mid+1,r,k-sum[o<<1]); }}tree;int main(){ while(scanf("%d",&n)!=EOF) { tree.build(1,1,n); for(int i=1;i<=n;i++) { int x; scanf("%d",&x); int pos=tree.query(1,1,n,x); ans[i]=pos; tree.update(1,1,n,pos,1); } scanf("%d",&m); for(int i=0;i<m;i++) { int x; scanf("%d",&x); if(i==0)printf("%d",ans[x]); else printf(" %d",ans[x]); } printf("\n"); } return 0;}
- 线段树zoj3635
- 线段树:ZOJ1610、ZOJ3635、ZOJ3633、ZOJ3349
- ZOJ3635 Cinema in Akiba
- zoj3635 Cinema in Akiba
- zoj3635-Cinema in Akiba 树状数组+二分
- 线段树?线段树!
- 线段树?线段树!
- 线段_线段树
- 线段_线段树
- ZOJ3635——Cinema in Akiba(树状数组+二分)
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- emacs的org-mode模式的链接
- The stack `XdataStack`()is outside the stack range
- HDU 1856 More is better
- 插入排序(insert sort)
- HDU4035 Maze 概率DP+图
- 线段树zoj3635
- 数码视讯非研发类考题
- zoj 3818 2014牡丹江网赛
- HDU4706 根据题目读题写程序
- C#判断点在直线上的方法
- STL algorithm算法copy_n(9)
- 2-5连接摄像头或avi文件进行拉普拉斯变换
- 顺序表:链表实现
- 对老年人来说,暖风机的“健康”作用要比浴霸强得多6666