atcoder#ABC242G. [ABC242G] Range Pairing Query

[ABC242G] Range Pairing Query

Score : 600600 points

Problem Statement

NN people numbered 1,2,,N1,2,\dots,N are standing in a row. Person ii wears Color AiA_i.

Answer QQ queries of the format below.

  • You are given integers ll and rr. Considering only Person l,l+1,,rl,l+1,\dots,r, how many pairs of people wearing the same color can be formed at most?

Constraints

  • All values in input are integers.
  • 1N1051 \le N \le 10^5
  • 1Q1061 \le Q \le 10^6
  • 1AiN1 \le A_i \le N
  • 1lrN1 \le l \le r \le N in each query.

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \dots ANA_N

QQ

Query1\mathrm{Query}_1

Query2\mathrm{Query}_2

\vdots

QueryQ\mathrm{Query}_Q

Here, Queryi\mathrm{Query}_i represents the ii-th query.

Each query is in the following format:

ll rr

Output

Print QQ lines. The ii-th line should contain the answer for the ii-th query as an integer. The use of fast input and output methods is recommended because of potentially large input and output.

10
1 2 3 2 3 1 3 1 2 3
6
6 10
5 8
3 6
4 4
1 6
1 10
2
2
1
0
3
4

We have A=(1,2,3,2,3,1,3,1,2,3)A=(1,2,3,2,3,1,3,1,2,3). This input contains six queries.

The first query is (l,r)=(6,10)(l, r) = (6, 10). By pairing Person 6,86, 8 and paring Person 7,107, 10, we can form two pairs of people wearing the same color.

The second query is (l,r)=(5,8)(l, r) = (5, 8). By pairing Person 5,75, 7 and paring Person 6,86, 8, we can form two pairs of people wearing the same color.

There can be a query where l=rl=r.