#P5677. [GZOI2017] 配对统计

    ID: 4647 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2017各省省选树状数组O2优化排序贵州

[GZOI2017] 配对统计

Description

Given nn numbers a1,,ana_1,\cdots,a_n.

For a pair (x,y)(x,y), if for all i=1,2,,ni=1,2,\cdots,n, it satisfies axayaxai(ix)|a_x-a_y|\le|a_x-a_i|(i\not=x), then (x,y)(x,y) is called a good pair (x|x| denotes the absolute value of xx).

You are given several queries. For each query, ask how many good pairs are contained in the interval [l,r][l,r].

That is, choose x,yx,y (lx,yrl\le x,y\le r and xyx\not=y), and ask how many pairs (x,y)(x,y) are good pairs.

Input Format

The first line contains two positive integers n,mn,m.

The second line contains nn numbers a1,,ana_1,\cdots,a_n.

In the next mm lines, each line gives two numbers l,rl,r.

Output Format

Let AnsiAns_i be the answer to the ii-th query. Output i=1mAnsi×i\sum_{i=1}^m\limits Ans_i\times i.

3 2
2 1 3
1 2
1 3
10

Hint

Sample Explanation

In the first query, the good pairs are: (1,2)(2,1)(1,2)(2,1).

In the second query, the good pairs are: (1,2)(2,1),(1,3)(3,1)(1,2)(2,1),(1,3)(3,1).

The answer =2×1+4×2=10=2\times 1+4\times 2=10.

Constraints

Translated by ChatGPT 5