#P11977. Pixelated Rectangles Movement

    ID: 14088 Type: Default 1000ms 256MiB

Pixelated Rectangles Movement

Pixelated Rectangles Movement

On an old computer with a primitive drawing program, the screen is represented as a grid of pixels. The pixel at the bottom‐left is located at \((1,1)\). The pixel at the \(a\)th position from the left and the \(b\)th position from the bottom is \((a, b)\).

Initially, there are \(N\) axis‐aligned rectangles drawn on the screen. Each rectangle is defined by its bottom–left and top–right pixel coordinates and includes every pixel inside that region.

Then, \(M\) move commands are executed sequentially. Each command instructs one of the rectangles to move in one of eight directions: east, north, west, south, northeast, northwest, southwest, or southeast. The move command also specifies a distance \(d\). More precisely, if a rectangle’s bottom–left pixel is \((a,b)\) then after moving by distance \(d\) its new bottom–left will be:

  • East: \((a+d, b)\)
  • North: \((a, b+d)\)
  • West: \((a-d, b)\)
  • South: \((a, b-d)\)
  • Northeast: \((a+d, b+d)\)
  • Northwest: \((a-d, b+d)\)
  • Southwest: \((a-d, b-d)\)
  • Southeast: \((a+d, b-d)\)

During the movement, the rectangle is drawn at every intermediate step (each unit move). For example, moving a rectangle 3 units to the northeast creates 3 additional drawings along its path; all drawn images remain on the screen. At the end of the move, the rectangle remains at its final position.

After all moves, there are \(Q\) queries. Each query specifies a pixel \(p\), and the task is to determine how many of the drawn rectangles (both initial and those drawn during the moves) contain pixel \(p\). A pixel \((x,y)\) is contained in a rectangle with bottom–left \((a,b)\) and top–right \((c,d)\) if \(a \le x \le c\) and \(b \le y \le d\), where boundaries are inclusive.

Implementation Details:

You must implement the following function:

vector<long long int> count_enclosing_rectangle(
    vector< pair<int, int> > R1, 
    vector< pair<int, int> > R2, 
    vector<int> V, 
    vector<int> I, 
    vector<int> D, 
    vector< pair<int, int> > P
);

The parameters are as follows:

  • R1 and R2 are of size \(N\). For \(1 \le i \le N\), \(R1[i-1]\) gives the bottom–left pixel \((a,b)\) and \(R2[i-1]\) gives the top–right pixel \((c,d)\) of the \(i\)th rectangle.
  • V, I, D are of size \(M\) and describe the move commands. For the \(i\)th command, rectangle number \(I[i]\) (1-indexed) moves in the direction given by \(V[i]\) for a distance \(D[i]\). The eight possible directions are encoded as integers:
    0: East, 1: North, 2: West, 3: South, 4: Northeast, 5: Northwest, 6: Southwest, 7: Southeast.
  • P is of size \(Q\). Each element in \(P\) is a pixel coordinate \((x,y)\) representing a query.

The function should return an array of \(Q\) long long integers, where the \(i\)th integer is the number of rectangles (both original and those drawn during moves) that contain the \(i\)th query pixel.

inputFormat

The input is provided via function parameters:

  • \(N\): number of initial rectangles; followed by two arrays R1 and R2 representing the bottom–left and top–right coordinates.
  • \(M\): number of move commands; followed by three arrays V, I, and D representing the move directions, the rectangle indices (1-indexed), and the move distances.
  • \(Q\): number of queries; and an array P containing the query pixel coordinates.

You do not need to handle any input or output operations; implement the function only.

outputFormat

The function count_enclosing_rectangle should return an array of \(Q\) long long integers. The \(i\)th element in the output array is the number of rectangles that contain the \(i\)th query pixel.

sample

1 1 3
1 1 3 3
4 1 2
1 1
2 2
4 4
1

2 2

</p>