Under construction

Still being built. Things may change

Mineral Extraction

Time limit3sMemory limit512 MB

Summary
Find the largest number of leading batches, and then the most robots from the next batch, that can be placed on the grid with at most q robots per cell.
Level

Hard8 of 10

Topics
Graph, Binary search
Solved
No attempts yet

Statement

A project is underway to develop a planet in a neighboring star system. Several batches of robots will be sent to the planet to mine minerals.

The mining area is a grid rectangle of size ww by hh, with cells from (1,1)(1, 1) to (w,h)(w, h). Some cells contain specialist bases, and robot batches are delivered to these bases. There are ss bases in total, and the ii-th base is in the cell (xi,yi)(x_i, y_i).

The jj-th batch is delivered to base bjb_j and contains njn_j robots. Each robot in the batch has mobility mjm_j. Once a batch arrives at its base, each robot moves across the surface from the base to some cell. A robot with mobility mm may move at most mm times, each time to one of the eight neighboring cells (see Fig. 1).

Fig. 1

After the robots from all delivered batches are placed, they are activated and start mining. During movement, any number of robots may share a cell. However, after activation, no cell may hold more than qq robots.

Input

The management has information about tt batches that can be sent to the planet in sequence. After all batches are delivered, limited mobility may make it impossible to place the robots so that no cell holds more than qq of them. Therefore management chooses the first kk batches (0≤k≤t0 \le k \le t) to be fully delivered to their bases. If k<tk < t, management also accepts zz of the nk+1n_{k+1} robots from the next batch, the (k+1)(k+1)-th one (0≤z<nk+10 \le z < n_{k+1}).

Management maximizes kk first and then maximizes zz.

Output

The first line contains ww, hh, ss, and qq, where 1≤w,h≤1051 \le w, h \le 10^5, 1≤s≤41 \le s \le 4, and 1≤q≤1001 \le q \le 100. The next ss lines each contain the coordinates xix_i and yiy_i of a base, where 1≤xi≤w1 \le x_i \le w and 1≤yi≤h1 \le y_i \le h. The next line contains the number of batches tt, where 1≤t≤1001 \le t \le 100. The next tt lines each contain bjb_j, njn_j, and mjm_j, where 1≤bj≤s1 \le b_j \le s, 1≤nj≤w⋅h⋅q1 \le n_j \le w \cdot h \cdot q, and 0≤mj<max⁡(w,h)0 \le m_j < \max(w, h).

Hint

In the sample, the whole first batch is accepted, and 7 robots from the second batch are accepted as well. Fig. 2 shows a placement where every cell holds at most one robot. Bases are drawn as circles. Cells with robots from base 1 are hatched vertically, and cells with robots from base 2 are gray.

Fig. 2

Examples1

  1. Example 1

    Input
    4 3 2 1
    1 1
    3 2
    3
    1 4 1
    2 9 1
    1 12 2
    
    Expected output
    1 7