Codeforces Round #368 (Div. 2) D. Persistent Bookcase (离线+dfs)

来源:互联网 发布:js if undefined 编辑:程序博客网 时间:2024/05/22 02:00

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:

  • 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?

Input

The first line of the input contains three integers nm and q (1 ≤ n, m ≤ 1031 ≤ 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
2 3 31 1 13 24 0
output
140
input
4 2 63 22 2 23 33 22 2 23 2
output
213324
input
2 2 23 22 2 1
output
21
Note

This image illustrates the second sample case.


题意:给你有n个书架,每个书架有m层。
有四个操作:

1 i j,在第i个书架第j层放一本书。
2 i j,把第i个书架第j层的书扔掉。
3 i,把第三层的所有书的状态取反,有的变没,没的变有。
4 k,回到第k个询问时候的状态。如果k=0,书架全部清空。

题解:离线处理一下,对每个操作按照顺序建一棵树。前3个操作怎么搞都行。第4个操作,我们可以根据询问的顺序建树,然后dfs搞一下就可以啦。


代码:

#pragma comment(linker, "/STACK:102400000,102400000")//#include<bits/stdc++.h>#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<cstring>#include<vector>#include<map>#include<cmath>#include<queue>#include<set>#include<stack>#include <utility>using namespace std;typedef long long ll;typedef unsigned long long ull;#define mst(a) memset(a, 0, sizeof(a))#define M_P(x,y) make_pair(x,y)  #define rep(i,j,k) for (int i = j; i <= k; i++)  #define per(i,j,k) for (int i = j; i >= k; i--)  #define lson x << 1, l, mid  #define rson x << 1 | 1, mid + 1, r  const int lowbit(int x) { return x&-x; }  const double eps = 1e-8;  const int INF = 1e9+7; const ll inf =(1LL<<62) ;const int MOD = 1e9 + 7;  const ll mod = (1LL<<32);const int N = 101010; const int M=100010; template <class T1, class T2>inline void getmax(T1 &a, T2 b) {if (b>a)a = b;}  template <class T1, class T2>inline void getmin(T1 &a, T2 b) {if (b<a)a = b;}int read(){int v = 0, f = 1;char c =getchar();while( c < 48 || 57 < c ){if(c=='-') f = -1;c = getchar();}while(48 <= c && c <= 57) v = v*10+c-48, c = getchar();return v*f;}struct bookcase {int op,x,y;int ans;}q[M];vector<int> v[M];bool a[1005][1005];int sum;int n,m,Q;void dfs(int x){int ok=0;if(q[x].op==1 && !a[q[x].x][q[x].y]){ok=1;a[q[x].x][q[x].y]=true;sum++;}else if(q[x].op==2 && a[q[x].x][q[x].y]){a[q[x].x][q[x].y]=false;ok=2;sum--;}else if(q[x].op==3){int i=q[x].x;ok=3;for(int j=1;j<=m;j++){if(a[i][j]){a[i][j]=false;sum--;}else{a[i][j]=true;sum++;}}}q[x].ans=sum;for(int i=0;i<(int)v[x].size();i++){dfs(v[x][i]);}if(ok==1){a[q[x].x][q[x].y]=false;sum--;}else if(ok==2){a[q[x].x][q[x].y]=true;sum++;}else if(ok==3){int i=q[x].x;for(int j=1;j<=m;j++){if(a[i][j]){a[i][j]=false;sum--;}else{a[i][j]=true;sum++;}}}}int main(){#ifndef ONLINE_JUDGE    freopen("in.txt","r",stdin);    #endifn=read(),m=read(),Q=read();for(int i=1;i<=Q;i++){scanf("%d%d",&q[i].op,&q[i].x);if(q[i].op<=2) scanf("%d",&q[i].y);if(q[i].op==4) v[q[i].x].push_back(i);else v[i-1].push_back(i);}dfs(0);for(int i=1;i<=Q;i++)printf("%d\n",q[i].ans);return 0;}


2 0