CodeForces 707D Persistent Bookcase
$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的更多相关文章
- 【离线】【深搜】【树】Codeforces 707D Persistent Bookcase
题目链接: http://codeforces.com/problemset/problem/707/D 题目大意: 一个N*M的书架,支持4种操作 1.把(x,y)变为有书. 2.把(x,y)变为没 ...
- Codeforces 707D Persistent Bookcase(时间树)
[题目链接] http://codeforces.com/problemset/problem/707/D [题目大意] 给出一个矩阵,要求满足如下操作,单个位置x|=1或者x&=0,一行的数 ...
- CodeForces 707D Persistent Bookcase ——(巧妙的dfs)
一个n*m的矩阵,有四种操作: 1.(i,j)处变1: 2.(i,j)处变0: 3.第i行的所有位置1,0反转: 4.回到第k次操作以后的状态: 问每次操作以后整个矩阵里面有多少个1. 其实不好处理的 ...
- Persistent Bookcase CodeForces - 707D (dfs 离线处理有根树模型的问题&&Bitset)
Persistent Bookcase CodeForces - 707D time limit per test 2 seconds memory limit per test 512 megaby ...
- codeforces 707D D. Persistent Bookcase(dfs)
题目链接: D. Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input ...
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase
Persistent Bookcase Problem Description: Recently in school Alina has learned what are the persisten ...
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力
D. Persistent Bookcase 题目连接: http://www.codeforces.com/contest/707/problem/D Description Recently in ...
- CodeForces #368 div2 D Persistent Bookcase DFS
题目链接:D Persistent Bookcase 题意:有一个n*m的书架,开始是空的,现在有k种操作: 1 x y 这个位置如果没书,放书. 2 x y 这个位置如果有书,拿走. 3 x 反转这 ...
- 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 ...
随机推荐
- DOM事件简介
DOM事件简介--摘自ADMIN10000 Posted on 2013-12-05 09:32 ziran 阅读(76) 评论(1) 编辑 收藏 Click.touch.load.drag.chan ...
- SPFA和FLOYD算法如何打印路径
早晨碰到了一题挺裸的最短路问题需要打印路径:vijos1635 1.首先说说spfa的方法: 其实自己之前打的最多的spfa是在网格上的那种,也就是二维的 一维的需要邻接表+queue 以及对于que ...
- D10
=-=今天被dev-c++坑到死..简直 晚上准备怒装liunx.. T1:数论 一开始碰到的是T1的运算符优先问题吧..maybe..但是我加上括号了还是WA啊..后面把式子拆开写才A了..次奥 附 ...
- Event对象的事件句柄
<html> <!-- onresize 事件会在窗口或框架被调整大小时发生 --> <!--onresize="alert('窗口的大小得到变化就会执行我') ...
- 上传组件uploadify的使用
上传组件uploadify的使用 大概一年前,我还在用Asp.NET开发一些行业管理系统的时候,就曾经使用这个组件作为文件的上传操作,在随笔<Web开发中的文件上传组件uploadify的使用& ...
- 程序处理数据库中值字段值为null的查询显示
1.如果你做了一个简单的注册界面,需要用户进行注册,但有些项是不必要填的,当用户完成注册时,数据库表中的相应字段的值会写入null,但如何将查询的字段的值null显示出来? 2.首先我们学习一下如何向 ...
- Struts2框架搭建
使用Struts2版本为:struts-2.3.16.3 下载地址:http://struts.apache.org/ 一.向web项目中添加struts2依赖库 commons-fileupload ...
- Use weechat (IRC client) on OS X. MacBook Pro
Weechat is a console IRC client. It is opensource and very easy to use. I use weechat in my Linux PC ...
- 那天有个小孩教我WCF[一][1/3]
那天有个小孩教我WCF[一][1/3] 既然是小孩系列,当然要有一点基础才能快速掌握,归纳,总结的一个系列,哈哈 前言: 第一篇嘛,不细讲,步步教你创建一个简单SOA案例,对WCF有个基本的认识,我不 ...
- poj1483 It's not a Bug, It's a Feature!
It's not a Bug, It's a Feature! Time Limit: 5000MS Memory Limit: 30000K Total Submissions: 1231 ...