【题目链接】 http://codeforces.com/problemset/problem/707/D

【题目大意】

  给出一个矩阵,要求满足如下操作,单个位置x|=1或者x&=0,一行的数全部取反,回到第k个操作。要求每次操作后输出这个矩阵中数字的和。

【题解】

  由于存在操作回溯,考虑使用可持久化数据结构或者建立离线操作树。因为懒,没写持久化。对于每个操作,将它和时间顺序上的上一个节点连边,这样子就形成了一棵树,对这棵树从根节点开始遍历,递归处理,每次处理完一棵子树就回溯操作,这样子就能离线处理操作的持久化了。

【代码】

  1. #include <cstdio>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5. const int N=1005,M=100005;
  6. int k,q,n,m,t[N],f[N][N],s[N],last=0,ans[M],op[M],x[M],y[M],id[M];
  7. vector<int> v[M];
  8. void dfs(int pre,int w){
  9. int flag=0;
  10. if(op[w]==1){
  11. if(f[x[w]][y[w]]^t[x[w]])ans[w]=ans[pre];
  12. else{
  13. f[x[w]][y[w]]^=1;
  14. if(f[x[w]][y[w]])s[x[w]]++,flag=1;else s[x[w]]--;
  15. ans[w]=ans[pre]+1;
  16. }
  17. }else if(op[w]==2){
  18. if(f[x[w]][y[w]]^t[x[w]]){
  19. f[x[w]][y[w]]^=1;
  20. if(f[x[w]][y[w]])s[x[w]]++,flag=1;else s[x[w]]--;
  21. ans[w]=ans[pre]-1;
  22. }else ans[w]=ans[pre];
  23. }else if(op[w]==3){
  24. if(t[x[w]]==0)ans[w]=ans[pre]-s[x[w]]+m-s[x[w]];
  25. else ans[w]=ans[pre]-(m-s[x[w]])+s[x[w]];
  26. t[x[w]]^=1;
  27. }
  28. for(int i=0;i<v[w].size();i++)dfs(w,v[w][i]);
  29. if(op[w]==1){
  30. if(ans[w]!=ans[pre]){
  31. f[x[w]][y[w]]^=1;
  32. if(flag)s[x[w]]--;else s[x[w]]++;
  33. }
  34. }else if(op[w]==2){
  35. if(ans[w]!=ans[pre]){
  36. f[x[w]][y[w]]^=1;
  37. if(flag)s[x[w]]--;else s[x[w]]++;
  38. }
  39. }else if(op[w]==3){
  40. t[x[w]]^=1;
  41. }
  42. }
  43. int main(){
  44. scanf("%d%d%d",&n,&m,&q);
  45. for(int i=1;i<=q;i++){
  46. scanf("%d",&op[i]);
  47. if(op[i]==1){
  48. scanf("%d%d",x+i,y+i);
  49. v[last].push_back(id[i]=i);
  50. last=i;
  51. }else if(op[i]==2){
  52. scanf("%d%d",x+i,y+i);
  53. v[last].push_back(id[i]=i);
  54. last=i;
  55. }else if(op[i]==3){
  56. scanf("%d",x+i);
  57. v[last].push_back(id[i]=i);
  58. last=i;
  59. }else{
  60. scanf("%d",&k);
  61. last=id[i]=id[k];
  62. }
  63. }dfs(0,0);
  64. for(int i=1;i<=q;i++)printf("%d\n",ans[id[i]]);
  65. return 0;
  66. }

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

    $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 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 #368 div2 D Persistent Bookcase DFS

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

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

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

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

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

随机推荐

  1. 自定义alert,confirm,prompt事件,模仿window.alert(),confirm(),prompt()

    css代码: /*custom_alert and custom_confirm*/ ; } ;;background-color: #585858; padding: 30px 30px; bord ...

  2. Stack的实现

    public class MyStack<AnyType> { private AnyType [] theItems; private final int DEFAULT_CAPACIT ...

  3. 运用Detours库hook API(原理是改写函数的头5个字节)

    一.Detours库的来历及下载: Detours库类似于WTL的来历,是由Galen Hunt and Doug Brubacher自己开发出来,于99年7月发表在一篇名为<Detours: ...

  4. Eclipse 快捷键操作和常用设置

    自动提示功能:一般的关键字,都有提示内容 像syso 就是System.out.println();for 循环 等 它的快捷键是 (Alt+/) 格式化代码: 有时候代码写多了 比较乱的话可以用快捷 ...

  5. Mirantis Fuel fundations

    Mirantis Nailgun is the most important service a RESTful application written in Python that contains ...

  6. 【LeetCode】4Sum 解题报告

    [题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d  ...

  7. windows编程之菜单操作

    分清几个概念 <1>"主菜单" 和 "顶层菜单" 是一个意思. <2>主菜单中的项目叫做 "弹出菜单" 或者 &qu ...

  8. 做ie8css样式时浏览器默认杂项模式遇到的一个小坑

    1 进行ie浏览器的样式兼容的时候,首先要确保打开浏览器浏览网页的时候的文本模式要为当前浏览器的"标准模式",注意<!DOCTYPE html>不缺失不错误,以免浏览器 ...

  9. SQL Server数据库---》基础

    SQL Server:只是操作数据库的一个工具(这种工具,只是提供一个界面化的方式让用户方便操作数据库) 开启服务:点击:我的电脑(计算机)--管理--服务和应用程序--服务--开启SQL Serve ...

  10. 获取xml文件

    <?xml version="1.0" encoding="utf-8" ?><ArrayOfSystemRool xmlns:xsi=&qu ...