#SGM0000067. Truy vấn k trực tuyến (K-Query Online)
Truy vấn k trực tuyến (K-Query Online)
K-Query Online
Source: SPOJ
Version: Phuoc Hung OJ Extended
Problem
Queries are online. Let last be the previous answer, initially . Decode , , . Clamp to at least and to at most . If , the answer is ; otherwise count values greater than in . Set last to that answer.
Input
The first line contains , the second line contains the array, the third line contains , and each following line contains encoded values .
Output
Print each decoded query answer in order.
Subtasks
Subtask 1 (20%)
- , number of queries .
- All other conditions are the same as Subtask 3.
Subtask 2 (30%)
- , number of queries .
- All other conditions are the same as Subtask 3.
Subtask 3 (50%)
Example
Input
6
8 9 3 5 1 9
5
2 3 5
3 3 7
0 0 11
0 0 2
3 7 4
Output
1
1
0
0
2
Explanation
Every query depends on the previous result, so queries cannot be reordered. The sample answers are 1,1,0,0,2.