Coloring (Codeforces 1574E)
Tags: Math, combinatorics

A matrix of size $$$n \times m$$$, such that each cell of it contains either $$$0$$$ or $$$1$$$, is considered beautiful if the sum in every contiguous submatrix of size $$$2 \times 2$$$ is exactly $$$2$$$, i. e. every "square" of size $$$2 \times 2$$$ contains exactly two $$$1$$$'s and exactly two $$$0$$$'s.You are given a matrix of size $$$n \times m$$$. Initially each cell of this matrix is empty. Let's denote the cell on the intersection of the $$$x$$$-th row and the $$$y$$$-th column as $$$(x, y)$$$. You have to process the queries of three types:  $$$x$$$ $$$y$$$ $$$-1$$$ — clear the cell $$$(x, y)$$$, if there was a number in it;  $$$x$$$ $$$y$$$ $$$0$$$ — write the number $$$0$$$ in the cell $$$(x, y)$$$, overwriting the number that was there previously (if any);  $$$x$$$ $$$y$$$ $$$1$$$ — write the number $$$1$$$ in the cell $$$(x, y)$$$, overwriting the number that was there previously (if any). After each query, print the number of ways to fill the empty cells of the matrix so that the resulting matrix is beautiful. Since the answers can be large, print them modulo $$$998244353$$$.

Input: The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n, m \le 10^6$$$; $$$1 \le k \le 3 \cdot 10^5$$$) — the number of rows in the matrix, the number of columns, and the number of queries, respectively.Then $$$k$$$ lines follow, the $$$i$$$-th of them contains three integers $$$x_i$$$, $$$y_i$$$, $$$t_i$$$ ($$$1 \le x_i \le n$$$; $$$1 \le y_i \le m$$$; $$$-1 \le t_i \le 1$$$) — the parameters for the $$$i$$$-th query.

Output: For each query, print one integer — the number of ways to fill the empty cells of the matrix after the respective query, taken modulo $$$998244353$$$.


Sample Inputs:
2 2 7
1 1 1
1 2 1
2 1 1
1 1 0
1 2 -1
2 1 -1
1 1 -1

Sample Outputs:
3
1
0
1
2
3
6

Solution (Python):
import sys
input = sys.stdin.readline
p = 998244353

class matrix_data:

    def __init__(self, n, m):
        self.n = n
        self.m = m
        self.filled = {}
        self.alternating = [0, 0]
        self.rows = [[0] * (n + 1), [0] * (n + 1)]
        self.cols = [[0] * (m + 1), [0] * (m + 1)]
        self.frozen_rows = 0
        self.frozen_cols = 0
        self.bad_rows = 0
        self.bad_cols = 0

    def clear(self, x, y):
        if (x, y) not in self.filled:
            return
        t = self.filled.pop((x, y))
        self.alternating[(x + y + t) % 2] -= 1
        self.rows[(y + t) % 2][x] -= 1
        if self.rows[(y + t) % 2][x] == 0:
            if self.rows[(y + t + 1) % 2][x] == 0:
                self.frozen_rows -= 1
            else:
                self.bad_rows -= 1
        self.cols[(x + t) % 2][y] -= 1
        if self.cols[(x + t) % 2][y] == 0:
            if self.cols[(x + t + 1) % 2][y] == 0:
                self.frozen_cols -= 1
            else:
                self.bad_cols -= 1

    def write(self, x, y, t):
        self.filled[x, y] = t
        self.alternating[(x + y + t) % 2] += 1
        self.rows[(y + t) % 2][x] += 1
        if self.rows[(y + t) % 2][x] == 1:
            if self.rows[(y + t + 1) % 2][x] == 0:
                self.frozen_rows += 1
            else:
                self.bad_rows += 1
        self.cols[(x + t) % 2][y] += 1
        if self.cols[(x + t) % 2][y] == 1:
            if self.cols[(x + t + 1) % 2][y] == 0:
                self.frozen_cols += 1
            else:
                self.bad_cols += 1

    def count(self):
        total = 0
        if self.bad_rows == 0:
            total += pow(2, n - self.frozen_rows, p)
        if self.bad_cols == 0:
            total += pow(2, m - self.frozen_cols, p)
        if self.alternating[0] == 0:
            total -= 1
        if self.alternating[1] == 0:
            total -= 1
        print(total % p)
(n, m, k) = map(int, input().split())
data = matrix_data(n, m)
for _ in range(k):
    (x, y, t) = map(int, input().split())
    if t == -1:
        data.clear(x, y)
    else:
        data.clear(x, y)
        data.write(x, y, t)
    data.count()

Solution (Python):
import sys
input = sys.stdin.readline
p = 998244353

