Sequence and Queries 40
Time limit2sMemory limit512 MB
Given a sequence A and modulus M, each query shifts every value by d modulo M and asks for the start index of the k-th smallest suffix in lexicographic order.
- Level
Hard8 of 10
- Topics
- String, Sorting, Binary search
- Solved
- No attempts yet
Problem
You are given a sequence of length and an integer . Each query consists of two integers and . For each query, perform the following steps in order.
- Build a new sequence for .
- For every (), define the -th suffix as .
- Output the suffix number of the sequence that is -th smallest in lexicographic order.
Input
The first line contains and .
The second line contains .
The third line contains the number of queries .
Each of the next lines contains one query, the integers and .
Output
For each query, print on its own line the suffix number of the -th smallest sequence in lexicographic order.
Constraints
Hint
In the first query, the sequence is . Sorting all suffixes in lexicographic order gives , , , , . The suffix in 4th place has number 1.