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:

  • i j — Place a book at position j at shelf i if there is no book at it.
  • i j — Remove the book from position j at shelf i if there is a book at it.
  • 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.
  • 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 ≤ 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.

Example

Input
  1. 2 3 3
    1 1 1
    3 2
    4 0
Output
  1. 1
    4
    0
Input
  1. 4 2 6
    3 2
    2 2 2
    3 3
    3 2
    2 2 2
    3 2
Output
  1. 2
    1
    3
    3
    2
    4
Input
  1. 2 2 2
    3 2
    2 2 1
Output
  1. 2
    1

Note

This image illustrates the second sample case.

题意:现在有一个N*M的书架,有Q个操作,对于每个操作,输入opt:

如果opt==1,那么输入x,y,如果第x行第y列无书,则放一本书。

如果opt==2,那么输入x,y,如果第x行第y列有书,则取走那本书。

如果opt==3,那么输入x,将第x行有书的取走,无书的位置放一本。

如果opt==4,那么输入k,表示把书架的情况恢复为第k次操作后的样貌,k在当前操作之前。

思路:初看可能是可持久化数据结构,但是注意到整体操作顺序为有根树,可以DFS回溯,对于书架上的情况,可以直接积累或者Bitset假设。

  1. #include<bitset>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<iostream>
  5. #include<algorithm>
  6. using namespace std;
  7. const int maxn=;
  8. const int maxm=;
  9. bitset<maxn>s[maxn],P;
  10. int N,M,Q;
  11. int Laxt[maxm],Next[maxm],To[maxm],cnt;
  12. int opt[maxm],x[maxm],y[maxm],ans[maxm];
  13. void read(int &res)
  14. {
  15. char c=getchar(); res=;
  16. for(;c>''||c<'';c=getchar());
  17. for(;c<=''&&c>='';c=getchar()) res=(res<<)+(res<<)+c-'';
  18. }
  19. void add(int u,int v)
  20. {
  21. Next[++cnt]=Laxt[u];
  22. Laxt[u]=cnt;
  23. To[cnt]=v;
  24. }
  25. void dfs(int u,int Now)
  26. {
  27. for(int i=Laxt[u];i;i=Next[i]){
  28. int v=To[i];
  29. if(opt[v]==&&s[x[v]][y[v]]==) {
  30. s[x[v]][y[v]]=;
  31. ans[v]=Now+;
  32. dfs(v,ans[v]);
  33. s[x[v]][y[v]]=;
  34. }
  35. else if(opt[v]==&&s[x[v]][y[v]]==) {
  36. s[x[v]][y[v]]=;
  37. ans[v]=Now-;
  38. dfs(v,ans[v]);
  39. s[x[v]][y[v]]=;
  40. }
  41. else if(opt[v]==){
  42. ans[v]=Now-s[x[v]].count();
  43. s[x[v]]^=P;
  44. ans[v]+=s[x[v]].count();
  45. dfs(v,ans[v]);
  46. s[x[v]]^=P;
  47. }
  48. else {
  49. ans[v]=Now;
  50. dfs(v,ans[v]);
  51. }
  52. }
  53. }
  54. int main()
  55. {
  56. read(N); read(M); read(Q);
  57. for(int i=;i<=M;i++) P.set(i);
  58. for(int i=;i<=Q;i++){
  59. scanf("%d",&opt[i]);
  60. if(opt[i]==||opt[i]==) read(x[i]),read(y[i]);
  61. else read(x[i]);
  62. if(opt[i]==) add(x[i],i);
  63. else add(i-,i);
  64. }
  65. dfs(,);
  66. for(int i=;i<=Q;i++) printf("%d\n",ans[i]);
  67. return ;
  68. }

