#P17398. [ICPC 2018 Shenyang R] Best ACMer Solves the Hardest Problem

[ICPC 2018 Shenyang R] Best ACMer Solves the Hardest Problem

Problem Description

One day an excellent ACMer will leave the field to face new challenges, just like what the predecessors are doing. Some of them have taken over their family businesses, and some of them are struggling with the edges of unemployment. Some of them have the courage to display themselves and become a professional Ingress player, and some of them are still pushing themselves to limits and try to solve all problems in Project Euler.

But all these destinations are so shallow for Benecol de Cecco, the former king. What he does now is to be the best respondents in StackOverflow. StackOverflow is the largest, most trusted online community for developers to learn, share their programming knowledge, and build their careers.

Today, he notices a question which is posted by Kevin Li, saying: Recently, I implemented an experiment where I need to find all the data records whose Euclidean distances to the query point qq are the same value rr. I tried to use the k-d tree to improve the search efficiency. However, I found that the k-d tree needs to traverse all leaf nodes to return the result, that is, it still needs to compare all dataset to obtain the result.

This question can be formalized to build a database with real-time queries and modifications. In the beginning, suppose we have nn different points in the plane. The ii-th point is located at (xi,yi)(x_i, y_i) and has a weight wiw_i. Then we consider several queries and modifications dynamically given by

  • 1 x y w\text{1 x y w}, to insert a new point at (x,y)(x, y) of weight ww, where we guarantee that before the operation no point was located in the place;
  • 2 x y\text{2 x y}, to delete the point located at (x,y)(x, y), where we guarantee that the point existed before the operation;
  • 3 x y k w\text{3 x y k w}, for each point whose Euclidean distance to (x,y)(x, y) is k\sqrt{k}, to increase its weight by ww;
  • 4 x y k\text{4 x y k}, to query the sum of weights for all points whose Euclidean distances to (x,y)(x, y) are k\sqrt{k}.

Benecol de Cecco says this question is pretty easy and he asked me to share the problem with you all. By the way, the Euclidean distance between two points (x0,y0)(x_0, y_0) and (x1,y1)(x_1, y_1) is equal to (x0−x1)2+(y0−y1)2\sqrt{(x_0 - x_1)^2 + (y_0 - y_1)^2}.

Input Format

The input contains several test cases, and the first line contains a positive integer TT indicating the number of test cases which is up to 10001000.

For each test case, the first line contains two integers nn and mm indicating the initial number of points in the plane and the number of operations respectively, where 1≤n,m≤1051 \le n, m \le 10^5.

Each of the following nn lines contains three integers x,yx, y and ww satisfying 1≤x,y,w≤60001 \le x, y, w \le 6000, which describe a point at (x,y)(x, y) of weight ww in the beginning.

Each of the following mm lines contains an operation which can be a query or a modification. These operations are given in the forms described above. To make all xx and yy in operations dynamic, we use lastanslastans to denote the answer to the most recent query and its initial value is zero. For each operation with the values xx and yy in input, their real values should be (((x+lastans) mod 6000)+1)(((x + lastans) \bmod 6000) + 1) and (((y+lastans) mod 6000)+1)(((y + lastans) \bmod 6000) + 1) respectively. All coefficients in operations are integers and satisfy 0≤k≤1070 \le k \leq 10^7 and 1≤x,y,w≤60001 \le x, y, w \le 6000.

We guarantee that the sum of nn and the sum of mm in all test cases are no larger than 10610^6 individually.

Output Format

For each test case, output a line containing "Case #x:" (without quotes) at first, where x\text{x} is the test case number starting from 11.

Then for each query, output an integer in a line indicating the answer.

1
3 6
2999 3000 1
3001 3000 1
3000 2999 1
1 2999 3000 1
4 2999 2999 1
2 2995 2996
3 2995 2995 1 1
4 2995 2995 1
4 3000 3000 1
Case #1:
4
6
0

Hint

In the sample case, if we ignore the special input format for dynamic xx and yy in operations, here we can show these modifications and queries directly in an offline format as follows:

  • 1 3000 3001 1\text{1 3000 3001 1};
  • 4 3000 3000 1\text{4 3000 3000 1};
  • 2 3000 3001\text{2 3000 3001};
  • 3 3000 3000 1 1\text{3 3000 3000 1 1};
  • 4 3000 3000 1\text{4 3000 3000 1};
  • 4 3007 3007 1\text{4 3007 3007 1}.