#P5232. The Disastrous Switch
The Disastrous Switch
The Disastrous Switch
In the year 1371 AD, Emperor Taizu ordered the construction of ten temples atop Chicken Cage Mountain. To facilitate the pilgrimage, a stone bridge was built with a mechanical grid of size \(Rx \times Ry\) suspended from its center. Each cell in the grid is flippable: one side is white (denoted by 0) and the other black (denoted by 1). Initially, all cells are white.
There are \(Rx+Ry\) buttons, with the first \(Rx\) corresponding to rows and the remaining \(Ry\) corresponding to columns. When a button is triggered, every cell in its corresponding row or column is flipped (white becomes black and vice versa). A renowned minister, Liu Ji, declared a special pattern called the "misfortune star." After a visitor presses a button, if the resulting grid coincides exactly with this misfortune star, the visitor is not allowed to pass.
It is guaranteed that the misfortune star pattern provided is achievable by a single flip; that is, it is either a row pattern (one entire row is black and all other cells white) or a column pattern (one entire column is black and all other cells white). In these cases, the corresponding button is:
• For a row pattern on row \(i\): button \(i\).
• For a column pattern on column \(j\): button \(Rx+j\).
There are \(N\) visitors per day, and initially every visitor triggers button 1. However, visitors may later change their chosen button either individually or in a contiguous group. You are to process \(Q\) operations. Each operation is either:
- An update operation that sets the triggered button for a range of visitors.
- A query operation asking for the number of visitors in a given interval whose chosen button produces the misfortune star.
Your task is to output the answer for each query.
inputFormat
The first line contains four integers \(Rx\), \(Ry\), \(N\), and \(Q\), where \(Rx\) and \(Ry\) specify the grid dimensions, \(N\) is the number of visitors, and \(Q\) is the number of operations.
The next \(Rx\) lines each contain \(Ry\) integers (each 0 or 1), representing the misfortune star pattern.
The following \(Q\) lines each describe an operation in one of two formats:
1 l r x
: Update the visitors from index \(l\) to \(r\) (inclusive) so that each now triggers button \(x\) (\(1 \le x \le Rx+Ry\)).2 l r
: Query the number of visitors in the interval \([l, r]\) whose triggered button produces the misfortune star.
Initially, all visitors trigger button 1.
outputFormat
For each query operation (lines beginning with 2), output a single integer on a new line, representing the number of visitors whose triggered button yields the misfortune star pattern.
sample
3 4 5 5
0 0 0 0
1 1 1 1
0 0 0 0
2 1 5
1 2 4 2
2 1 5
1 5 5 2
2 3 5
0
3
3
</p>