CodeForces707D.Persistent Bookcase(DFS)

                                                   D. Persistent Bookcase

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:

  • i j — Place a book at position j at shelf i if there is no book at it.
  • i j — Remove the book from position j at shelf i if there is a book at it.
  • i — Invert book placing at shelf i. This means that from every position at shelf i which has a book at it, the book should be removed, and at every position at shelf i which has not book at it, a book should be placed.
  • k — Return the books in the bookcase in a state they were after applying k-th operation. In particular, k = 0 means that the bookcase should be in initial state, thus every book in the bookcase should be removed from its position.

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

CodeForces707D.Persistent Bookcase(DFS)_第1张图片

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
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;


int n,m,Q;
const int N=1e5+5;
int op[N];
int posx[N],posy[N];
vectore[N];
int book[1005][1005];
int ans[N];


void DFS(int nx,int num)
{
    ans[nx]=num;
    for(int i=0;i

 

你可能感兴趣的:(CodeForces707D.Persistent Bookcase(DFS))