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

网友投稿 623 2022-11-08

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

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

D. Persistent Bookcase

time limit per test

memory limit per test

input

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

1ij— Place a book at positionjat shelfi2ij— Remove the book from positionjat shelfi3i— Invert book placing at shelfi. This means that from every position at shelfiwhich has a book at it, the book should be removed, and at every position at shelfi4k— Return the books in the bookcase in a state they were after applyingk-th operation. In particular,k= 0

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

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#include#include#include#include#include#include#include#include#include#include#include#include 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 inline void getmax(T1 &a, T2 b) {if (b>a)a = b;} template inline void getmin(T1 &a, T2 b) {if (b 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); #endif n=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;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学构造)
下一篇:Codeforces Round #333 (Div. 2) E. Kleofáš and the n-thlon (概率dp)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~