#P7125. [Ynoi2008] rsmemq

[Ynoi2008] rsmemq

Problem Description

Given a sequence aa of length nn, define xx to be the mode of the interval [l,r][l, r] if and only if there does not exist yy such that the number of occurrences of yy in the interval [l,r][l, r] is greater than the number of occurrences of xx in the interval [l,r][l, r].

There are mm queries. Each query gives l,rl, r. For each query, find how many ordered pairs (l,r)(l', r') satisfy llrrl \le l' \le r' \le r, and the length of interval [l,r][l', r'] is odd, and (l+r)/2(l' + r') / 2 (note that this is an index, not the value at that index) is the mode of the interval [l,r][l', r'].

Input Format

The first line contains two integers nn and mm.

The next line contains nn integers describing the sequence.

The following mm lines each contain two integers ll and rr, representing one query.

Output Format

Output mm lines in total, each being the answer for the corresponding query.

10 10
2 2 2 1 2 7 7 9 6 10
1 4
4 4
1 3
2 6
6 6
7 10
2 6
4 10
3 5
3 7

2
0
2
1
0
3
1
6
0
1

Hint

Idea: yummy&nzhtl1477, Solution: nzhtl1477, Code: nzhtl1477&czr, Data: nzhtl1477(partially uploaded)

For 100%100\% of the testdata: 1n,m5×1051 \le n, m \le 5 \times 10^5, 1lrn1 \le l \le r \le n, 1ain1 \le a_i \le n. All values are integers.

Sample explanation:

In [1,4][1,4], the subintervals that satisfy the conditions are [1,3][1,3], [2,2][2,2].

In [1,3][1,3], the subintervals that satisfy the conditions are [1,3][1,3], [2,2][2,2].

In [2,6][2,6], the subintervals that satisfy the conditions are [2,2][2,2].

In [7,10][7,10], the subintervals that satisfy the conditions are [7,7][7,7], [8,10][8,10], [10,10][10,10].

In [4,10][4,10], the subintervals that satisfy the conditions are [7,7][7,7], [6,8][6,8], [5,9][5,9], [4,10][4,10], [8,10][8,10], [10,10][10,10].

In [3,7][3,7], the subintervals that satisfy the conditions are [7,7][7,7].

Translated by ChatGPT 5