Processing math: 100%
Pairs' value - MarisaOJ: Marisa Online Judge

Pairs' value

Time limit: 1000 ms
Memory limit: 256 MB
Given a permutation a consisting of integers from 1 to n. For a pair of indices i<j, with c=max(ai+1,ai+2,…,aj−1), the value of this pair is defined as: - x if c≤min(ai,aj) or i=j−1. - y if ai<c<aj or ai>c>aj. - 0 in all other cases. Given q queries l,r, calculate the total value of all pairs (a,b) such that l≤a<b≤r. ### Input - The first line contains four integers n,q,x,y. - The second line contains n integers representing the permutation a. - The next q lines each contain two integers l,r representing a query. ### Output - For each query, print the total value of all pairs that satisfy the condition. ### Constraints - 1≤n,q≤2×105. - 1≤x,y≤1000. ### Example Input: 10523935410768121715195913 Output: 272038186 ### Subtasks - Subtask 1 (30% of the points): 1≤n,q≤500. - Subtask 2 (30% of the points): x=2×y. - Subtask 3 (40% of the points): No additional constraints.