题目链接:

  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的更多相关文章

  1. Codeforces 707D Persistent Bookcase(时间树)

    [题目链接] http://codeforces.com/problemset/problem/707/D [题目大意] 给出一个矩阵,要求满足如下操作,单个位置x|=1或者x&=0,一行的数 ...

  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 Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力

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

  6. codeforces 707D D. Persistent Bookcase(dfs)

    题目链接: D. Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input ...

  7. Codeforces Round #368 (Div. 2) D. Persistent Bookcase

    Persistent Bookcase Problem Description: Recently in school Alina has learned what are the persisten ...

  8. CodeForces #368 div2 D Persistent Bookcase DFS

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

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

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

随机推荐

  1. oracle在linux配置信息

    这两天在linux中给已有的oracle添加新实例,其中涉及数据库服务.监听配置,oracle服务是否正常.监听是否成功等操作,特此记录存档,以备后用. oracle服务启动操作命令 1.查看orac ...

  2. order by 自定义排序

    使用order by排序,有时候不是根据字符或数字顺序,而是根据实际要求排序. 例如有客户A,B,C,我希望排序结果是B,C,A,那么就要通过自定义的规则排序. 第一种方法,可以构造一张映射表,将客户 ...

  3. widows sever2003 PHP环境搭建

    此文仅为文字笔记,非原创,摘阅自互联网  1.安装IIS6.0及Framework 2.0  2.安装fastcgi http://www.iis.net/download/FastCGI 安装fas ...

  4. CakePHP的文章分类的功能实现

    前些天实现了[微个人.大家园]的文章文类功能.现在回忆一下,是如何完成的吧. 具体的操作步骤如下: 1.在文章posts表里添加一个列,category_id. 2.在数据库中添加一个数据表,cate ...

  5. 多线程 - 线程同步锁(lock、Monitor)

    1. 前言 多线程编程的时候,我们不光希望两个线程间能够实现逻辑上的先后顺序运行,还希望两个不相关的线程在访问同一个资源的时候,同时只能有一个线程对资源进行操作,否则就会出现无法预知的结果. 比如,有 ...

  6. iOS菜鸟之FMDB的二次封装简单易用

    闲来无事写点东西,希望大家多多指正! 大家先去git下载FMDB,然后将其中source文件夹中的fmdb文件夹拖入自己的项目中.最后就可以引用下面的代码对fmdb进行一次简单的封装. 这样可以更直观 ...

  7. asp.net尽量不在js里写<%%>

    asp.net尽量不在js里写<%%> eg: <script type="text/javascript"> var rootsid="&quo ...

  8. WebDriverWait 中 and, or, not用法

    1. And 用法 wait.until(ExpectedConditions.and( ExpectedConditions.visibilityOfAllElementsLocatedBy(By. ...

  9. jQuery 元素移除empty() remove()与detach()的区别?

    @1.empty() 删除匹配元素集合中所有的后代字节点元素: <p>hello<span>world</span></p> $("p&quo ...

  10. PHP学习之中数组--创建数组【1】

    在PHP中数组的定义有三种写法,分别是: <?php //第一种方式是用关键字 array来创建的 $username = array("demo1","demo2 ...