Accel Thinking

CP Weekly Challenges

CCC 2021 J5/S2 Modern Art Solution: Count Gold Cells with Parity

Learn how to solve Modern Art with parity, row and column toggling, and an O(M + N + K) counting formula in Python and C++.

This problem looks like a two-dimensional grid simulation. We have an M x N canvas, every cell starts black, and each operation toggles one entire row or one entire column.

But the efficient solution does not build the whole canvas. The key idea is parity: whether each row and column has been toggled an odd or even number of times.

Where This Fits in the CCC DSA Map

What to Study Next

What the Problem Is Asking

Initially, every cell is black.

There are K brush operations:

Toggling reverses the color:

After all operations, we need to output the number of gold cells.

Where Beginners Often Get Stuck

A natural first attempt is to create the full grid:

grid = [[False] * N for _ in range(M)]

Then each row operation flips every cell in a row, and each column operation flips every cell in a column.

This may work for tiny examples, but it is not the right contest approach. The canvas can be too large to store or update directly.

Instead, ask: what determines the final color of one cell?

The answer is the number of times that cell was toggled.

If a cell is toggled an odd number of times, it ends gold. If it is toggled an even number of times, it ends black.

Core Algorithm Idea

For a cell (r, c), the total number of toggles is:

rowPaintCount[r] + colPaintCount[c]

The cell is affected only by its own row and its own column.

We do not need the exact counts. We only need odd or even.

So we maintain:

row_odd[r] = whether row r was toggled an odd number of times
col_odd[c] = whether column c was toggled an odd number of times

Each R x operation flips row_odd[x]. Each C y operation flips col_odd[y].

State and Counting Formula

Let:

R = number of rows toggled odd times
C = number of columns toggled odd times

A cell is gold exactly when one of its row or column is odd, but not both.

In Boolean terms:

row_odd[r] XOR col_odd[c] == True

There are two gold cases:

  1. odd row + even column
  2. even row + odd column

So the total number of gold cells is:

R * (N - C) + (M - R) * C

The first term counts cells in odd rows and even columns. The second term counts cells in even rows and odd columns.

Algorithm Steps

  1. Read M , N , and K .
  2. Create row_odd and col_odd .
  3. For each operation:
  1. Count how many rows are odd.
  2. Count how many columns are odd.
  3. Apply the formula:
R * (N - C) + (M - R) * C

Python Solution

M = int(input())
N = int(input())
K = int(input())

row_odd = [False] * (M + 1)
col_odd = [False] * (N + 1)

for _ in range(K):
    brush_type, num = input().split()
    idx = int(num)

    if brush_type == "R":
        row_odd[idx] = not row_odd[idx]
    else:
        col_odd[idx] = not col_odd[idx]

R = sum(row_odd)
C = sum(col_odd)

gold = R * (N - C) + (M - R) * C
print(gold)

C++ Solution

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int M, N, K;
    cin >> M >> N >> K;

    vector<bool> rowOdd(M + 1, false);
    vector<bool> colOdd(N + 1, false);

    for (int i = 0; i < K; i++) {
        char brushType;
        int idx;
        cin >> brushType >> idx;

        if (brushType == 'R') {
            rowOdd[idx] = !rowOdd[idx];
        } else {
            colOdd[idx] = !colOdd[idx];
        }
    }

    long long R = 0;
    long long C = 0;

    for (int i = 1; i <= M; i++) {
        if (rowOdd[i]) R++;
    }

    for (int j = 1; j <= N; j++) {
        if (colOdd[j]) C++;
    }

    long long gold = R * (N - C) + (M - R) * C;
    cout << gold << "\n";

    return 0;
}

Complexity Analysis

We avoid building the M x N grid.

Processing the K operations takes:

O(K)

Counting odd rows and columns takes:

O(M + N)

So the total time complexity is:

O(M + N + K)

The memory complexity is:

O(M + N)

Common Mistakes

  1. Building and updating the full M x N canvas.
  2. Tracking full paint counts when only parity matters.
  3. Forgetting that toggling the same row twice cancels out.
  4. Counting cells where both row and column are odd as gold, even though two toggles return them to black.
  5. Using int for the final answer in C++, which may overflow.

Review Questions

  1. Why does only odd/even toggle count matter?
  2. Why does painting the same row twice cancel out?
  3. What does R * (N - C) count?
  4. What does (M - R) * C count?
  5. Why is building the full grid unnecessary?