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 ...
随机推荐
- Jquery中bind和live.one,delegate的区别
Jquery中绑定事件有三种方法:以click事件为例 (1)target.click(function(){}); (2)target.bind("click",function ...
- php调用java类文件
最近在折腾php调用java类,网上查阅资料,最终选用JavaBridge,遇到的第一个问题是java_require() 函数引入自定义java打包而成的jar文件,在新版本的JavaBridge中 ...
- chroot
用途:更改命令的根目录. 语法:chroot Directory Command 描述: 注意:如果新根目录中的特殊文件具有与实际根目录不同的主要和次要设备号,则可能会覆盖文件系统. 只有具有 roo ...
- 目前常用AD/DA芯片简介
目前生产AD/DA的主要厂家有ADI.TI.BB.PHILIP.MOTOROLA等,武汉力源公司拥有多年从事电子产品的经验和雄厚的技术力量支持,已取得排名世界前列的模拟IC生产厂家ADI.TI公司代理 ...
- VC维度
由vc bound可以知道: $P(\exists h\in H~s.t~|E_{in}(h)-E_{out}(h)|>\epsilon)\\ \leq 4M_H(2N)exp(-\frac{ ...
- #include <sstream>
1 std::istringstream 2 std::stringstream 1 std::istringstream input 1 在一个字符串string里提取部分数据,这些数据以空格' ' ...
- MySQL- 锁机制及MyISAM表锁
锁是计算机协调多个进程或线程并发访问某一资源的机制.在数据库中,除传统的计算资源(如CPU.RAM.I/O等)的争用以外,数据也是一种供许 多用户 共享的资源.如何保证数据并发访问的一致性.有效性是所 ...
- leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically ne ...
- EF5 通用数据层 增删改查操作,泛型类(转)
using System; using System.Collections.Generic; using System.Data.Entity.Infrastructure; using Syste ...
- poj1487
题目大意: 给一棵递归树,看链接图片,从根节点开始对于每个节点往它的子节点移动,直到叶子节点停止.每个节点选哪一个孩子节点继续往下走是随机的(等概率).然后叶子节点都会标记一个数值,记为走到该节点的得 ...