Under construction

Still being built. Things may change

줄넘기

Interview

Time limit2sMemory limit256 MB

Summary
For each query range [l, r], find the longest segment whose two ends have equal heights and no student in between has that same height, or print 0.
Level

Medium5 of 10

Topics
Hash map, Sorting, Implementation
Solved
No attempts yet

문제

NN students stand in a line to jump rope together. The height of student ii is AiA_i. You pick some of these students for a group jump rope. The process goes as follows.

First, you pick students with consecutive numbers to be the group. Suppose you pick students from number ss to number ee. Students ss and ee turn the rope, and the students in between jump over it.

If the two students holding the rope have different heights, the rope is hard to turn, so the two holders must have the same height. (That is, As=AeA_s = A_e.) Also, if another student in between has the same height, that student gets in the way, so every student in between must have a height different from AsA_s.

There are QQ queries. In the ii-th query, only students with numbers from lil_i to rir_i inclusive may take part in the jump rope. Return the maximum number of students who can take part in the jump rope. If no jump rope is possible, print 0.

입력

The first line contains NN, the number of students.

The second line contains the heights A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N.

The third line contains QQ, the number of queries.

Each of the next QQ lines contains two integers lil_i and rir_i.

출력

For each of the QQ queries, print the maximum number of students who can take part in the jump rope, one per line. If no jump rope is possible, print 0.

제한

  • 2≤N,Q≤5×1052 \le N, Q \le 5 \times 10^5
  • 1≤Ai≤1091 \le A_i \le 10^9 (1≤i≤N1 \le i \le N)
  • 1≤li<ri≤N1 \le l_i < r_i \le N (1≤i≤Q1 \le i \le Q)

Examples1

  1. Example 1

    Input
    6
    4 2 2 1 4 1
    3
    1 6
    2 4
    3 5
    
    Expected output
    5
    2
    0