#P6344. [CCO 2017] Vera 与现代艺术

[CCO 2017] Vera 与现代艺术

Description

Inspired by the great painter Picasso, Vera decided to create her masterpiece. She has a canvas that can be simplified as an infinitely large 2D plane. Vera likes integer powers of 22 (1,2,4,8,16,)(1,2,4,8,16,\ldots), and she will color some points using step sizes that are powers of 22.

Vera will color nn times. The ii-th coloring operation is described by three integers xi,yi,vix_i, y_i, v_i. Let aia_i be the largest power of 22 that is not greater than xix_i, and let bib_i be the largest power of 22 that is not greater than yiy_i. Vera will use color viv_i to color all points with coordinates (xi+aip,yi+biq)(x_i + a_i p, y_i + b_i q), where p,qp, q are non-negative integers. A point may be colored multiple times with different colors, and it may also be colored multiple times with the same color. The color value of a point is the sum of all colors that have been applied to that point.

Then Vera will ask QQ questions. For the jj-th question, she wants to know the color value of the point at coordinates (rj,cj)(r_j, c_j). If a point has never been colored, then its color value is 00.

Because you are forced to be Vera's assistant, you have to answer Vera's questions.

Input Format

The first line contains two integers n,Qn, Q, separated by a space.

The next nn lines each contain three integers xi,yi,vix_i, y_i, v_i separated by spaces, meaning that the coloring operation described in the statement is performed using color viv_i, with parameters xix_i and yiy_i.

The next QQ lines each contain two integers rj,cjr_j, c_j separated by spaces, asking for the color value of the point (rj,cj)(r_j, c_j).

Output Format

Output QQ lines. Each line contains one integer. The integer on the jj-th line is the color value of the point (rj,cj)(r_j, c_j).

5 6
1 2 1
3 4 2
4 5 3
6 3 4
7 1 5
2 6
7 8
5 9
11 2
10 7
4 5
1
8
0
6
4
3

Hint

Sample Explanation

Let colors 1,2,3,4,51,2,3,4,5 be red, blue, green, orange, and purple, respectively.

Let p,qp, q be non-negative integers. Then:

  • Points with coordinates (1+p,2+2q)(1 + p, 2 + 2q) are colored red.
  • Points with coordinates (3+2p,4+4q)(3 + 2p, 4 + 4q) are colored blue.
  • Points with coordinates (4+4p,5+4q)(4 + 4p, 5 + 4q) are colored green.
  • Points with coordinates (6+4p,3+2q)(6 + 4p, 3 + 2q) are colored orange.
  • Points with coordinates (7+4p,1+q)(7 + 4p, 1 + q) are colored purple.

The coordinate grid from (0,0)(0,0) to (11,11)(11,11) is shown in the figure:

We can observe that:

  • (2,6)(2,6) is colored red, so its color value is 11.
  • (7,8)(7,8) is colored red, blue, and purple, so its color value is 1+2+5=81+2+5=8.
  • (5,9)(5,9) is not colored, so its color value is 00.
  • (11,2)(11,2) is colored red and purple, so its color value is 1+5=61+5=6.
  • (10,7)(10,7) is colored orange, so its color value is 44.
  • (4,5)(4,5) is colored green, so its color value is 33.

Constraints and Notes

  • For 20%20\% of the testdata, 1n,Q2×1031 \le n, Q \le 2 \times 10^3.
  • For another 20%20\% of the testdata, yi=1 (1in)y_i = 1 \ (1 \le i \le n).
  • For another 20%20\% of the testdata, 1n,Q1051 \le n, Q \le 10^5 and 1rj,cj109 (1jQ)1 \le r_j, c_j \le 10^9 \ (1 \le j \le Q).
  • For 100%100\% of the testdata, 1n,Q2×1051 \le n, Q \le 2 \times 10^5, 1in1 \le i \le n, 1vi1041 \le v_i \le 10^4, 1xi,yi,rj,cj10181 \le x_i, y_i, r_j, c_j \le 10^{18}, and 1jQ1 \le j \le Q.

Due to issues with the judging machine, only the last 1010 test cases are kept.

Source: CCO 2017 Day 1 “ Vera and Modern Art ”.

Note: This translation is from LOJ.

Translated by ChatGPT 5