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 exactlym positions for books at it. Alina enumerates shelves by integers from1 to n and positions at shelves — from1 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:
- 1 i j — Place a book at position j at shelfi if there is no book at it.
- 2 i j — Remove the book from position j at shelfi if there is a book at it.
- 3 i — Invert book placing at shelfi. This means that from every position at shelfi which has a book at it, the book should be removed, and at every position at shelfi which has not book at it, a book should be placed.
- 4 k — Return the books in the bookcase in a state they were after applyingk-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?
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 numberk corresponds to some operation before it or equals to0.
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.
2 3 3 1 1 1 3 2 4 0
1 4 0
4 2 6 3 2 2 2 2 3 3 3 2 2 2 2 3 2
2 1 3 3 2 4
2 2 2 3 2 2 2 1
2 1
This image illustrates the second sample case.
题目大意:
有一个开始是空的书柜,有四种操作,在某一位置加一本书,在某一位置减一本书,将某一行的书的状态反转(有书就取下,没书就加上)。回到某一次操作的状态。输出每一次操作后书架上书的个数。
解题思路:
题目一开始就说可持续化数据结构,再加上这题的输入输出又是这么标准的数据结构题模式,我就没有向其它方向思考。其实这题完全不用什么可持续化数据结构!!!只要离线处理,对每个操作按照顺序建一棵树。然后dfs模拟一下就可以了。。。
具体的建树:如果是操作4,我们把这个操作接在输入的k后面,否则接到上一个操作后面。(详情见代码)
AC代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn=1000+3;
const int maxq=100000+3;
int N,M,Q,op[maxq],x[maxq],y[maxq];
vector<int> G[maxq];
bool shell[maxn][maxn];
int ans[maxq],num;
void dfs(int u)
{
int done=0;//执行的操作,回溯用
if(op[u]==1&&!shell[x[u]][y[u]])
{
done=1;
++num;
shell[x[u]][y[u]]=true;
}
else if(op[u]==2&&shell[x[u]][y[u]])
{
done=2;
--num;
shell[x[u]][y[u]]=false;
}
else if(op[u]==3)
{
for(int i=1;i<=M;++i)
{
if(shell[x[u]][i])
{
--num;
shell[x[u]][i]=false;
}
else
{
++num;
shell[x[u]][i]=true;
}
}
}
ans[u]=num;//记录结果
for(int i=0;i<G[u].size();++i)
dfs(G[u][i]);
if(op[u]==1&&done==1)//回溯
{
--num;
shell[x[u]][y[u]]=false;
}
else if(op[u]==2&&done==2)
{
++num;
shell[x[u]][y[u]]=true;
}
else if(op[u]==3)
{
for(int i=1;i<=M;++i)
{
if(shell[x[u]][i])
{
--num;
shell[x[u]][i]=false;
}
else
{
++num;
shell[x[u]][i]=true;
}
}
}
}
int main()
{
scanf("%d%d%d",&N,&M,&Q);
for(int i=1;i<=Q;++i)
{
scanf("%d%d",&op[i],&x[i]);
if(op[i]==1||op[i]==2)
scanf("%d",&y[i]);
if(op[i]==4)//建树
G[x[i]].push_back(i);
else G[i-1].push_back(i);
}
for(int i=0;i<G[0].size();++i)
dfs(G[0][i]);
for(int i=1;i<=Q;++i)
printf("%d\n",ans[i]);
return 0;
}