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 ...
随机推荐
- 自定义alert,confirm,prompt事件,模仿window.alert(),confirm(),prompt()
css代码: /*custom_alert and custom_confirm*/ ; } ;;background-color: #585858; padding: 30px 30px; bord ...
- Stack的实现
public class MyStack<AnyType> { private AnyType [] theItems; private final int DEFAULT_CAPACIT ...
- 运用Detours库hook API(原理是改写函数的头5个字节)
一.Detours库的来历及下载: Detours库类似于WTL的来历,是由Galen Hunt and Doug Brubacher自己开发出来,于99年7月发表在一篇名为<Detours: ...
- Eclipse 快捷键操作和常用设置
自动提示功能:一般的关键字,都有提示内容 像syso 就是System.out.println();for 循环 等 它的快捷键是 (Alt+/) 格式化代码: 有时候代码写多了 比较乱的话可以用快捷 ...
- Mirantis Fuel fundations
Mirantis Nailgun is the most important service a RESTful application written in Python that contains ...
- 【LeetCode】4Sum 解题报告
[题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
- windows编程之菜单操作
分清几个概念 <1>"主菜单" 和 "顶层菜单" 是一个意思. <2>主菜单中的项目叫做 "弹出菜单" 或者 &qu ...
- 做ie8css样式时浏览器默认杂项模式遇到的一个小坑
1 进行ie浏览器的样式兼容的时候,首先要确保打开浏览器浏览网页的时候的文本模式要为当前浏览器的"标准模式",注意<!DOCTYPE html>不缺失不错误,以免浏览器 ...
- SQL Server数据库---》基础
SQL Server:只是操作数据库的一个工具(这种工具,只是提供一个界面化的方式让用户方便操作数据库) 开启服务:点击:我的电脑(计算机)--管理--服务和应用程序--服务--开启SQL Serve ...
- 获取xml文件
<?xml version="1.0" encoding="utf-8" ?><ArrayOfSystemRool xmlns:xsi=&qu ...