Under construction

Still being built. Things may change

Sequence and Queries 6

Time limit2sMemory limit512 MB

Summary
For each query range [i, j], report the highest number of occurrences of any single value inside that range.
Level

Hard9 of 10

Topics
Segment tree, Divide and conquer, Binary search, Prefix sum
Solved
No attempts yet

Problem

You are given a sequence A1,A2,…,ANA_1, A_2, \ldots, A_N of length NN. Write a program that processes the following query on this sequence.

  • i j: print how many times the most frequent value among Ai,Ai+1,…,AjA_i, A_{i+1}, \ldots, A_j occurs.

Input

The first line contains the length of the sequence NN (1≤N≤100 0001 \le N \le 100\,000).

The second line contains A1,A2,…,ANA_1, A_2, \ldots, A_N separated by spaces. (1≤Ai≤100 0001 \le A_i \le 100\,000)

The third line contains the number of queries MM (1≤M≤100 0001 \le M \le 100\,000).

Each of the next MM lines contains one query ii, jj. (1≤i≤j≤N1 \le i \le j \le N)

Output

Print the answer to each query on its own line, in the order the queries are given.

Examples2

  1. Example 1

    Input
    5
    1 2 1 3 3
    3
    1 3
    2 3
    1 5
    
    Expected output
    2
    1
    2
    
  2. Example 2

    Input
    1
    100000
    1
    1 1
    
    Expected output
    1