题目链接:

  http://codeforces.com/problemset/problem/707/D

题目大意:

  一个N*M的书架,支持4种操作

  1.把(x,y)变为有书。

  2.把(x,y)变为没书。

  3.把x行上的所有书状态改变,有变没,没变有。

  4.回到第K个操作时的状态。

  求每一次操作后书架上总共多少书。

题目思路:

  【离线】【深搜】【树】

  现场有思路不过没敢写哈。还是太弱了。

  总共只用保存一张图,把操作看成一棵树,一开始I操作连接在I-1操作后,如果遇到操作4的话,把I操作与I-1操作的边断开,改为连接到K下。

  这样把所有操作链完以后得到一棵多叉树,接下来深搜一遍,记录答案,回溯的时候把图的状态改回去即可。

  1. //
  2. //by coolxxx
  3. //#include<bits/stdc++.h>
  4. #include<iostream>
  5. #include<algorithm>
  6. #include<string>
  7. #include<iomanip>
  8. #include<map>
  9. #include<memory.h>
  10. #include<time.h>
  11. #include<stdio.h>
  12. #include<stdlib.h>
  13. #include<string.h>
  14. //#include<stdbool.h>
  15. #include<math.h>
  16. #define min(a,b) ((a)<(b)?(a):(b))
  17. #define max(a,b) ((a)>(b)?(a):(b))
  18. #define abs(a) ((a)>0?(a):(-(a)))
  19. #define lowbit(a) (a&(-a))
  20. #define sqr(a) ((a)*(a))
  21. #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
  22. #define mem(a,b) memset(a,b,sizeof(a))
  23. #define eps (1e-8)
  24. #define J 10
  25. #define mod 1000000007
  26. #define MAX 0x7f7f7f7f
  27. #define PI 3.14159265358979323
  28. #define N 1004
  29. #define M 100005
  30. using namespace std;
  31. typedef long long LL;
  32. int cas,cass;
  33. int n,m,lll,ans;
  34. struct xxx
  35. {
  36. int next,to,q,x,y;;
  37. }a[M];
  38. int last[M],an[M];
  39. bool mapp[N][N];
  40. void add(int x,int y)
  41. {
  42. a[++lll].next=last[x];
  43. a[lll].to=y;
  44. last[x]=lll;
  45. }
  46. void dfs(int now,int sum)
  47. {
  48. int i,j;
  49. an[now]=sum;
  50. for(i=last[now];i;i=a[i].next)
  51. {
  52. if(a[i].q==)
  53. {
  54. if(mapp[a[i].x][a[i].y])
  55. dfs(a[i].to,sum);
  56. else
  57. {
  58. mapp[a[i].x][a[i].y]=;
  59. dfs(a[i].to,sum+);
  60. mapp[a[i].x][a[i].y]=;
  61. }
  62. }
  63. else if(a[i].q==)
  64. {
  65. if(mapp[a[i].x][a[i].y])
  66. {
  67. mapp[a[i].x][a[i].y]=;
  68. dfs(a[i].to,sum-);
  69. mapp[a[i].x][a[i].y]=;
  70. }
  71. else dfs(a[i].to,sum);
  72. }
  73. else if(a[i].q==)
  74. {
  75. int k=;
  76. for(j=;j<=m;j++)
  77. {
  78. if(mapp[a[i].x][j])k--;
  79. else k++;
  80. mapp[a[i].x][j]^=;
  81. }
  82. dfs(a[i].to,sum+k);
  83. for(j=;j<=m;j++)mapp[a[i].x][j]^=;
  84. }
  85. else if(a[i].q==)
  86. dfs(a[i].to,sum);
  87. }
  88. }
  89. int main()
  90. {
  91. #ifndef ONLINE_JUDGE
  92. // freopen("1.txt","r",stdin);
  93. // freopen("2.txt","w",stdout);
  94. #endif
  95. int i,j,k;
  96. int x,y;
  97. // for(scanf("%d",&cas);cas;cas--)
  98. // for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
  99. // while(~scanf("%s",s+1))
  100. while(~scanf("%d",&n))
  101. {
  102. //lll=0;mem(fa,0);mem(last,0);
  103. scanf("%d%d",&m,&cas);
  104. for(i=;i<=cas;i++)
  105. {
  106. scanf("%d",&a[i].q);
  107. if(a[i].q<)
  108. scanf("%d%d",&a[i].x,&a[i].y);
  109. else if(a[i].q==)
  110. scanf("%d",&a[i].x);
  111. else if(a[i].q==)
  112. {
  113. scanf("%d",&a[i].x);
  114. add(a[i].x,i);
  115. continue;
  116. }
  117. add(i-,i);
  118. }
  119. dfs(,);
  120. for(i=;i<=cas;i++)
  121. printf("%d\n",an[i]);
  122. puts("");
  123. }
  124. return ;
  125. }
  126. /*
  127. //
  128.  
  129. //
  130. */

