QOJ.ac

QOJ

시간 제한: 2 s 메모리 제한: 512 MB 총점: 100

#18798. Sequence and Query 40

통계

Given a sequence $A_1, A_2, \ldots, A_N$ of length $N$ and an integer $M$. Process $Q$ queries. Each query consists of two integers $d$ and $k$, and the following operations are performed in order:

  • Construct a new sequence $B_i = (A_i + d) \bmod M$ ($1 \le i \le N$).
  • For all $i$ ($1 \le i \le N$), define the $i$-th suffix as $B_i, B_{i+1}, \ldots, B_N$.
  • Output the index of the $K$-th lexicographically smallest suffix of the sequence $B$.

Input

The first line contains the sequence length $N$ and integer $M$.

The second line contains $A_1, A_2, \ldots, A_N$.

The third line contains the number of queries $Q$.

The next $Q$ lines each contain a query $d$ and $k$.

Output

For each query, output the index of the $K$-th lexicographically smallest suffix of the sequence $B$, each on a new line.

Note

In the first query, the sequence $B = [5, 2, 0, 5, 5]$. Sorting all suffixes lexicographically yields $[0, 5, 5]$, $[2, 0, 5, 5]$, $[5]$, $[5, 2, 0, 5, 5]$, $[5, 5]$. The 4th lexicographically smallest suffix has index $1$.

Examples

Input 1

5 6
1 4 2 1 1
3
4 4
2 3
1 1

Output 1

1
1
5

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.