QOJ.ac

QOJ

Time Limit: 5 s Memory Limit: 1024 MB Total points: 100

#4339. Frog Problem

Statistics

Given a sequence of length $n$, answer $q$ queries. For each query $[l, r]$, find the shortest subsegment $[l', r']$ such that all numbers appearing in $[l, r]$ also appear in $[l', r']$. You only need to output the length of $[l', r']$, which is $r' - l' + 1$.

Input

The first line contains a positive integer $n$. The next line contains $n$ positive integers, representing the elements of the sequence. The next line contains a positive integer $q$. The next $q$ lines each contain two positive integers $l, r$, representing a query.

Output

For each query, output a single positive integer on a new line representing the answer.

Subtasks

Subtask Score Constraints
1 20 pts $n, q, a_i \le 5 \times 10^3$
2 30 pts $n, q, a_i \le 5 \times 10^4$
3 50 pts No special restrictions

For all data, $1 \le n, q, a_i \le 2 \times 10^6$.

Examples

Input 1

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

Output 1

2
3
3

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.