#Lutece2540. 区间逆序对

区间逆序对

Migrated from Lutece 2540 区间逆序对

All parts of this problem, including description, images, samples, data and checker, might be broken. If you find bugs in this problem, please contact the admins.

Description

数列 aa 的一组逆序对是指一对位置 (i,j)(i,j) 满足 i<ji<jai>aja_i>a_j。例如 a={3,1,2}a=\{3,1,2\} 的逆序对数量为 22,满足条件的 (i,j)(i,j) 分别是 (1,2),(1,3)(1,2),(1,3)

一个数列 aa 的子区间 [l,r][l,r] 是指数列 {al,al+1,,ar1,ar}\{a_l,a_{l+1},\ldots ,a_{r-1},a_{r}\}

现在给定一个数列 aa,请求出他的多个子区间的逆序对数量。

Input

第一行两个正整数 n,mn,m,分别表示数列长度和子区间个数。

第二行 nn 个正整数,第 ii 个数表示 aia_i

接下来 mm 行,每行两个正整数 l,rl,r 表示一个子区间 [l,r][l,r]

Output

mm 行,每行一个正整数,表示子区间的逆序对数量。

Samples

3 2
3 1 2
1 2
1 3
1
2

Constraints

$1 \leq n,m \leq 2\times 10^5,1\le a_i\le n,1\le l\le r\le n$

Note

请注意 aa 并不保证是一个排列。

Resources

2021 UESTC ICPC Training for Data Structures