【离线】【深搜】【树】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: ...
随机推荐
- 如何消除inline-block产生的元素间空隙
前端初学者可能都会碰到这个问题:有时候排版需要,会把一些块状元素的display属性设置为inline-block,如 <!-- HTML代码 --> <div class=&quo ...
- DataTbale取值
有一个DataTable数据 //创建DataTable对象 DataTable dt = new DataTable("Table_AX"); //为DataTable创建列 / ...
- object标签参考(转载)
<object> 元素可支持多种不同的媒介类型,比如: 图片 音频 视频 Other 对象 显示图片 你可以显示一幅图片: <object height="100%&quo ...
- angularjs 将带标签的内容解析后返回
参考地址:http://okashii.lofter.com/post/1cba87e8_29e0fab 引入angular-sanitize.js文件 注入ngSanitize 页面数据绑定 ng- ...
- JS & JQuery 动态添加 select option
因为是转载文章 在此标明出处,以前有文章是转的没标明的请谅解,因为有些已经无法找到出处,或者与其它原因. 如有冒犯请联系本人,或删除,或标明出处. 因为好的文章,以前只想收藏,但连接有时候会失效,所以 ...
- java把InputStram 转换为String
public static String readStream(InputStream in) throws Exception{ //定义一个内存输出流 ByteArrayOutputStream ...
- java——输入流FileInputStream
写一个简单的程序,实现从电脑中的一个盘里的文件中往程序中输入内容. package com.liaojianya.chapter5; import java.io.FileInputStream; i ...
- Linq查询IEnumerable与IQueryable
class Program { static void Main(string[] args) { System.Diagnostics.Stopwatch stp = new Stopwatch() ...
- 第三篇、调优之路 Apache调优
1. 简介 在第一篇中整合了apache + tomcat ,利用了apache解析静态文件为tomcat解压.但是在测试机上发现两者性能不足,不能充分利用服务器的性能,该篇中将对apache进行性 ...
- topcoder算法练习2
Problem Statement In most states, gamblers can choose from a wide variety of different lottery ...