$dfs$,优化。

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

对于$1$和$2$操作,可以开一个数组$a[i][j]$记录每一格子被操作$1$和$2$操作了几次。

然后开一个数组$r[i]$记录每一行被操作$3$操作了几次。 每一格真正的状态为$\left( {a\left[ i \right]\left[ j \right] + r\left[ i \right]} \right)\% 2$。 这样记录的话可以$O(1)$效率进行状态改变。

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. #include<vector>
  7. #include<map>
  8. #include<set>
  9. #include<queue>
  10. #include<stack>
  11. #include<iostream>
  12. using namespace std;
  13. typedef long long LL;
  14. const double pi=acos(-1.0),eps=1e-;
  15. void File()
  16. {
  17. freopen("D:\\in.txt","r",stdin);
  18. freopen("D:\\out.txt","w",stdout);
  19. }
  20.  
  21. struct X
  22. {
  23. int t,r,c,k,id;
  24. bool f;
  25. }s[];
  26. int sz;
  27.  
  28. int h[],r[],a[][];
  29. int ans,n,m,k;
  30. vector<int>G[];
  31. int Ans[];
  32.  
  33. void work1(int op,int tag)
  34. {
  35. if(tag==)
  36. {
  37. if((a[s[op].r][s[op].c]+r[s[op].r])%==) return;
  38. s[op].f=; a[s[op].r][s[op].c]++; h[s[op].r]++; ans++;
  39. }
  40.  
  41. else
  42. {
  43. if(s[op].f==) return ;
  44. a[s[op].r][s[op].c]--; h[s[op].r]--; ans--; s[op].f=;
  45. }
  46. }
  47.  
  48. void work2(int op,int tag)
  49. {
  50. if(tag==)
  51. {
  52. if((a[s[op].r][s[op].c]+r[s[op].r])%==) return;
  53. s[op].f=; a[s[op].r][s[op].c]--; h[s[op].r]--; ans--;
  54. }
  55.  
  56. else
  57. {
  58. if(s[op].f==) return;
  59. a[s[op].r][s[op].c]++; h[s[op].r]++; ans++; s[op].f=;
  60. }
  61. }
  62.  
  63. void work3(int op,int tag)
  64. {
  65. if(tag==)
  66. {
  67. s[op].f=;
  68. ans=ans-h[s[op].r]+(m-h[s[op].r]);
  69. h[s[op].r]=m-h[s[op].r];
  70. r[s[op].r]++;
  71. }
  72.  
  73. else
  74. {
  75. ans=ans-h[s[op].r]+(m-h[s[op].r]);
  76. h[s[op].r]=m-h[s[op].r];
  77. r[s[op].r]--; s[op].f=;
  78. }
  79. }
  80.  
  81. void dfs(int x)
  82. {
  83. if(s[x].t==) work1(x,);
  84. else if(s[x].t==) work2(x,);
  85. else if(s[x].t==) work3(x,);
  86.  
  87. for(int i=;i<G[x].size();i++)
  88. dfs(G[x][i]);
  89. Ans[x]=ans;
  90. if(s[x].t==) work1(x,);
  91. else if(s[x].t==) work2(x,);
  92. else if(s[x].t==) work3(x,);
  93. }
  94.  
  95. int main()
  96. {
  97. scanf("%d%d%d",&n,&m,&k);
  98. for(int i=;i<=k;i++)
  99. {
  100. scanf("%d",&s[i].t); int from=i-;
  101. if(s[i].t==) scanf("%d%d",&s[i].r,&s[i].c);
  102. else if(s[i].t==) scanf("%d%d",&s[i].r,&s[i].c);
  103. else if(s[i].t==) scanf("%d",&s[i].r);
  104. else scanf("%d",&from);
  105. G[from].push_back(i);
  106. }
  107. s[].t=; dfs();
  108. for(int i=;i<=k;i++) printf("%d\n",Ans[i]);
  109. return ;
  110. }

CodeForces 707D Persistent Bookcase的更多相关文章

  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)

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

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

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

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

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

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

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

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

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

  8. CodeForces #368 div2 D Persistent Bookcase DFS

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

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

    D. Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input stand ...

随机推荐

  1. 解析Infopath生成的XSN结构

    解析Infopath生成的XSN结构 解压XSN文件,得到下图文件 Infopath包括xsl.xsd.xsf.xml文件格式 Manifest.xsf是infopath的主要集合文件,包含对其他各个 ...

  2. elasticsearch data importing

    ElasticSearch stores each piece of data in a document. That's what I need. Using the bulk API. Trans ...

  3. ExtJS初接触 —— 了解 Ext Core

    ExtJS初接触 —— 了解 Ext Core Ext Core是一款和jQuery媲美的轻型JS库,基于MIT许可.对于Dom的操作,我个人还是比较喜欢用jQuery.当然如果项目中用的是ExtJS ...

  4. [修]python普通继承方式和super继承方式

    [转]python普通继承方式和super继承方式 原文出自:http://www.360doc.com/content/13/0306/15/9934052_269664772.shtml 原文的错 ...

  5. excel==>csv==via phpmyadmin (edit php.ini & my.ini)==> MySQL Database

    正如同标题, 标题的顺序是 先从Excel表单,保存为csv文档. 步骤: 1.这个可以用linux下的libra office打开 abc.xls 2.用libra office 将 abc.xls ...

  6. MFC控件(8):command button与syslink control

    在VS 2008里MFC多了4种控件,分别是 split buttons ,command button , syslink controls和 network address controls. s ...

  7. 用android代码显示图片的一部分源码

    ShowPoritionPictureActivity代码: [java] <span style="font-size:16px;"> package com.iwi ...

  8. Windows 8.1 Preview的新功能和新API

    http://msdn.microsoft.com/en-us/library/windows/apps/bg182410 App打包 新的App程序包将使App的提交更简单.资源包可以让你提供附加的 ...

  9. Android的状态栏通知(Notification)

    通知用于在状态栏显示消息,消息到来时以图标方式表示,如下: 如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息. 1.Layout布局文件: <RelativeLayout xmlns:an ...

  10. Maven 插件 maven-tomcat7-plugin - 常用命令及配置

    常用命令 tomcat7:deploy 说明:部署 WAR 到 Tomcat tomcat7:help 说明:查看插件帮助信息 tomcat7:run 说明:支行当前项目 配置 <project ...