GCC '16 P3 - Hang Gliding Fun

View as PDF

Submit solution


Points: 25 (partial)
Time limit: 1.0s
Java 8 1.4s
Memory limit: 256M

Authors:
Problem types

Bob lives in a city with N skyscrapers arranged in a row running north to south. He likes to hang glide over the city. Bob can glide south to a shorter building if all of the passed buildings' heights are smaller than his current building's height.

Each of the N buildings has height hi, view value vi, and danger value di for 1iN. Bob has Q questions for you: Given that he starts at the Bth northmost building (1BN), what is the maximum possible sum of the view values of buildings which he lands on such that none of those buildings has danger value more than M (1M106)?

Input Specification

The first line contains two space-separated numbers N (1N100000) and Q (1Q100000), the number of buildings and queries respectively.
The next N lines each contain hi (1hi106), vi (1vi106), and di (1di106), the height, view value, and danger value for the ith northmost building.
The next Q lines each contain the query. Each line contains two space-separated numbers B (1BN) and M (1M106). It is guaranteed that the danger value of the Bth northmost building is not more than M.

Output Specification

For each query, output the answer on a new line.

Sample Input 1

Copy
3 2
3 1 2
3 4 4
1 2 10
2 20
2 4

Sample Output 1

Copy
6
4

Explanation

For the first query, the path which maximizes the sum of view values is hang gliding to the third building, so the answer is 4+2. For the second query, the path which maximizes the sum of view values is simply staying put at the second building, so the answer is 4.


Comments

There are no comments at the moment.