#P3722. [AHOI2017/HNOI2017] 影魔

    ID: 2696 远端评测题 2000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2017线段树各省省选湖南排序

[AHOI2017/HNOI2017] 影魔

Description

Nevermore has nn souls. Inside the vast body of the Shadow Fiend, they can line up in a row, labeled from left to right 11 to nn. The ii-th soul has combat power kik_i, and souls provide attack power for the Shadow Fiend in the form of pairs. For a soul pair i,ji, j with i<ji < j, if there is no ksk_s with i<s<ji < s < j that is greater than kik_i or greater than kjk_j, then it provides p1p_1 attack power. In another case, let cc be the maximum of ki+1,ki+2,,kj1k_{i + 1}, k_{i + 2}, \cdots, k_{j - 1}. If cc satisfies ki<c<kjk_i < c < k_j, or kj<c<kik_j < c < k_i, then it provides p2p_2 attack power. When such a cc does not exist, it naturally does not provide this p2p_2 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 [a,b][a, b], 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 i,ji, j satisfying ai<jba \le i < j \le b.

Incidentally, the souls’ combat powers form a permutation of 11 to nn: k1,k2,,knk_1, k_2, \cdots, k_n.

Input Format

The first line contains four integers n,m,p1,p2n, m, p_1, p_2.

The second line contains nn integers k1,k2,,knk_1, k_2, \cdots, k_n.

Then there are mm lines. Each line contains two integers a,ba, b, representing a query asking how much attack power is provided by the soul pairs within the interval [a,b][a, b].

Output Format

Output mm lines, each containing one answer, corresponding to the mm 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 30%30\% of the testdata, 1n,m5001 \le n, m \le 500.

For another 30%30\% of the testdata, p1=2p2p_1 = 2 p_2.

For 100%100\% of the testdata, 1n,m2000001 \le n, m \le 200000, 1p1,p210001 \le p_1, p_2 \le 1000.

Translated by ChatGPT 5