Under construction

Still being built. Things may change

Item Collection

Interview

Time limit3sMemory limit1024 MB

Summary
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 NN boxes from which items can be collected. The position of the ii-th box is (xi,yi)(x_i, y_i), and each time the car passes this position, it gets wiw_i items.

The car moves in a direction parallel to the xx-axis or the yy-axis. A move is given by two integers dd and vv. If d=0d = 0, the car moves vv in the direction of increasing xx. If d=1d = 1, it moves vv in the direction of increasing yy. If d=2d = 2, it moves vv in the direction of decreasing xx. If d=3d = 3, it moves vv in the direction of decreasing yy.

Items in the box at the starting position of a move cannot be collected. That is, when the car moves from (sx,sy)(s_x, s_y) to (ex,ey)(e_x, e_y), the box at (sx,sy)(s_x, s_y) gives no items, but the box at (ex,ey)(e_x, e_y) does.

The car starts at (1,1)(1, 1) and moves a total of QQ times. Given the direction and distance of each move, find the total number of items collected over the QQ moves.

Input

The first line contains two integers NN and QQ, separated by a space, the number of boxes and the number of moves.

The next NN lines each contain three integers xix_i, yiy_i, and wiw_i, separated by spaces. This means the ii-th box is at (xi,yi)(x_i, y_i), and passing this position gives wiw_i items.

The next QQ lines each contain two integers djd_j and vjv_j, separated by a space. This means the car moves vjv_j in direction djd_j.

Output

On the first line, print the total number of items collected over the QQ moves.

Constraints

  • 1≤N≤200,0001 \le N \le 200{,}000
  • 1≤Q≤200,0001 \le Q \le 200{,}000
  • 1≤xi≤200,0001 \le x_i \le 200{,}000
  • 1≤yi≤200,0001 \le y_i \le 200{,}000
  • 1≤wi≤200,0001 \le w_i \le 200{,}000
  • 0≤dj≤30 \le d_j \le 3
  • 1≤vj≤200,0001 \le v_j \le 200{,}000
  • The positions of the boxes are all different.
  • At every moment, the xx and yy coordinates of the car are between 11 and 200,000200{,}000, inclusive.
  • All given numbers are integers.

Examples2

  1. Example 1

    Input
    4 6
    5 5 3
    5 8 5
    3 5 2
    1 5 1
    0 4
    1 9
    3 5
    2 3
    2 1
    0 5
    
    Expected output
    24
    
  2. Example 2

    Input
    3 3
    1 3 1
    2 2 1
    3 1 1
    1 3
    0 2
    3 3
    
    Expected output
    2