class matrix_data:

    def __init__(self, n, m):
        self.n = n
        self.m = m
        self.filled = {}
        self.alternating = [0, 0]
        self.rows = [[0] * (n + 1), [0] * (n + 1)]
        self.cols = [[0] * (m + 1), [0] * (m + 1)]
        self.frozen_rows = 0
        self.frozen_cols = 0
        self.bad_rows = 0
        self.bad_cols = 0

    def clear(self, x, y):
        if (x, y) not in self.filled:
            return
        t = self.filled.pop((x, y))
        self.alternating[(x + y + t) % 2] -= 1
        self.rows[(y + t) % 2][x] -= 1
        if self.rows[(y + t) % 2][x] == 0:
            if self.rows[(y + t + 1) % 2][x] == 0:
                self.frozen_rows -= 1
            else:
                self.bad_rows -= 1
        self.cols[(x + t) % 2][y] -= 1
        if self.cols[(x + t) % 2][y] == 0:
            if self.cols[(x + t + 1) % 2][y] == 0:
                self.frozen_cols -= 1
            else:
                self.bad_cols -= 1

    def write(self, x, y, t):
        self.filled[x, y] = t
        self.alternating[(x + y + t) % 2] += 1
        self.rows[(y + t) % 2][x] += 1
        if self.rows[(y + t) % 2][x] == 1:
            if self.rows[(y + t + 1) % 2][x] == 0:
                self.frozen_rows += 1
            else:
                self.bad_rows += 1
        self.cols[(x + t) % 2][y] += 1
        if self.cols[(x + t) % 2][y] == 1:
            if self.cols[(x + t + 1) % 2][y] == 0:
                self.frozen_cols += 1
            else:
                self.bad_cols += 1

    def count(self):
        total = 0
        if self.bad_rows == 0:
            total += pow(2, n - self.frozen_rows, p)
        if self.bad_cols == 0:
            total += pow(2, m - self.frozen_cols, p)
        if self.alternating[0] == 0:
            total -= 1
        if self.alternating[1] == 0:
            total -= 1
        print(total % p)
(n, m, k) = map(int, input().split())
data = matrix_data(n, m)
for _ in range(k):
    (x, y, t) = map(int, input().split())
    if t == -1:
        data.clear(x, y)
    else:
        data.clear(x, y)
        data.write(x, y, t)
    data.count()

Solution (Python):
import sys
input = sys.stdin.readline
p = 998244353

class matrix_data:

    def __init__(self, n, m):
        self.n = n
        self.m = m
        self.filled = {}
        self.alternating = [0, 0]
        self.rows = [[0] * (n + 1), [0] * (n + 1)]
        self.cols = [[0] * (m + 1), [0] * (m + 1)]
        self.frozen_rows = 0
        self.frozen_cols = 0
        self.bad_rows = 0
        self.bad_cols = 0

    def clear(self, x, y):
        if (x, y) not in self.filled:
            return
        t = self.filled.pop((x, y))
        self.alternating[(x + y + t) % 2] -= 1
        self.rows[(y + t) % 2][x] -= 1
        if self.rows[(y + t) % 2][x] == 0:
            if self.rows[(y + t + 1) % 2][x] == 0:
                self.frozen_rows -= 1
            else:
                self.bad_rows -= 1
        self.cols[(x + t) % 2][y] -= 1
        if self.cols[(x + t) % 2][y] == 0:
            if self.cols[(x + t + 1) % 2][y] == 0:
                self.frozen_cols -= 1
            else:
                self.bad_cols -= 1

    def write(self, x, y, t):
        self.filled[x, y] = t
        self.alternating[(x + y + t) % 2] += 1
        self.rows[(y + t) % 2][x] += 1
        if self.rows[(y + t) % 2][x] == 1:
            if self.rows[(y + t + 1) % 2][x] == 0:
                self.frozen_rows += 1
            else:
                self.bad_rows += 1
        self.cols[(x + t) % 2][y] += 1
        if self.cols[(x + t) % 2][y] == 1:
            if self.cols[(x + t + 1) % 2][y] == 0:
                self.frozen_cols += 1
            else:
                self.bad_cols += 1

    def count(self):
        total = 0
        if self.bad_rows == 0:
            total += pow(2, n - self.frozen_rows, p)
        if self.bad_cols == 0:
            total += pow(2, m - self.frozen_cols, p)
        if self.alternating[0] == 0:
            total -= 1
        if self.alternating[1] == 0:
            total -= 1
        print(total % p)
(n, m, k) = map(int, input().split())
data = matrix_data(n, m)
for _ in range(k):
    (x, y, t) = map(int, input().split())
    if t == -1:
        data.clear(x, y)
    else:
        data.clear(x, y)
        data.write(x, y, t)
    data.count()