Processing math: 100%
Subarray distance - MarisaOJ: Marisa Online Judge

Subarray distance

Time limit: 4000 ms
Memory limit: 512 MB
Given an array A of n integers and q queries, each of the form a,b,c,d, calculate: b∑i=ad∑j=c|Ai−Aj| ### Input - The first line contains two integers n,q. - The second line contains n integers Ai. - The next q lines, each line contains four integers a,b,c,d, a query. ### Output - Print the answer for each query. ### Constraints - 1≤n,q≤105. - 1≤Ai≤109. - 1≤a≤b≤n. - 1≤c≤d≤n. ### Example Input: 551234511221123112412231515 Output: 136440