HHPC1 P3 - Yonder Ridge

View as PDF

Submit solution

Points: 12 (partial)
Time limit: 2.0s
Memory limit: 1G

Author:
Problem types

Imagine a picturesque landscape made up of various ridges stretching across an xy-plane. The x-axis of the plane extends from 0 to M, while the y-axis extends from to . There are N ridges numbered from 1 to N. The i-th ridge can be represented as a line segment connecting (0,ai) and (M,bi), with an aesthetic value of vi.

For an interval [l,r], the goodness of the landscape from ridge k refers the sum of the aesthetic values of other ridges that are ever strictly above the height of ridge k within [l,r].

A person is planning to explore the landscape, and has a few questions. In the i-th question, they wonder what the goodness of landscape pi is over the interval [si,si+K]. Note that K is the same for each query.

Constraints

For all subtasks:

1N5×103

1KM109

1Q5×105

109ai,bi109

1vi109

1piN

0siMK

Subtask 1 [30%]

1Q5×103

Subtask 2 [70%]

No additional constraints.

Input Specification

The first line contains four space-separated integers N, M, Q, and K.

The i-th of the following N lines each contain three space-separated integers ai, bi, and vi.

The i-th of the following Q lines each contain two space-separated integers pi and si.

Output Specification

For each query, output an integer, representing the goodness of the landscape.

Sample Input

Copy
3 15 2 7
6 9 3
2 11 2
10 1 1
1 3
1 4

Sample Output

Copy
1
3

Sample Explanation

For the first query, only ridge 3 is ever strictly above ridge 1 in the interval [3,10], so the answer is 1.

For the second query, both ridge 2 and ridge 3 are ever strictly above ridge 1 at some point in the interval [4,11], so the answer is 1+2=3.


Comments

There are no comments at the moment.