Codeforces-707D:Persistent Bookcase (离线处理特殊的可持久化问题&&Bitset)的更多相关文章

  1. 【离线】【深搜】【树】Codeforces 707D Persistent Bookcase

    题目链接: http://codeforces.com/problemset/problem/707/D 题目大意: 一个N*M的书架,支持4种操作 1.把(x,y)变为有书. 2.把(x,y)变为没 ...

  2. Codeforces 707D Persistent Bookcase(时间树)

    [题目链接] http://codeforces.com/problemset/problem/707/D [题目大意] 给出一个矩阵,要求满足如下操作,单个位置x|=1或者x&=0,一行的数 ...

  3. CodeForces 707D Persistent Bookcase

    $dfs$,优化. $return$操作说明该操作完成之后的状态和经过操作$k$之后的状态是一样的.因此我们可以建树,然后从根节点开始$dfs$一次(回溯的时候复原一下状态)就可以算出所有状态的答案. ...

  4. CodeForces 707D Persistent Bookcase ——(巧妙的dfs)

    一个n*m的矩阵,有四种操作: 1.(i,j)处变1: 2.(i,j)处变0: 3.第i行的所有位置1,0反转: 4.回到第k次操作以后的状态: 问每次操作以后整个矩阵里面有多少个1. 其实不好处理的 ...

  5. Persistent Bookcase CodeForces - 707D (dfs 离线处理有根树模型的问题&&Bitset)

    Persistent Bookcase CodeForces - 707D time limit per test 2 seconds memory limit per test 512 megaby ...

  6. Codeforces Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力

    D. Persistent Bookcase 题目连接: http://www.codeforces.com/contest/707/problem/D Description Recently in ...

  7. codeforces 707D D. Persistent Bookcase(dfs)

    题目链接: D. Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input ...

  8. Codeforces Round #368 (Div. 2) D. Persistent Bookcase

    Persistent Bookcase Problem Description: Recently in school Alina has learned what are the persisten ...

  9. CodeForces #368 div2 D Persistent Bookcase DFS

    题目链接:D Persistent Bookcase 题意:有一个n*m的书架,开始是空的,现在有k种操作: 1 x y 这个位置如果没书,放书. 2 x y 这个位置如果有书,拿走. 3 x 反转这 ...

随机推荐

  1. Hello SpringMVC

    1. MVC框架能做哪些事情? 讲url映射到java类或者方法 封装用户提交的数据 处理请求-调用相关业务处理-封装相应数据 将相应数据进行渲染 jsp/html/freemaker等 ... 2. ...

  2. hdu3491最小割转最大流+拆点

    题意:求最小割,即求最大流即可.此题之关键为拆点(限制在点),每条边都是双向边,注意一下. 未1A原因:在拆点之后添加边的过程中,要注意,出去的是i`,进来的是i,!!所以,写addegde函数时候 ...

  3. 安装配置JDK+Eclipse+Maven、Eclipse里新建Maven Project以及HDFS命令和Java API-课堂内容

    步骤:1.安装JDK→2.安装Eclipse→3.安装Maven→4. Eclipse里配置Maven (下载Windows版本,在Windows里安装使用.) 1.安装配置JDK ①官网下载Java ...

  4. PostgreSQL SystemTap on Linux

    http://digoal126.wap.blog.163.com/w2/blogDetail.do;jsessionid=3949B03DE151DA0E55D807466C5E630B.yqblo ...

  5. 从SOA到BFV【普元的一份广告文章】

    人类对美好生活的追求是一切技术进步的原动力. 简便.快捷.联结……,这些移动互联的价值让它正成为最贴近消费者的力量.人和设备,设备和设备,人和服务,人和企业,企业和企业都发生了连接.诸如微信.携程.大 ...

  6. MySQL安装总是失败,提示缺少Visual Studio 2013 Redistributable

    MySQL安装总是失败,提示缺少Visual Studio 2013 Redistributable,但是很疑惑,我明明已经安装了呀,原来问题出在版本上,以下提供了一个可以匹配新版本mysql的版本: ...

  7. Ghost本地安装highlight.js使代码高亮

    对于程序猿写博客来说,这代码高亮是起码的要求.可是Ghost本身没有支持高亮代码. 可是能够通过扩展来实现,它就是highlight.js--附官方站点,看了下首页介绍,真的非常强大,如今说说怎么进行 ...

  8. ZOJ ACM 1314(JAVA)

    昨天做了几个题目.过于简单,就不在博客里面写了. 1314这道题也比較简单,写出来是由于我认为在这里有一个小技巧,对于时间复杂度和空间复杂度都比較节省. 这个题目类似哈希表的求解.可是更简单.刚拿到题 ...

  9. INAPP登陆调用的FB接口

    public function login_get (){ $this->load->helper ( 'auth' ); $redirectUrl = $this->input-& ...

  10. 项目Beta冲刺(团队6/7)

    项目Beta冲刺(团队6/7) 团队名称: 云打印 作业要求: 项目Beta冲刺(团队) 作业目标: 完成项目Beta版本 团队队员 队员学号 队员姓名 个人博客地址 备注 221600412 陈宇 ...