Under construction

Still being built. Things may change

Longest distinct segment queries

Interview

Time limit1sMemory limit1024 MB

Summary
Given a sequence of N values and Q queries (l, r), find the length of the longest contiguous subarray inside [l, r] whose values are all distinct.
Level

Medium6 of 10

Topics
Array, Binary search
Solved
No attempts yet

Problem

You are given a sequence A1,A2,…,ANA_1, A_2, \ldots, A_N of length NN. Process the following query.

  • ll rr: Print the largest size among intervals [s,e][s, e] with l≤s≤e≤rl \le s \le e \le r that contain only distinct values. The size of interval [s,e][s, e] is e−s+1e-s+1.

An interval [s,e][s, e] with only distinct values must not contain ii and jj with Ai=AjA_i = A_j (s≤i<j≤es \le i < j \le e).

Input

The first line contains NN. (1≤N≤500 0001 \le N \le 500\,000)

The second line contains the integer sequence A1,A2,…,ANA_1, A_2, \ldots, A_N, separated by spaces. (1≤Ai≤N1 \le A_i \le N)

The third line contains QQ. (1≤Q≤500 0001 \le Q \le 500\,000)

From the fourth line through line Q+3Q+3, each query is given on its own line. (1≤l≤r≤N1 \le l \le r \le N)

Output

For each query, print the answer on its own line.

Examples1

  1. Example 1

    Input
    7
    3 1 3 2 2 2 2
    3
    2 3
    1 5
    1 3
    
    Expected output
    2
    3
    2