Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 3635.
Cinema in Akba (CIA)Is a small but very popular cinema inAkihabara. Every night the cinema is full of people. The layoutCIAIs 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 ticketCIAIs strange, too. There areNSeats in
CIAAnd they are numbered from 1NIn order. Apparently,
NTickets will be sold everyday. when buying a ticket, if there areKTickets left, your ticket number will be an integerI(1 ≤
I≤K), And you shoshould chooseIthEmpty seat (not occupied by others) and sit down for the film.
On November, 11th,NGeeks goCIATo celebrate their anual Festival. The ticket number ofIthGeek 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 integerN(1 ≤N≤ 50000), the number of geeks as well as the number of seats inCIA. Then follows a line containing
NIntegersA1,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 isMIntegers,
Q1,Q2,...,QM(1 ≤
Qi≤N), Each represents the geek's number and you shoshould help him find his seat.
Output
For each test case, printMIntegers in a line, seperated by one space.IthInteger is the seat number of
QithGeek.
Sample Input
31 1 131 2 352 3 3 2 152 3 4 5 1
Sample output
1 2 34 5 3 1 2
Question Analysis:
There are n seats, numbered 1 ~ N. Each time a person gets a ticket, there are K tickets left. Then, based on the relative position of the vacant space, the number of the seats that each person finally takes.
Use the line segment tree to store the number of empty seats in the interval. Then, each time you query the number of seats, locate the seat number based on the comparison of the positions of the relative vacancies.
AC code:
# Include <stdio. h ># include <iostream> # define n 50001 using namespace STD; int f [N]; // record the seat number at the relative position int B [N]; struct segetree {int left, right; int account;} tree [N * 4]; void build (int l, int R, int K) {If (L = r) {tree [K]. left = L; tree [K]. right = r; tree [K]. account = 1 ;}else {int mid = (L + r)/2; tree [K]. left = L; tree [K]. right = r; tree [K]. account = r-L + 1; build (L, mid, k <1); Build (Mid + 1, R, (k <1) | 1 );}} int Update (int I, int K) {int TEM P; If (tree [K]. left = tree [K]. right) {tree [K]. account = 0; // This position has been occupied, so the number of vacant positions is 0 return tree [K]. left;} else {int mid = tree [k <1]. account; if (I <= mid) temp = Update (I, K <1); elsetemp = Update (I-mid, (k <1) | 1 );} tree [K]. account = tree [2 * K]. account + tree [2 * k + 1]. account; // return temp;} int main () {int num, I, j, query; while (scanf ("% d ", & num )! = EOF) {build (1, num, 1); for (I = 1; I <= num; ++ I) {scanf ("% d", & J ); f [I] = Update (J, 1) ;}scanf ("% d", & query); For (j = 1; j <= query; ++ J) {scanf ("% d", & B [J]) ;}for (j = 1; j <query; ++ J) printf ("% d ", f [B [J]); printf ("% d \ n", F [B [J]);}