Reflection

시간 제한2초메모리 제한2048 MB

요약
N이 짝수인 N×N 격자가 주어질 때 가로 및 세로 반사를 모두 만족하도록 만드는 최소 칸 뒤집기 횟수를 구하고, 각 갱신 후에도 다시 출력한다.
난이도

보통10점 중 5점

유형
해시맵, 구현, 배열
정답자
아직 제출이 없습니다

문제

Farmer John has a square canvas represented by an NN by NN grid of cells (2≤N≤20002 \leq N \leq 2000, NN is even). He paints the canvas according to the following steps:

  1. First, he divides the canvas into four equal quadrants, separated by horizontal and vertical lines through the center of the canvas.
  2. Next, he paints a lovely painting in the top-right quadrant of the canvas. Each cell of the top-right quadrant will either be painted (represented by '#') or unpainted (represented by '.').
  3. Finally, since he is so proud of his painting, he reflects it across the previously mentioned horizontal and vertical lines into the other quadrants of the canvas.

For example, suppose N=8N=8 and FJ painted the following painting in the top-right quadrant in step 2:

.#..
.#..
.##.
....

Then after reflecting across the horizontal and vertical lines into the other quadrants in step 3, the canvas would look as follows:

..#..#..
..#..#..
.##..##.
........
........
.##..##.
..#..#..
..#..#..

However, while FJ was sleeping, Bessie broke into his barn and stole his precious canvas. She totally vandalized the canvas—removing some painted cells and adding more painted cells! Before FJ woke up, she returned the canvas to FJ.

FJ would like to modify his canvas so that it once again satisfies the reflective condition: that is, it is the result of reflecting the top-right quadrant into each of the other quadrants. Since he only has a limited number of resources, he would like to achieve this in as few operations as possible, where a single operation consists of either painting a cell or removing paint from a cell.

You are given the canvas after Bessie's vandalism, as well as a sequence of UU (0≤U≤1050\le U \leq 10^5) updates to the canvas, each toggling a single cell to '.' if it is '#', or vice versa. Before any updates, and after each update, output the minimum number of operations xx FJ needs to perform so that the reflective condition is satisfied.

입력

The first line contains integers NN and UU.

The next NN lines each contain NN characters representing the canvas after Bessie's vandalism. Every character is either '#' or '.'.

The following UU lines each contain integers rr and cc, where 1≤r,c≤N1 \leq r, c \leq N, representing an update to the cell in the rrth row from the top and ccth column from the left of the canvas.

출력

Output U+1U+1 lines representing xx before any updates and after each update.

예제1

  1. 예제 1

    입력
    4 5
    ..#.
    ##.#
    ####
    ..##
    1 3
    2 3
    4 3
    4 4
    4 4
    
    예상 출력
    4
    3
    2
    1
    0
    1