Item Collection
InterviewTime limit3sMemory limit1024 MB
Given N weighted boxes on a grid and Q axis-parallel moves from (1, 1), find the total items collected along the path.
- Level
Medium5 of 10
- Topics
- Binary search, Prefix sum, Sorting
- Solved
- No attempts yet
Problem
You are building a game where a car moves on a two-dimensional map and collects items.
The map has boxes from which items can be collected. The position of the -th box is , and each time the car passes this position, it gets items.
The car moves in a direction parallel to the -axis or the -axis. A move is given by two integers and . If , the car moves in the direction of increasing . If , it moves in the direction of increasing . If , it moves in the direction of decreasing . If , it moves in the direction of decreasing .
Items in the box at the starting position of a move cannot be collected. That is, when the car moves from to , the box at gives no items, but the box at does.
The car starts at and moves a total of times. Given the direction and distance of each move, find the total number of items collected over the moves.
Input
The first line contains two integers and , separated by a space, the number of boxes and the number of moves.
The next lines each contain three integers , , and , separated by spaces. This means the -th box is at , and passing this position gives items.
The next lines each contain two integers and , separated by a space. This means the car moves in direction .
Output
On the first line, print the total number of items collected over the moves.
Constraints
- The positions of the boxes are all different.
- At every moment, the and coordinates of the car are between and , inclusive.
- All given numbers are integers.