time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
Recently in school Alina has learned what are the persistent data structures: they are data structures that always preserves the previous version of itself and access to it when it is modified.
After reaching home Alina decided to invent her own persistent data structure. Inventing didn't take long: there is a bookcase right behind her bed. Alina thinks that the bookcase is a good choice for a persistent data structure. Initially the bookcase is empty, thus there is no book at any position at any shelf.
The bookcase consists of n shelves, and each shelf has exactly m positions for books at it. Alina enumerates shelves by integers from 1 to n and positions at shelves — from 1 to m. Initially the bookcase is empty, thus there is no book at any position at any shelf in it.
Alina wrote down q operations, which will be consecutively applied to the bookcase. Each of the operations has one of four types:
After applying each of operation Alina is interested in the number of books in the bookcase. Alina got 'A' in the school and had no problem finding this values. Will you do so?
Input
The first line of the input contains three integers n, m and q (1 ≤ n, m ≤ 103, 1 ≤ q ≤ 105) — the bookcase dimensions and the number of operations respectively.
The next q lines describes operations in chronological order — i-th of them describes i-th operation in one of the four formats described in the statement.
It is guaranteed that shelf indices and position indices are correct, and in each of fourth-type operation the number k corresponds to some operation before it or equals to 0.
Output
For each operation, print the number of books in the bookcase after applying it in a separate line. The answers should be printed in chronological order.
Examples
input
Copy
2 3 3 1 1 1 3 2 4 0
output
Copy
1 4 0
input
Copy
4 2 6 3 2 2 2 2 3 3 3 2 2 2 2 3 2
output
Copy
2 1 3 3 2 4
input
Copy
2 2 2 3 2 2 2 1
output
Copy
2 1
Note
This image illustrates the second sample case.
点我传送
现在有一个N*M的书架,有Q个询问。存在四种操作
如果 1,那么输入x,y,如果第x行第y列无书,则放一本书。
如果2,那么输入x,y,如果第x行第y列有书,则取走那本书。
如果3,那么输入x,将第x行有书的取走,无书的位置放一本。
如果4,那么输入k,表示把书架的情况恢复到第K个版本。
预处理图,
对于操作1、2、3来说,实际上操作 i 继承的情况就是上一次的操作 i-1 ,所以建立的边是(i-1 , i)。
对于操作4来说,还原到版本 k 实际上就是,从版本 k 直接到达当前版本的意思,所以建立 ( k , i )的边。
以上的图跑DFS,这样就只需要一个[2000][2000]大小的矩阵就可以了。
#include
#include
#include
#include
#include
#include
#include