【离线】【深搜】【树】Codeforces 707D Persistent Bookcase的更多相关文章

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

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

  2. CodeForces 707D Persistent Bookcase

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

  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 Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力

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

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

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

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

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

  8. CodeForces #368 div2 D Persistent Bookcase DFS

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

  9. 【Codeforces-707D】Persistent Bookcase DFS + 线段树

    D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...

随机推荐

  1. PHP 开启报错机制

    屏蔽PHP错误提示 方法一:在有可能出错的函数前加@,然后or die("") 如: @mysql_connect(...) or die("Database Conne ...

  2. IIS7.5 asp+access数据库连接失败处理 64位系统

    IIS7.5 asp+access数据库连接失败处理(SRV 2008R2 x64/win7 x64) IIS7.5不支持oledb4.0驱动?把IIS运行模式设置成32位就可以了,微软没有支持出64 ...

  3. (转)修改ECSHOP前后台的title中的ecshop

    前台部分: 1:去掉头部TITLE部分的ECSHOP演示站 Powered by ecshop 前者在后台商店设置 - 商店标题修改 后者打开includes/lib_main.php $page_t ...

  4. [转]mysql自动定时备份数据库的最佳方法-支持windows系统

    网上有很多关于window下Mysql自动备份的方法,可是真的能用的也没有几个,有些说的还非常的复杂,难以操作. 我们都知道mssql本身就自带了计划任务可以用来自动备份,可是mysql咱们要怎么样自 ...

  5. 分享一个nodejs写的小论坛

    引言:作为一个前端小菜鸟,最近迷上了node,于是乎空闲时间,为了练练手写了一个node的小社区,关于微信小程序的,欢迎大家批评指导. 项目架构部分 一.前端架构 作为一个写样式也得无聊的前端狮,我偷 ...

  6. SGU 163.Wise King

    一道题目长的水题.... 总结就一句话,给出n个(-3~3)的数,一个数m,取任意个数是使这些数的m次幂之和最大. code #include <iostream> #include &l ...

  7. jQuery慢慢啃之事件(七)

    1.ready(fn)//当DOM载入就绪可以查询及操纵时绑定一个要执行的函数. $(document).ready(function(){ // 在这里写你的代码...}); 使用 $(docume ...

  8. linux 获取cpu百分比

    vmstat 1 |head -n 4 |tail -n 1 |awk '{print $13}'

  9. PHP 开启 ssh2

    首先,为PHP安装SSH2扩展需要两个软件包, libssh2和ssh2(php pecl拓展). 两者的最新版本分别为libssh2-1.5.0.tar.gz和ssh2-0.12.tgz,下载地址分 ...

  10. Python新手学习基础之初识python——与众不同2

    看完了Python的缩进,现在来看看Python的标识符.引号和注释. 标识符 关于Python的标识符,其实不是与众不同,只是有一定的规则. 标识符是编程时使用的名字.在Python中,标识符有几点 ...