#P5906. 【模板】回滚莫队&不删除莫队

【模板】回滚莫队&不删除莫队

Background

This is a template problem.

Problem Description

Given a sequence, you will be asked multiple queries on an interval [l,r][l,r]. For each query, find the maximum distance between two equal numbers within the interval.

The distance between two elements in the sequence means the absolute difference of their indices.

Input Format

The first line contains an integer nn, which is the length of the sequence.

The second line contains nn integers describing the sequence.

The third line contains an integer mm, which is the number of queries.

Then follow mm lines, each containing two integers l,rl, r, representing the query interval.

Output Format

Output mm lines. Each line contains one integer, the answer. If there are no two equal numbers in the interval, output 00.

8
1 6 2 2 3 3 1 6
5
1 4
2 5
2 8
5 6
1 7
1
1
6
1
6

Hint

Let aia_i denote the elements of the sequence.

For 40%40\% of the testdata, 1ai4001\leq a_i \leq 400, 1n,m600001\leq n,m\leq 60000.

For 100%100\% of the testdata, 1n,m21051\leq n,m\leq 2\cdot 10^5, 1ai21091\leq a_i\leq 2\cdot 10^9

Translated by ChatGPT 5