Codeforces 707D Persistent Bookcase(时间树)
【题目链接】 http://codeforces.com/problemset/problem/707/D
【题目大意】
给出一个矩阵,要求满足如下操作,单个位置x|=1或者x&=0,一行的数全部取反,回到第k个操作。要求每次操作后输出这个矩阵中数字的和。
【题解】
由于存在操作回溯,考虑使用可持久化数据结构或者建立离线操作树。因为懒,没写持久化。对于每个操作,将它和时间顺序上的上一个节点连边,这样子就形成了一棵树,对这棵树从根节点开始遍历,递归处理,每次处理完一棵子树就回溯操作,这样子就能离线处理操作的持久化了。
【代码】
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int N=1005,M=100005;
int k,q,n,m,t[N],f[N][N],s[N],last=0,ans[M],op[M],x[M],y[M],id[M];
vector<int> v[M];
void dfs(int pre,int w){
int flag=0;
if(op[w]==1){
if(f[x[w]][y[w]]^t[x[w]])ans[w]=ans[pre];
else{
f[x[w]][y[w]]^=1;
if(f[x[w]][y[w]])s[x[w]]++,flag=1;else s[x[w]]--;
ans[w]=ans[pre]+1;
}
}else if(op[w]==2){
if(f[x[w]][y[w]]^t[x[w]]){
f[x[w]][y[w]]^=1;
if(f[x[w]][y[w]])s[x[w]]++,flag=1;else s[x[w]]--;
ans[w]=ans[pre]-1;
}else ans[w]=ans[pre];
}else if(op[w]==3){
if(t[x[w]]==0)ans[w]=ans[pre]-s[x[w]]+m-s[x[w]];
else ans[w]=ans[pre]-(m-s[x[w]])+s[x[w]];
t[x[w]]^=1;
}
for(int i=0;i<v[w].size();i++)dfs(w,v[w][i]);
if(op[w]==1){
if(ans[w]!=ans[pre]){
f[x[w]][y[w]]^=1;
if(flag)s[x[w]]--;else s[x[w]]++;
}
}else if(op[w]==2){
if(ans[w]!=ans[pre]){
f[x[w]][y[w]]^=1;
if(flag)s[x[w]]--;else s[x[w]]++;
}
}else if(op[w]==3){
t[x[w]]^=1;
}
}
int main(){
scanf("%d%d%d",&n,&m,&q);
for(int i=1;i<=q;i++){
scanf("%d",&op[i]);
if(op[i]==1){
scanf("%d%d",x+i,y+i);
v[last].push_back(id[i]=i);
last=i;
}else if(op[i]==2){
scanf("%d%d",x+i,y+i);
v[last].push_back(id[i]=i);
last=i;
}else if(op[i]==3){
scanf("%d",x+i);
v[last].push_back(id[i]=i);
last=i;
}else{
scanf("%d",&k);
last=id[i]=id[k];
}
}dfs(0,0);
for(int i=1;i<=q;i++)printf("%d\n",ans[id[i]]);
return 0;
}
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
$dfs$,优化. $return$操作说明该操作完成之后的状态和经过操作$k$之后的状态是一样的.因此我们可以建树,然后从根节点开始$dfs$一次(回溯的时候复原一下状态)就可以算出所有状态的答案. ...
- 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 #368 div2 D Persistent Bookcase DFS
题目链接:D Persistent Bookcase 题意:有一个n*m的书架,开始是空的,现在有k种操作: 1 x y 这个位置如果没书,放书. 2 x y 这个位置如果有书,拿走. 3 x 反转这 ...
- 【Codeforces-707D】Persistent Bookcase DFS + 线段树
D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力
D. Persistent Bookcase 题目连接: http://www.codeforces.com/contest/707/problem/D Description Recently in ...
随机推荐
- leetcode算法刷题(三)
今天在刷了几道简单的动态规划后,又看了看string方面的题 第五题 Longest Palindromic Substring 题目的意思:求一个字符串的最长回文子串 分析:开始,我的想法是,现在字 ...
- Delphi获取当前系统时间(使用API函数GetSystemTime)
在开发应用程序时往往需要获取当前系统时间.尽管Y2K似乎已经平安过去,但在我们新开发的应用程序中还是要谨慎处理“时间”问题. 在<融会贯通--Delphi4.0实战技巧>(以下简称“该书” ...
- docker 保存更改的镜像:
<pre name="code" class="ruby">保存更改的镜像: docker:/root# docker commit -m &quo ...
- iOS中如何呼出另一个应用
我们经常会遇到在一个应用里面呼出另一个应用的需求,比如在文档里面点击地址,调用safari来打开网页:比如在文件浏览器里面点击某种文件,自动激活一个应用来打开文件. iOS里面对于这样的需求使用URL ...
- click through rate prediction
包括内容如下图: 使用直接估计法,置信区间置信率的估计: 1.使用二项分布直接估计 $p(0.04<\hat{p}<0.06) = \sum_{0.04n\leq k \leq 0.06n ...
- heap creation
There two methods to construct a heap from a unordered set of array. If a array has size n, it can b ...
- JAVA之数组查询binarySearch()方法详解
binarySearch()方法提供了多种重载形式,用于满足各种类型数组的查找需要,binarySearch()有两种参数类型 注:此法为二分搜索法,故查询前需要用sort()方法将数组排序,如果数组 ...
- Struts2 对Action中所有方法进行输入校验、单个方法进行校验
index.jsp: <body> <s:fielderror /> <form action="${pageContext.request.contextPa ...
- iOS开发 使用Xcode自带的Leaks
http://www.jianshu.com/p/0837331875f0 http://www.cnblogs.com/tanzhenblog/p/5001344.html?utm_source=t ...
- JavaScript-1.最简单的程序之网页弹出对话框,显示为Warning---ShinePans
代码: <html> <head> <meta http-equiv="content-type" content="text/html;c ...