Collecting Images is Fun
Time limit5sMemory limit256 MB
Start with an all-white 2^N by 2^N grid; after each of Q row or column flips, report the size of the quadtree that represents the image.
- Level
Hard8 of 10
- Topics
- Divide and conquer, Implementation, Math, Bit manipulation
- Solved
- No attempts yet
Problem
JOI loves collecting images and has a lot of them. Recently he noticed that he has collected too many images and his hard disk is running low on space. He does not have the money to buy a new hard disk, and deleting the images he owns would pain him more than anything, so he decided to compress the images well and reduce the space they take.
An image is represented by pixels arranged in a square with rows and columns. Each pixel is either white or black.
JOI decided to compress such an image in the following way.
- If all pixels in the image have the same color, record only that color. The size of the compressed data is then 1.
- Otherwise, split the image into 4 smaller images. If the image has rows and columns, split it at the center both vertically and horizontally to obtain 4 images with rows and columns. Compress these 4 smaller images in the same way. The size of the compressed data is then the sum of the sizes of the compressed data of the 4 smaller images, plus 1.
JOI became worried about whether this method really compresses images, so he decided to experiment with various images. The experiment works as follows.
- First, prepare an image in which every pixel is white.
- For , perform the operation: "if , invert the colors of the pixels in the -th row counted from the top; if , invert the colors of the pixels in the -th column counted from the left." That is, writing the pixel in row from the top and column from the left as , for each : if , then for every pixel with , and if , then for every pixel with , change white pixels to black and black pixels to white.
- For each , find the size of the compressed data when the image after the -th operation is compressed by JOI's method.
To perform as many operations as possible in the experiment, the size of the compressed data must be found quickly.
Given the integer representing the image size, the number of operations , and the instructions for the operations, write a program that finds the size of the compressed data when the image after each operation is compressed by JOI's method.
Input
Read the following input from standard input.
- The first line contains two integers separated by a space, representing that the image has rows and columns and that operations are performed.
- The following lines contain the operation instructions. The -th of these lines () contains two integers ( and ) separated by a space, representing that the -th operation inverts the colors of all pixels in the -th row from the top if , or in the -th column from the left if .
Output
Output lines to standard output. The -th line () contains a single integer: the size of the compressed data when the image after the -th operation is compressed by JOI's method.
Constraints
- .
- .