CCO '17 P3 - Vera and Modern Art

View as PDF

Submit solution

Points: 30 (partial)
Time limit: 4.0s
Memory limit: 1G

Author:
Problem types
Canadian Computing Olympiad: 2017 Day 1, Problem 3

After being inspired by the great painter Picowso, Vera decided to make her own masterpiece. She has an empty painting surface which can be modeled as an infinite 2D coordinate plane. Vera likes powers of two (1, 2, 4, 8, 16, \dots) and will paint some points in a repeated manner using step sizes which are a power of two.

Vera will paint N times. The i-th time can be described by three integers x_i, y_i, v_i. Let a_i be the largest power of two not greater than x_i and let b_i be the largest power of two not greater than y_i.

Vera will add a paint drop with colour v_i to all points that are of the form (x_i+a_ip, y_i+b_iq), where p, q are non-negative integers. A point may have multiple paint drops on it or have multiple drops of the same colour.

Then Vera will ask Q questions. For the j-th question she wants to know the colour at the point (r_j, c_j). The colour at a point is equal to the sum of the colours of all paint drops at that point. If there are no paint drops at a point, the colour of that point is 0.

Since you are forced to be her art assistant, you will have to answer Vera's questions.

Input Specification

The first line contains two integers N, Q, separated by one space (1 \le N, Q \le 2 \cdot 10^5).
The next N lines each contain three space-separated integers, x_i, y_i, v_i representing the paint drops of colour v_i (1 \le i \le N; 1 \le v_i \le 10\,000; 1 \le x_i, y_i \le 10^{18}).
The next Q lines each contain two space-separated integers r_j, c_j, representing the Q questions about the point (r_j, c_j) (1 \le j \le Q; 1 \le r_j \le 10^{18}; 1 \le c_j \le 10^{18}).
For 5 of the 25 available marks, N, Q \le 2\,000.
For an additional 5 of the 25 available marks, y_i = 1 (1 \le i \le N).
For an additional 5 of the 25 available marks, N, Q \le 10^5 and 1 \le r_j, c_j \le 10^9 (1 \le j \le Q).

Output Specification

The output will be Q lines. The j-th line (1 \le j \le Q) should have one integer, which is the colour of point (r_j, c_j).

Sample Input

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

Sample Output

1
8
0
6
4
3

Explanation for Sample Output

Let colour 1, 2, 3, 4, 5 be red, blue, green, orange, purple respectively.
Let p, q be non-negative integers, then:

  • Points (1+p, 2+2q) have a red paint drop.
  • Points (3+2p, 4+4q) have a blue paint drop.
  • Points (4+4p, 5+4q) have a green paint drop.
  • Points (6+4p, 3+2q) have an orange paint drop.
  • Points (7+4p, 1+q) have a purple paint drop.

The painting from (0, 0) to (11, 11) is shown below:

We can see that:

  • (2, 6) has a red paint drop, so it has colour 1.
  • (7, 8) has a red, blue and purple paint drop, so it has colour 1+2+5 = 8.
  • (5, 9) has no paint drops, so it has colour 0.
  • (11, 2) has a red and purple paint drop, so it has colour 1+5 = 6.
  • (10, 7) has an orange paint drop, so it has colour 4.
  • (4, 5) has a green paint drop, so it has colour 3.

Comments


  • 2
    X_Ray  commented on May 26, 2022, 6:31 p.m.

    For C++ users, GP Hash Table can experience a lot of collisions in this problem and become very slow. Refer to the KACTL GP Hash Table or this article if you're TLEing because of collisions (or switch to unordered map, which experiences [less?] collisions here).