Prefix minimum - MarisaOJ: Marisa Online Judge

Prefix minimum

Time limit: 1000 ms
Memory limit: 256 MB
Given an array $A$ of $n$ integers. There are $q$ queries, each consisting of two integers $l, r$. Calculate $A_l + \text{min}(A_l, A_{l+1}) + ... + \text{min}(A_l, A_{l+1}, ..., A_r)$. ### Input - The first line contains two positive integers $n, q$. - The second line contains $n$ integers $A_i$. - The next $q$ lines each contain two integers $l, r$. ### Output - For each query, print the answer on a separate line. ### Constraints - $1 \le n, q \le 10^5$. - $1 \le A_i \le 10^9$. - $1 \le l \le r \le n$. ### Example Input: ``` 6 3 5 3 7 2 1 4 2 4 3 6 1 3 ``` Output: ``` 8 11 11 ```