Holiday Painting
Time limit2sMemory limit128 MB
Paint rectangle updates on an R x C grid, R up to 50000 and C up to 15, and after each of Q updates report how many cells equal a fixed target pattern.
- Level
Hard8 of 10
- Topics
- Segment tree, Bit manipulation, Implementation, Matrix
- Solved
- No attempts yet
Problem
To celebrate the holidays, the cows want to paint a picture. The picture is an grid of unit squares (, ), where each square holds a or a . Rows are numbered through from top to bottom, and columns through from left to right. There is one fixed target picture the cows want to reproduce.
Pressed for time, the cows built a machine that hurls paint by the bucket. The grid starts with every square set to . Each throw covers one rectangle of the grid, setting every square inside it to a single color ( or ).
The cows perform paint operations in order (). Operation is given by five integers (, , ), meaning that every square whose row index is between and inclusive and whose column index is between and inclusive is painted color .
Immediately after each operation, determine how many squares of the current grid match the color of the corresponding square in the target picture.
Input
- Line 1: three space-separated integers , , and .
- Lines 2 to : line contains characters ('0' or '1') with no spaces, giving row of the target picture.
- Lines to : each line contains five space-separated integers describing a paint operation: , , , , .
Output
- Print lines. Line contains a single integer: the number of squares that match the target picture immediately after operation .
Explanation
In the first example, after the first operation the grid looks like this:
000000000000000
000000000000000
000000000000000
000000000000000
011111111111110
011111111111110
011111111111110
011111111111110
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
At this point squares match the target picture; they are marked with 'x' below (the remaining squares are shown with their actual color after the first paint throw).
0000000x0000000
000000xxx000000
00000xxxxx00000
0000xxxxxxx0000
0xx111111111xx0
0xxx1111111xxx0
0xx111111111xx0
0x11111111111x0
000xxxxxxxxx000
00xxxxxxxxxxx00
0xxxxxxxxxxxxx0
00xxxxxxxxxxx00
0xxxxxxxxxxxxx0
xxxxxxxxxxxxxxx
000000xxx000000
000000xxx000000
000000xxx000000