QOJ.ac

QOJ

Süre Sınırı: 5 s Bellek Sınırı: 1536 MB Toplam puan: 100

#18772. Sequences and Queries 14

İstatistikler

Given a sequence $A_1, A_2, \ldots, A_N$ of length $N$. Write a program to process the following queries:

  • l r k: Let $S$ be the set (without duplicates) consisting of the $l$-th through $r$-th numbers in $A$, sorted in ascending order. Output the $k$-th smallest number in $S$. If the $k$-th smallest number does not exist, output $-1$.

The sequence is indexed starting from $1$.

Input

The first line contains an integer $N$, the length of the sequence. ($1 \le N \le 100{,}000$)

The second line contains $N$ integers $A_1, A_2, \ldots, A_N$. ($1 \le A_i \le 10^9$)

The third line contains an integer $M$, the number of queries. ($1 \le M \le 100{,}000$)

The next $M$ lines each contain five integers $a_i, b_i, c_i, d_i, k_i$, used to construct the queries. ($0 \le a_i, b_i, c_i, d_i \le N$, $1 \le k_i \le N$)

The $l_i$ and $r_i$ of each query are constructed as follows:

Let $ans_{i-1}$ be the answer to the $(i-1)$-th query (the answer to the $0$-th query is $ans_0 = 0$).

  • $l_i = (a_i \times \max(ans_{i-1}, 0) + b_i) \bmod N + 1$
  • $r_i = (c_i \times \max(ans_{i-1}, 0) + d_i) \bmod N + 1$

If $l_i > r_i$, swap $l_i$ and $r_i$.

Output

For each query, output one line in order containing the answer.

Examples

Input 1

4
3 2 1 2
4
0 1 0 3 2
2 0 0 3 4
1 2 1 3 2
2 0 0 3 3

Output 1

2
-1
2
3

Input 2

10
9 10 6 3 8 4 9 6 4 10
10
0 2 0 9 3
1 9 1 3 3
1 8 1 0 3
1 2 1 7 2
1 6 1 2 3
1 4 1 3 1
1 6 1 6 1
1 4 1 8 1
1 9 1 3 3
1 9 1 2 1

Output 2

6
9
10
4
6
3
10
4
6
4

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.