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 1to 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:
- 1 i j — Place a book at position j at shelf i if there is no book at it.
- 2 i j — Remove the book from position j at shelf i if there is a book at it.
- 3 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.
- 4 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?
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.
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.
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <map> #include <queue> #include <stack> #include <vector> #include <list> #define rep(i,m,n) for(i=m;i<=n;i++) #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define Lson L, mid, rt<<1 #define Rson mid+1, R, rt<<1|1 const int maxn=1e5+10; const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}}; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} int n,m,k,t,s[maxn][3],ans[maxn],sum[1010],q[1010],p[1010][1010]; vi a[maxn]; void dfs(int now) { for(int x:a[now]) { if(s[x][0]==4) { ans[x]=ans[now]; dfs(x); } else if(s[x][0]==1) { if((q[s[x][1]]^p[s[x][1]][s[x][2]])==0) { p[s[x][1]][s[x][2]]^=1; sum[s[x][1]]++; ans[x]=ans[now]+1; dfs(x); p[s[x][1]][s[x][2]]^=1; sum[s[x][1]]--; } else ans[x]=ans[now],dfs(x); } else if(s[x][0]==2) { if((q[s[x][1]]^p[s[x][1]][s[x][2]])==1) { p[s[x][1]][s[x][2]]^=1; sum[s[x][1]]--; ans[x]=ans[now]-1; dfs(x); p[s[x][1]][s[x][2]]^=1; sum[s[x][1]]++; } else ans[x]=ans[now],dfs(x); } else { q[s[x][1]]^=1; ans[x]=ans[now]-2*sum[s[x][1]]+m; sum[s[x][1]]=m-sum[s[x][1]]; dfs(x); q[s[x][1]]^=1; sum[s[x][1]]=m-sum[s[x][1]]; } } } int main() { int i,j; scanf("%d%d%d",&n,&m,&t); rep(i,1,t) { scanf("%d",&s[i][0]); if(s[i][0]==1||s[i][0]==2) { scanf("%d%d",&s[i][1],&s[i][2]); a[i-1].pb(i); } else if(s[i][0]==3) { scanf("%d",&s[i][1]); a[i-1].pb(i); } else { scanf("%d",&s[i][1]); a[s[i][1]].pb(i); } } dfs(0); rep(i,1,t)printf("%d ",ans[i]); //system("Pause"); return 0; }