Cinema in Akiba
Cinema in Akiba (CIA) was a small but very popular cinema in Akihabara. Every night the cinema is full of people. The layout of the CIA is very interesting, as there are only one row so that every audience can enjoy the wonderful MO VIEs without any annoyance by + audiences sitting in front of him/her.
The ticket for CIA is strange, too. There is n seats in CIA and they is numbered from 1 to in n order. Apparently, n tickets'll be sold everyday. When buying a ticket, if there is k tickets left, your ticket number would 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 them find out their seat numbers?
Input
The input contains multiple test cases. Process to end of file.
The first line of all case was 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 m is a integer (1≤ m ≤3000), the number of queries, and the next line is m integers, , q2 , ..., qm (1≤ qi ≤ n ), each represents the geek ' s number and your should help him find his seat.
Output
For each test case, print m integers in a line, seperated by one space. The integer is the seat number of the ith 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
The idea is clear.
Tree-like array + two-point
See note for details
1 /*************************************************************************2 > File name:code/zoj/3635.cpp3 > Author:111qqz4 > Email: [Email protected]5 > Created time:2015 October 22 Thursday 10:03 36 seconds6 ************************************************************************/7 8#include <iostream>9#include <iomanip>Ten#include <cstdio> One#include <algorithm> A#include <cmath> -#include <cstring> -#include <string> the#include <map> -#include <Set> -#include <queue> -#include <vector> +#include <stack> -#include <cctype> + A #defineYn hez111qqz at #defineJ1 CUTE111QQZ - #defineMS (A,X) memset (A,x,sizeof (a)) - using namespacestd; - Const intdx4[4]={1,0,0,-1}; - Const intdy4[4]={0,-1,1,0}; -typedefLong LongLL; intypedefDoubleDB; - Const intINF =0x3f3f3f3f; to Const intn=5e5+7; + intT[n],a[n]; - intans[n],k; the intn,m; * $ Panax Notoginseng intLowbit (intx) - { the returnx& (-x); + } A voidUpdate (intXintDelta) the { + for(inti = x; I <= N; i = i +lowbit (i)) - { $T[i] = T[i] +Delta; $ } - } - the intSumintx) - {Wuyi intres =0 ; the for(inti = x; I >=1; i = i-lowbit (i)) - { Wures = res +T[i]; - } About returnRes; $ } - - intBin_search (intLintR) - { A while(l<R) + { the intMid = (l+r) >>1; - if(Sum (mid) <k) $L = mid +1; the ElseR =mid; the } the returnR; the } - intMain () in { the #ifndef Online_judge theFreopen ("In.txt","R", stdin); About #endif the the while(SCANF ("%d", &n)! =EOF) the { + -MS (T,0); theMS (ANS,0);BayiMS (A,0); the for(inti =1; I <= N; i++) Update (I,1);//1 means not accounted for, and the initial position is not accounted for. the - for(inti =1; I <= N; i++) - { the thescanf"%d", &k);//each time to occupy the position of K is not occupied, because the occupied position is set to 0, so is to find the K-large. the the intPosi = Bin_search (1, n); -Ans[i] =posi; theUpdate (posi,-1);//set the occupied position to 0 the the }94 the the the 98scanf"%d",&m); About intx; - for(inti =1; I < m; i++)101 {102scanf"%d",&x);103printf"%d", ans[x]);104 } thescanf"%d",&x);106printf"%d\n", ans[x]);107 108 }109 the 111 #ifndef Online_judge the fclose (stdin);113 #endif the return 0; the}
View Code
Zoj 3635 Cinema in Akiba (tree-like array for K-Large)