$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)$效率进行状态改变。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
} struct X
{
int t,r,c,k,id;
bool f;
}s[];
int sz; int h[],r[],a[][];
int ans,n,m,k;
vector<int>G[];
int Ans[]; void work1(int op,int tag)
{
if(tag==)
{
if((a[s[op].r][s[op].c]+r[s[op].r])%==) return;
s[op].f=; a[s[op].r][s[op].c]++; h[s[op].r]++; ans++;
} else
{
if(s[op].f==) return ;
a[s[op].r][s[op].c]--; h[s[op].r]--; ans--; s[op].f=;
}
} void work2(int op,int tag)
{
if(tag==)
{
if((a[s[op].r][s[op].c]+r[s[op].r])%==) return;
s[op].f=; a[s[op].r][s[op].c]--; h[s[op].r]--; ans--;
} else
{
if(s[op].f==) return;
a[s[op].r][s[op].c]++; h[s[op].r]++; ans++; s[op].f=;
}
} void work3(int op,int tag)
{
if(tag==)
{
s[op].f=;
ans=ans-h[s[op].r]+(m-h[s[op].r]);
h[s[op].r]=m-h[s[op].r];
r[s[op].r]++;
} else
{
ans=ans-h[s[op].r]+(m-h[s[op].r]);
h[s[op].r]=m-h[s[op].r];
r[s[op].r]--; s[op].f=;
}
} void dfs(int x)
{
if(s[x].t==) work1(x,);
else if(s[x].t==) work2(x,);
else if(s[x].t==) work3(x,); for(int i=;i<G[x].size();i++)
dfs(G[x][i]);
Ans[x]=ans;
if(s[x].t==) work1(x,);
else if(s[x].t==) work2(x,);
else if(s[x].t==) work3(x,);
} int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=k;i++)
{
scanf("%d",&s[i].t); int from=i-;
if(s[i].t==) scanf("%d%d",&s[i].r,&s[i].c);
else if(s[i].t==) scanf("%d%d",&s[i].r,&s[i].c);
else if(s[i].t==) scanf("%d",&s[i].r);
else scanf("%d",&from);
G[from].push_back(i);
}
s[].t=; dfs();
for(int i=;i<=k;i++) printf("%d\n",Ans[i]);
return ;
}

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. DOM事件简介

    DOM事件简介--摘自ADMIN10000 Posted on 2013-12-05 09:32 ziran 阅读(76) 评论(1) 编辑 收藏 Click.touch.load.drag.chan ...

  2. vijos1060 隔板法

    排列组合问题 之前没有学过隔板法,随便学习了一下 其实挺好理解的 附上题解: 先只考虑一种球:因为有n个盒子每个盒子可以放任意多球,还可以空出来任意多球.所以可以考虑为n+1个盒子,最后一个盒子里面是 ...

  3. 长度为n的数组,有一个数重复出现了n/2+1次,找出(三种方法)

    问题: 长度为n的数组,有一个数重复出现了n/2+1次,找出这个数:   解决: 比较直接的思路是遍历每个元素,让其与剩下其他元素比较,相等一次计数器sum++,直到sum=n/2+1为止: #inc ...

  4. 关于Jquery 序列化表单的注意事项

    在JQuery 的serialize方法序列化表单的过程中,如果表单的name值最后有空格,会出现“+”号,查源码可见原因.这一小问题就浪费了半小时的时间,记录下来,备忘.

  5. CSS3 3D笨蛋教程

    英文原文An Introduction to CSS 3-D Transforms 爱因斯坦说所有概念都必须介绍给儿童们,若他们无法了解,这些理论就毫无价值. 透视 一个元素需要一个透视点才能激活3D ...

  6. 推荐一些C#相关的网站、资源和书籍 (转载自http://www.cnblogs.com/jiangxiaofan/p/3808316.html)

    推荐一些C#相关的网站.资源和书籍 (转载自http://blog.csdn.net/chinacsharper/article/details/17514923)   一.网站 1.http://m ...

  7. Linux C编程一站式学习读书笔记——socket编程

    前言 研一的时候写过socket网络编程,研二这一年已经在用php写api都快把之前的基础知识忘干净了,这里回顾一下,主要也是项目里用到了,最近博客好杂乱啊,不过确实是到了关键时刻,各种复习加巩固准备 ...

  8. 关于props和state以及redux中的state

    React的数据模型分为共有数据和私有数据,共有数据可以在组件间进行传递,私有数据为当前组件私有.共有数据在React中使用props对象来调用,它包含标签所有的属性名称和属性值,props对象有三个 ...

  9. Java的Exception和Error面试题10问10答

    在Java核心知识的面试中,你总能碰到关于 处理Exception和Error的面试题.Exception处理是Java应用开发中一个非常重要的方面,也是编写强健而稳定的Java程序的关键,这自然使它 ...

  10. 配置 Ionic环境

    本博客只适合win7 系统 准备  必须要有 注意了!!!! //注意所以路径均不支持中文 1.安装JSK (jsva环境) 默认安装位置 如果是32位操作系统  需要安装32bit版的JDK C:\ ...