【离线】【深搜】【树】Codeforces 707D Persistent Bookcase
题目链接:
http://codeforces.com/problemset/problem/707/D
题目大意:
一个N*M的书架,支持4种操作
1.把(x,y)变为有书。
2.把(x,y)变为没书。
3.把x行上的所有书状态改变,有变没,没变有。
4.回到第K个操作时的状态。
求每一次操作后书架上总共多少书。
题目思路:
【离线】【深搜】【树】
现场有思路不过没敢写哈。还是太弱了。
总共只用保存一张图,把操作看成一棵树,一开始I操作连接在I-1操作后,如果遇到操作4的话,把I操作与I-1操作的边断开,改为连接到K下。
这样把所有操作链完以后得到一棵多叉树,接下来深搜一遍,记录答案,回溯的时候把图的状态改回去即可。
- //
- //by coolxxx
- //#include<bits/stdc++.h>
- #include<iostream>
- #include<algorithm>
- #include<string>
- #include<iomanip>
- #include<map>
- #include<memory.h>
- #include<time.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- //#include<stdbool.h>
- #include<math.h>
- #define min(a,b) ((a)<(b)?(a):(b))
- #define max(a,b) ((a)>(b)?(a):(b))
- #define abs(a) ((a)>0?(a):(-(a)))
- #define lowbit(a) (a&(-a))
- #define sqr(a) ((a)*(a))
- #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
- #define mem(a,b) memset(a,b,sizeof(a))
- #define eps (1e-8)
- #define J 10
- #define mod 1000000007
- #define MAX 0x7f7f7f7f
- #define PI 3.14159265358979323
- #define N 1004
- #define M 100005
- using namespace std;
- typedef long long LL;
- int cas,cass;
- int n,m,lll,ans;
- struct xxx
- {
- int next,to,q,x,y;;
- }a[M];
- int last[M],an[M];
- bool mapp[N][N];
- void add(int x,int y)
- {
- a[++lll].next=last[x];
- a[lll].to=y;
- last[x]=lll;
- }
- void dfs(int now,int sum)
- {
- int i,j;
- an[now]=sum;
- for(i=last[now];i;i=a[i].next)
- {
- if(a[i].q==)
- {
- if(mapp[a[i].x][a[i].y])
- dfs(a[i].to,sum);
- else
- {
- mapp[a[i].x][a[i].y]=;
- dfs(a[i].to,sum+);
- mapp[a[i].x][a[i].y]=;
- }
- }
- else if(a[i].q==)
- {
- if(mapp[a[i].x][a[i].y])
- {
- mapp[a[i].x][a[i].y]=;
- dfs(a[i].to,sum-);
- mapp[a[i].x][a[i].y]=;
- }
- else dfs(a[i].to,sum);
- }
- else if(a[i].q==)
- {
- int k=;
- for(j=;j<=m;j++)
- {
- if(mapp[a[i].x][j])k--;
- else k++;
- mapp[a[i].x][j]^=;
- }
- dfs(a[i].to,sum+k);
- for(j=;j<=m;j++)mapp[a[i].x][j]^=;
- }
- else if(a[i].q==)
- dfs(a[i].to,sum);
- }
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- // freopen("1.txt","r",stdin);
- // freopen("2.txt","w",stdout);
- #endif
- int i,j,k;
- int x,y;
- // for(scanf("%d",&cas);cas;cas--)
- // for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
- // while(~scanf("%s",s+1))
- while(~scanf("%d",&n))
- {
- //lll=0;mem(fa,0);mem(last,0);
- scanf("%d%d",&m,&cas);
- for(i=;i<=cas;i++)
- {
- scanf("%d",&a[i].q);
- if(a[i].q<)
- scanf("%d%d",&a[i].x,&a[i].y);
- else if(a[i].q==)
- scanf("%d",&a[i].x);
- else if(a[i].q==)
- {
- scanf("%d",&a[i].x);
- add(a[i].x,i);
- continue;
- }
- add(i-,i);
- }
- dfs(,);
- for(i=;i<=cas;i++)
- printf("%d\n",an[i]);
- puts("");
- }
- return ;
- }
- /*
- //
- //
- */
【离线】【深搜】【树】Codeforces 707D Persistent Bookcase的更多相关文章
- Codeforces 707D Persistent Bookcase(时间树)
[题目链接] http://codeforces.com/problemset/problem/707/D [题目大意] 给出一个矩阵,要求满足如下操作,单个位置x|=1或者x&=0,一行的数 ...
- 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 Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力
D. Persistent Bookcase 题目连接: http://www.codeforces.com/contest/707/problem/D Description Recently in ...
- 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: ...
随机推荐
- PHP 开启报错机制
屏蔽PHP错误提示 方法一:在有可能出错的函数前加@,然后or die("") 如: @mysql_connect(...) or die("Database Conne ...
- IIS7.5 asp+access数据库连接失败处理 64位系统
IIS7.5 asp+access数据库连接失败处理(SRV 2008R2 x64/win7 x64) IIS7.5不支持oledb4.0驱动?把IIS运行模式设置成32位就可以了,微软没有支持出64 ...
- (转)修改ECSHOP前后台的title中的ecshop
前台部分: 1:去掉头部TITLE部分的ECSHOP演示站 Powered by ecshop 前者在后台商店设置 - 商店标题修改 后者打开includes/lib_main.php $page_t ...
- [转]mysql自动定时备份数据库的最佳方法-支持windows系统
网上有很多关于window下Mysql自动备份的方法,可是真的能用的也没有几个,有些说的还非常的复杂,难以操作. 我们都知道mssql本身就自带了计划任务可以用来自动备份,可是mysql咱们要怎么样自 ...
- 分享一个nodejs写的小论坛
引言:作为一个前端小菜鸟,最近迷上了node,于是乎空闲时间,为了练练手写了一个node的小社区,关于微信小程序的,欢迎大家批评指导. 项目架构部分 一.前端架构 作为一个写样式也得无聊的前端狮,我偷 ...
- SGU 163.Wise King
一道题目长的水题.... 总结就一句话,给出n个(-3~3)的数,一个数m,取任意个数是使这些数的m次幂之和最大. code #include <iostream> #include &l ...
- jQuery慢慢啃之事件(七)
1.ready(fn)//当DOM载入就绪可以查询及操纵时绑定一个要执行的函数. $(document).ready(function(){ // 在这里写你的代码...}); 使用 $(docume ...
- linux 获取cpu百分比
vmstat 1 |head -n 4 |tail -n 1 |awk '{print $13}'
- PHP 开启 ssh2
首先,为PHP安装SSH2扩展需要两个软件包, libssh2和ssh2(php pecl拓展). 两者的最新版本分别为libssh2-1.5.0.tar.gz和ssh2-0.12.tgz,下载地址分 ...
- Python新手学习基础之初识python——与众不同2
看完了Python的缩进,现在来看看Python的标识符.引号和注释. 标识符 关于Python的标识符,其实不是与众不同,只是有一定的规则. 标识符是编程时使用的名字.在Python中,标识符有几点 ...