#P11053. [IOI 2024] 马赛克上色

[IOI 2024] 马赛克上色

Background

Do not include mosaic.h when submitting.

Do not use C++14 (GCC 9) for submission.

Problem Description

Salma wants to color a clay mosaic on a wall. The mosaic consists of N×NN \times N square tiles, for a total of N2N^2 tiles. Each tile has size 1×11 \times 1 and is initially uncolored. The rows of the mosaic are numbered from 00 to N1N-1 from top to bottom, and the columns are numbered from 00 to N1N-1 from left to right. The tile in row ii and column jj (0i<N0 \leq i < N, 0j<N0 \leq j < N) is denoted (i,j)(i,j). Each tile is colored either white (denoted 00) or black (denoted 11).

To color the mosaic, Salma first chooses two arrays XX and YY of length NN, each consisting of 00 and 11, and with X[0]=Y[0]X[0] = Y[0]. She colors the top row (row 00) according to array XX, so that tile (0,j)(0,j) has color X[j]X[j] (0j<N0 \leq j < N). She colors the leftmost column (column 00) according to array YY, so that tile (i,0)(i,0) has color Y[i]Y[i] (0i<N0 \leq i < N).

Then she repeats the following steps until all tiles are colored:

  • She finds any uncolored tile (i,j)(i,j) such that the tile directly above it (i1,j)(i-1, j) and the tile directly to its left (i,j1)(i, j-1) are both already colored.
  • Then, if both of these adjacent tiles are white, she colors tile (i,j)(i,j) black; otherwise, she colors it white.

It can be proven that the final colors of the tiles do not depend on the order in which Salma colors them.

Yasmin is very curious about the colors of the mosaic tiles. She asks Salma QQ questions, numbered from 00 to Q1Q-1. In question kk (0k<Q0 \leq k < Q), Yasmin specifies a rectangle in the mosaic using the following information:

  • The top row T[k]T[k] and the bottom row B[k]B[k] (0T[k]B[k]<N0 \leq T[k] \leq B[k] < N).
  • The left column L[k]L[k] and the right column R[k]R[k] (0L[k]R[k]<N0 \leq L[k] \leq R[k] < N).

The answer to a question is the number of black tiles inside that rectangle. More precisely, Salma should determine how many tiles (i,j)(i,j) satisfy T[k]iB[k]T[k] \leq i \leq B[k], L[k]jR[k]L[k] \leq j \leq R[k], and are colored black.

Write a program to answer Yasmin’s questions.

Implementation Details

You need to implement the following function.

std::vector<long long> mosaic(
	std::vector<int> X, std::vector<int> Y,
    std::vector<int> T, std::vector<int> B,
    std::vector<int> L, std::vector<int> R)
  • XX, YY: arrays of length NN, describing the colors of the top row and the leftmost column, respectively.
  • TT, BB, LL, RR: arrays of length QQ, describing Yasmin’s questions.
  • The function should return an array CC of length QQ, where C[k]C[k] gives the answer to question kk (0k<Q0 \leq k < Q).
  • For each test case, this function is called exactly once.

Input Format

The sample grader reads input in the following format:

N
X[0]  X[1]  ...  X[N-1]
Y[0]  Y[1]  ...  Y[N-1]
Q
T[0]  B[0]  L[0]  R[0]
T[1]  B[1]  L[1]  R[1]
...
T[Q-1]  B[Q-1]  L[Q-1]  R[Q-1]

Output Format

The sample grader prints your answers in the following format:

C[0]
C[1]
...
C[S-1]

Here, SS is the length of the array CC returned by mosaic.

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

7
3

Hint

Consider the following function call.

mosaic([1, 0, 1, 0], [1, 1, 0, 1], [0, 2], [3, 3], [0, 0], [3, 2])

This example is shown in the figure below. The figure on the left shows the colors of the tiles in the mosaic. The middle and right figures show the rectangles in Yasmin’s first and second questions, respectively.

The answers to the two questions (that is, the number of 11 in the shaded rectangles) are 77 and 33, respectively. Therefore, the function should return [7,3][7, 3].

Constraints

  • 1N2000001 \leq N \leq 200\,000
  • 1Q2000001 \leq Q \leq 200\,000
  • For all ii with 0i<N0 \leq i < N, X[i]{0,1}X[i] \in \{0, 1\} and Y[i]{0,1}Y[i] \in \{0, 1\}.
  • X[0]=Y[0]X[0] = Y[0].
  • For all kk with 0k<Q0 \leq k < Q, 0T[k]B[k]<N0 \leq T[k] \leq B[k] < N and 0L[k]R[k]<N0 \leq L[k] \leq R[k] < N.
Subtask Score Additional Constraints
1 55 N2;Q10N \leq 2; Q \leq 10
2 77 N200;Q200N \leq 200; Q \leq 200
3 For all kk with 0k<Q0 \leq k < Q, T[k]=B[k]=0T[k] = B[k] = 0
4 1010 N5000N \leq 5000
5 88 For all ii with 0i<N0 \leq i < N, X[i]=Y[i]=0X[i] = Y[i] = 0
6 2222 For all kk with 0k<Q0 \leq k < Q, T[k]=B[k]T[k] = B[k] and L[k]=R[k]L[k] = R[k]
7 1919 For all kk with 0k<Q0 \leq k < Q, T[k]=B[k]T[k] = B[k]
8 2222 No additional constraints.

Translated by ChatGPT 5