DMOPC '18 Contest 4 P4 - Dr. Henri and Lab Data

View as PDF

Submit solution


Points: 12 (partial)
Time limit: 1.4s
Java 2.5s
Memory limit: 256M

Author:
Problem type

Dr. Henri is working on a new method of analyzing lab data! He has collected N data points: A1,A2,,AN, and has defined the k-interest of a subarray as the sum of all numbers greater than or equal to k minus the sum of all numbers less than k.

For example, the 5-interest of the array [4,2,6,5,1] is (6+5)(4+2+1)=4.

Dr. Henri knows that some of the data might be outliers, so he asks you Q queries of the form l r k, asking you to compute the k-interest of the subarray Al,Al+1,,Ar.

Can you write a program to help Dr. Henri?

Constraints

Subtask 1 [10%]

1N,Q4000
1Ai109

Subtask 2 [60%]

1N,Q200000
1Ai200000

Subtask 3 [30%]

1N,Q200000
1Ai109

Input Specification

The first line of input will contain two space-separated integers, N and Q.
The second line of input will contain N space-separated integers, A1,A2,,AN.
The next Q lines will each contain three space-separated integers, li, ri, and ki.
It is guaranteed that 1liriN and 1ki109.

Output Specification

Q lines, where the ith line is the answer to the ith query.

Sample Input 1

Copy
3 6
5 10 15
1 2 1
1 3 16
2 2 10
2 2 11
1 3 6
1 1 9

Sample Output 1

Copy
15
-30
10
-10
20
-5

Sample Input 2

Copy
10 10
1 2 3 4 5 6 7 8 9 10
2 7 4
4 10 1
9 9 10
1 5 2
1 5 8
3 6 5
4 8 999
2 3 1
6 8 1
5 7 5

Sample Output 2

Copy
17
49
-9
13
-15
4
-30
5
21
18

Comments