Under construction

Still being built. Things may change

Holiday Painting

Time limit2sMemory limit128 MB

Summary
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 R×CR \times C grid of unit squares (1≤R≤50,0001 \le R \le 50{,}000, 1≤C≤151 \le C \le 15), where each square holds a 00 or a 11. Rows are numbered 11 through RR from top to bottom, and columns 11 through CC 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 00. Each throw covers one rectangle of the grid, setting every square inside it to a single color (00 or 11).

The cows perform QQ paint operations in order (1≤Q≤10,0001 \le Q \le 10{,}000). Operation ii is given by five integers R1i,R2i,C1i,C2i,XiR1_i, R2_i, C1_i, C2_i, X_i (1≤R1i≤R2i≤R1 \le R1_i \le R2_i \le R, 1≤C1i≤C2i≤C1 \le C1_i \le C2_i \le C, 0≤Xi≤10 \le X_i \le 1), meaning that every square whose row index is between R1iR1_i and R2iR2_i inclusive and whose column index is between C1iC1_i and C2iC2_i inclusive is painted color XiX_i.

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 RR, CC, and QQ.
  • Lines 2 to R+1R+1: line i+1i+1 contains CC characters ('0' or '1') with no spaces, giving row ii of the target picture.
  • Lines R+2R+2 to R+Q+1R+Q+1: each line contains five space-separated integers describing a paint operation: R1iR1_i, R2iR2_i, C1iC1_i, C2iC2_i, XiX_i.

Output

  • Print QQ lines. Line ii contains a single integer: the number of squares that match the target picture immediately after operation ii.

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 113113 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

Examples1

  1. Example 1

    Input
    17 15 10
    111111101111111
    111111000111111
    111110000011111
    111100000001111
    111000000000111
    111100000001111
    111000000000111
    110000000000011
    111000000000111
    110000000000011
    100000000000001
    110000000000011
    100000000000001
    000000000000000
    111111000111111
    111111000111111
    111111000111111
    5 8 2 14 1
    8 17 3 7 1
    4 5 10 15 0
    7 16 12 14 1
    2 17 13 14 0
    2 6 2 3 1
    13 14 4 8 1
    3 6 6 7 1
    1 16 10 11 0
    7 16 10 10 0
    
    Expected output
    113
    94
    95
    91
    87
    93
    91
    87
    93
    93