Rollback
Time limit2sMemory limit512 MB
For each online query (l, k), find the smallest r such that the subarray a[l..r] contains at least k distinct server numbers, or report 0 if impossible.
- Level
Hard8 of 10
- Topics
- Binary search, Prefix sum, Array, Sorting
- Solved
- No attempts yet
Problem
Sergey works as a system administrator at a very large company. Naturally, his duties include backing up the information stored on various servers and <> to a previous version when problems arise.
Right now Sergey is fighting a shortage of space for storing recovery information. He decided to move part of the information to new servers. Unfortunately, if something goes wrong during the move, he will not be able to roll back, so the move has to be planned carefully.
At the moment Sergey has recovery points of various servers, numbered from 1 to . The recovery point with number can roll back server . Sergey decided to split the move into stages, and at each stage, if problems arise, the recovery points with numbers will be available.
To plan the data move optimally, Sergey needs to answer queries: for a given , what is the minimum such that at least distinct servers are available during the move?
Help Sergey.
Input
The first line of the input file contains two integers and separated by a space: the number of recovery points and the number of servers (). The second line contains integers : the server numbers that correspond to the recovery points ().
The third line of the input file contains : the number of queries to process (). While processing the queries, you must maintain a number , initially equal to 0. Each query is given by a pair of numbers and ; use them to obtain the query data as follows: , (, ). Let the answer to the -th query be . After completing this query, assign the value .
Output
For each query, output one number: the minimum sought, or 0 if no such exists.