#P3722. [AHOI2017/HNOI2017] 影魔
[AHOI2017/HNOI2017] 影魔
Description
Nevermore has souls. Inside the vast body of the Shadow Fiend, they can line up in a row, labeled from left to right to . The -th soul has combat power , and souls provide attack power for the Shadow Fiend in the form of pairs. For a soul pair with , if there is no with that is greater than or greater than , then it provides attack power. In another case, let be the maximum of . If satisfies , or , then it provides attack power. When such a does not exist, it naturally does not provide this attack power. In all other cases, the pair does not provide any attack power.
Shadow Fiend’s close friend Lifestealer visited him one day and was captivated by these souls. He wants to know, for any interval , how much attack power is provided by the soul pairs within these intervals, that is, consider the sum of the attack power provided by all soul pairs satisfying .
Incidentally, the souls’ combat powers form a permutation of to : .
Input Format
The first line contains four integers .
The second line contains integers .
Then there are lines. Each line contains two integers , representing a query asking how much attack power is provided by the soul pairs within the interval .
Output Format
Output lines, each containing one answer, corresponding to the queries in order.
10 5 2 3
7 9 5 1 3 10 6 8 2 4
1 7
1 9
1 3
5 9
1 5
30
39
4
13
16
Hint
For of the testdata, .
For another of the testdata, .
For of the testdata, , .
Translated by ChatGPT 5
京公网安备 11011102002149号