codeforces 707D:Persistent Bookcase

Description

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 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 3
1 1 1
3 2
4 0
Output
1
4
0
Input
4 2 6
3 2
2 2 2
3 3
3 2
2 2 2
3 2
Output
2
1
3
3
2
4
Input
2 2 2
3 2
2 2 1
Output
2
1



正解:搜索+操作树
解题报告:
  这道题误导我思考可持久化数据结构。。。
  先把操作离线,然后考虑当前操作由哪一步转过来。若是要完成第4个操作,我们需要保存下每步操作之后的全局局面,空间不够,不妨时间换空间。
  考虑我们每步操作都走向他能走向的操作(应对return操作),然后修改状态,得出当前答案,dfs下去,只需要在dfs完之后回溯,减掉这次操作的贡献就可以了。
  这种做法第一次接触,还是很神的。


 1 //It is made by jump~
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <ctime>
 9 #include <vector>
10 #include <queue>
11 #include <map>
12 #include <set>
13 using namespace std;
14 typedef long long LL;
15 const int MAXQ = 100011;
16 const int MAXN = 100011;
17 const int MAXM = 500011;
18 int n,m,q,now,ecnt;
19 int next[MAXM],first[MAXN],to[MAXM],f[MAXN];
20 int tag[1011],a[1011][1011];
21 int ans[MAXQ];
22 int cnt[1011];
23 int all_cnt;//全局
24 struct wen{
25     int type;
26     int x,y;
27 }Q[MAXQ];
28 
29 inline int getint()
30 {
31        int w=0,q=0;
32        char c=getchar();
33        while((c<'0' || c>'9') && c!='-') c=getchar();
34        if (c=='-')  q=1, c=getchar();
35        while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
36        return q ? -w : w;
37 }
38 
39 inline void link(int x,int y){next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y;}
40 
41 inline void dfs(int k){
42     bool flag=false;//是否做过修改
43     int tp=Q[k].type,x=Q[k].x,y=Q[k].y;
44     if(tp==1){ if(!(a[x][y]^tag[x])) cnt[x]++,all_cnt++,a[x][y]^=1,flag=true; }
45     else if(tp==2){ if(a[x][y]^tag[x]) cnt[x]--,all_cnt--,a[x][y]^=1,flag=true; }
46     else if(tp==3) flag=true,all_cnt-=cnt[x],tag[x]^=1,cnt[x]=m-cnt[x],all_cnt+=cnt[x];
47 
48     ans[k]=all_cnt;
49     for(int i=first[k];i;i=next[i]) dfs(to[i]);
50     if(!flag) return ;
51     if(tp==1) cnt[x]--,all_cnt--,a[x][y]^=1;
52     else if(tp==2) cnt[x]++,all_cnt++,a[x][y]^=1;
53     else if(tp==3) all_cnt-=cnt[x],tag[x]^=1,cnt[x]=m-cnt[x],all_cnt+=cnt[x];
54 }
55 
56 inline void work(){
57     n=getint(); m=getint(); q=getint();
58     now=0;//为操作连边
59     for(int i=1;i<=q;i++) {
60     Q[i].type=getint(); 
61     if(Q[i].type!=4) link(now,i);
62 
63     f[i]=now=i;
64     if(Q[i].type==1) Q[i].x=getint(),Q[i].y=getint(); 
65     else if(Q[i].type==2) Q[i].x=getint(),Q[i].y=getint();
66     else if(Q[i].type==3)   Q[i].x=getint(); 
67     else Q[i].x=getint(),Q[i].x=f[Q[i].x],f[i]=now=Q[i].x;//返回到k次操作之前的那次之后
68     }
69     dfs(0);
70     for(int i=1;i<=q;i++) printf("%d\n",ans[f[i]]);
71 }
72 
73 int main()
74 {
75   work();
76   return 0;
77 }

 

posted @ 2016-08-23 22:34  ljh_2000  阅读(439)  评论(0编辑  收藏  举报