$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. ROWNUMBER()、RANK()、DENSE_RANK()、NTILE1

    SQLServer针对排名函数ROWNUMBER().RANK().DENSE_RANK().NTILE的研究!~ 相信大家在软件工程中经常会遇到对某些数据进行排名的问题,尤其是对于电子商务的HR来说 ...

  2. 历年noip复赛试题整合

    早晨打算把历年的试题都过一遍,整理一下大概会往哪个方向考,考什么,不说太多,开始吧 2013: Day1: T1 转圈游戏 : 快速幂(关键在于要会打 快速幂) 思路:因为每次都进m位,相当于每次x加 ...

  3. jquery水印插件:placeholder

    jquery水印插件:placeholder 有的浏览器支持html5的水印placeholder(如Crome,firefox,ie10+),有的不支持html5的placeholder(ie9,i ...

  4. UAC权限

    .NET中提升UAC权限的方法总结   [题外话] 从Vista开始,由于增加了UAC(用户账户控制,User Account Control)功能,使得管理员用户平时不再拥有能控制所有功能的管理员权 ...

  5. IOS学习之路十(仿人人滑动菜单Slide-out Sidebar Menu)

    最近滑动菜单比较流行,像facebook和人人等都在使用滑动菜单,今天做了一个小demo大体效果如下: 这次用了一个开源的项目ECSlidingViewController这个也是一个挺著名的托管在G ...

  6. 论公司spring的滥用

    这个公司每个项目用不同的一套开发框架,实在忍不住拿一个出来说说事.

  7. SystemTray文字颜色问题

    今天想给SystemTray的ForegroundColor设置为白色,但是模拟器正确,真机仍为黑色.经过一番折腾,发现是微软做了限制,背景是什么颜色,ForegroundColor就不能为什么颜色. ...

  8. MVC从视图传参到Controller几种方式

    简单数组传递 var array = ["aaa", "bbb", "ccc"]; $.ajax({ url:"@Url.Acti ...

  9. CentOS下Eclipse的安装教程

    CentOS下Eclipse的安装教程 据了解,在Linux下的Java开发很多时候都比较喜欢使用vim + 插件,反而很少使用Eclipse,但是我是第一次使用Linux来进行Java编程,就什么都 ...

  10. 解密:LL与LR解析 1(译)

    解密:LL与LR解析 1 作者:Josh Haberman 翻译:杨贵福 由于GFW,我无法联系到作者,所以没有授权,瞎翻译的.原文在这里[http://blog.reverberate.org/20 ...