Each of N paint drops covers a lattice of points with power-of-two steps; answer Q queries for the total colour sum at a given point.
Hard8MathBit manipulationHash mapImplementationNo attempts yetTime limit4sMemory limit1024 MBInspired by the great painter Picowso, Vera decided to make her own masterpiece. Her canvas is an infinite two-dimensional coordinate plane. Vera likes powers of two (1, 2, 4, 8, 16, ...), so she paints points repeatedly with step sizes that are powers of two.
Vera paints N times. The i-th time is described by three integers xi, yi, vi. Let ai be the largest power of two not greater than xi, and let bi be the largest power of two not greater than yi. Vera adds one paint drop of colour vi to every point of the form (xi+aip, yi+biq), where p and q are non-negative integers. A point may hold several paint drops, and several drops of the same colour.
Vera then asks Q questions. The j-th question asks for the colour at the point (rj,cj). The colour at a point is the sum of the colours of all paint drops on it. A point with no paint drop has colour 0.
You are her art assistant, so you have to answer the questions.
The first line contains two integers N and Q, separated by one space (1≤N,Q≤2⋅105).
Each of the next N lines contains three space-separated integers xi, yi, vi, describing the paint drops of colour vi (1≤i≤N; 1≤vi≤10000; 1≤xi,yi≤1018).
Each of the next Q lines contains two space-separated integers rj and cj, describing a question about the point (rj,cj) (1≤j≤Q; 1≤rj,cj≤1018).
Print Q lines. Line j contains one integer, the colour of the point (rj,cj).
In the first example, let colours 1, 2, 3, 4, 5 be red, blue, green, orange, purple. With p and q non-negative integers, the drops land as follows.
The painting from (0,0) to (11,11) looks like this.

The point (7,8) has a red, a blue and a purple drop, so its colour is 1+2+5=8. The point (5,9) has no drop, so its colour is 0.