#P7723. Interactive Grid Operation Simulation
Interactive Grid Operation Simulation
Interactive Grid Operation Simulation
You are given an interactive simulation on a 2D plane. Initially there are (n) points with coordinates ((i, p_i)) and corresponding weights (d_i) for (0 \le i < n). There are (m) operations (numbered from 0 to (m-1)) executed in increasing order. In the (w)-th operation, you are given four numbers (x_1,x_2,y_1,y_2) satisfying (0 < x_1 < x_2 < n) and (0 < y_1 < y_2 < n). For each point with current coordinates ((x,y)), its grid coordinate is defined to be [ (X, Y) = \Big(\mathbb{1}{{x \ge x_1}}+\mathbb{1}{{x \ge x_2}},; \mathbb{1}{{y \ge y_1}}+\mathbb{1}{{y \ge y_2}}\Big), ] where (\mathbb{1}_{\cdot}) is the indicator function. The operation consists of three steps:
- For each grid cell ((X,Y)) (where (X,Y\in{0,1,2})), query the sum of the weights of all points whose grid coordinate is ((X,Y)) and record it in (ans[X][Y]).
- For each grid cell ((X,Y)), update the weights of the points in that cell by performing an operation: if a point has weight (a) then its weight becomes (o[X][Y] \cdot a). (Here (\cdot) is an abstract operation defined on type (D) with unit (\epsilon_D) and (o[X][Y]) is of abstract type (O) with unit (\epsilon_O). They satisfy the following properties: for all (a,b\in D), (a+b=b+a) and (a+\epsilon_D=a); for all (u,v\in O), (u\cdot v) is associative and (u\cdot \epsilon_O=\epsilon_O\cdot u=u); and for every (u,v\in O) and (a,b\in D), (u\cdot (a+b)=(u\cdot a)+(u\cdot b)) with (\epsilon_O\cdot a=a). In this problem, you may assume (\epsilon_D = 0) and (\epsilon_O = 1), and that (+) and (\cdot) behave like normal integer addition and multiplication.
- All points update their coordinates simultaneously. For a point in grid cell ((X,Y)), its coordinate ((x,y)) becomes [ (x+dx[X],;y+dy[Y]), ] where [ \begin{aligned} dx[0]&=0,\ dx[1]&=n-x_2,\ dx[2]&=x_1-x_2,\ dy[0]&=0,\ dy[1]&=n-y_2,\ dy[2]&=y_1-y_2. \end{aligned} ]
You must implement the function
void solve(
const int n,
const int m,
const int p[],
const Data d[],
const int x1[],
const int x2[],
const int y1[],
const int y2[],
const Operation o[][3][3],
Data ans[][3][3]);
using the provided APIs for type manipulation. In your implementation, you can simulate the abstract types (D) and (O) as integers with (\epsilon_D = 0), (\epsilon_O = 1), addition as (+) and multiplication as ordinary multiplication. The answer for each operation is the 3\times3 grid of values collected before the update.
Note: The interactive protocol is simulated via a single input file. Please assume the following input format for your program (see the input description).
inputFormat
The input is given via standard input as follows:
- The first line contains two integers (n) and (m) -- the number of points and the number of operations.
- The second line contains (n) integers: (p[0], p[1], \ldots, p[n-1]) where point (i) has initial y-coordinate (p[i]) (the x-coordinate is (i)).
- The third line contains (n) integers: (d[0], d[1], \ldots, d[n-1]) representing the initial weight of each point.
- Then (m) blocks follow. For each operation (from (0) to (m-1)):
- One line contains four integers: (x_1, x_2, y_1, y_2) with (0 < x_1 < x_2 < n) and (0 < y_1 < y_2 < n).
- Next 3 lines follow, each containing 3 integers. These represent the (3 \times 3) matrix for the operation, i.e. the values for (o[w][X][Y]) for (X, Y \in {0,1,2}).
It is guaranteed that all numbers are integers and that the constraints on the indices hold.
outputFormat
For each operation (in order from 0 to (m-1)), output the corresponding 3\times3 answer grid. For each grid, print 3 lines; each line should contain 3 space-separated integers. Separate the grids by an empty line.
sample
3 1
0 1 2
1 2 3
1 2 1 2
2 2 2
2 2 2
2 2 2
1 0 0
0 2 0
0 0 3
</p>