【题目链接】 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(时间树)的更多相关文章

  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

    $dfs$,优化. $return$操作说明该操作完成之后的状态和经过操作$k$之后的状态是一样的.因此我们可以建树,然后从根节点开始$dfs$一次(回溯的时候复原一下状态)就可以算出所有状态的答案. ...

  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 #368 div2 D Persistent Bookcase DFS

    题目链接:D Persistent Bookcase 题意:有一个n*m的书架,开始是空的,现在有k种操作: 1 x y 这个位置如果没书,放书. 2 x y 这个位置如果有书,拿走. 3 x 反转这 ...

  8. 【Codeforces-707D】Persistent Bookcase DFS + 线段树

    D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...

  9. Codeforces Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力

    D. Persistent Bookcase 题目连接: http://www.codeforces.com/contest/707/problem/D Description Recently in ...

随机推荐

  1. Django模板-分离的模板

    上一篇Django模板-在视图中使用模板最后的问题,我们需要把数据和展现分离开. 你可能首先考虑把模板保存在文件系统的某个位置并用 Python 内建的文件操作函数来读取文件内容. 假设文件保存在 E ...

  2. web前端开发常用工具

    http://www.gbin1.com/technology/javautilities/20120806-resource-for-front-end-developer/ 冒泡样式 http:/ ...

  3. mysql排行榜sql的实现

    SELECT num_rows AS num_rows, openid , openid, money FROM lt_cash_user ) t ORDER BY money DESC ) t1 W ...

  4. #include <windows.h>

      1 FindWindowA 2 keybd_event 3 malloc 4 MessageBox 5 MessageBoxA 6 MessageBoxW 7 mouse_event 8 SetC ...

  5. php解决与处理网站高并发 大流量访问的方法

    方法/步骤   首先,确认服务器硬件是否足够支持当前的流量 普通的P4服务器一般最多能支持每天10万独立IP,如果访问量比这个还要大, 那么必须首先配置一台更高性能的专用服务器才能解决问题 ,否则怎么 ...

  6. Android上下左右滑动,显示底层布局

    转载博客地址:http://www.cnblogs.com/flyme2012/p/4106308.html 闲着没事做了一个小东西.Android的上下左右滑动,显示底层布局.可以做类似于QQ消息列 ...

  7. Sizzle一步步实现所有功能(一)

    前提: 1.HTML5自带querySelectAll可以完全替代Sizlle,所以我们下面写的Sizzle,是不考虑QSA的. 2.作者考虑了大量兼容情况,比如黑莓4.6系统这样几乎接触不到的bug ...

  8. SSIS之Foreach循环容器用法

    要实现的业务:A数据库服务器上某库的T_GOODS_DECL的状态字段“Is_Delete”标记为“1”的时候删除B数据库服务器上对应库的T_GOODS_DECL表中的记录,二者的主键为“DECL_N ...

  9. Oracle EBS DBA常用SQL - 安装/补丁【Z】

    Oracle EBS DBA常用SQL - 安装/补丁 检查应用补丁有没有安装:select bug_number,last_update_date from ad_bugs where bug_nu ...

  10. 场景:A-->B-->C 跳转到C时,要关掉B的处理方法

    场景:A-->B-->C 跳转到C时,要关掉B的处理方法:相当于从A跳转到C UIViewController *preController = [self.navigationContr ...