codeforces 707D:Persistent Bookcase
Description
Recently in school Alina has learned what are the persistent data structures: they are data structures that always preserves the previous version of itself and access to it when it is modified.
After reaching home Alina decided to invent her own persistent data structure. Inventing didn't take long: there is a bookcase right behind her bed. Alina thinks that the bookcase is a good choice for a persistent data structure. Initially the bookcase is empty, thus there is no book at any position at any shelf.
The bookcase consists of n shelves, and each shelf has exactly m positions for books at it. Alina enumerates shelves by integers from 1 to n and positions at shelves — from 1 to m. Initially the bookcase is empty, thus there is no book at any position at any shelf in it.
Alina wrote down q operations, which will be consecutively applied to the bookcase. Each of the operations has one of four types:
- 1 i j — Place a book at position j at shelf i if there is no book at it.
- 2 i j — Remove the book from position j at shelf i if there is a book at it.
- 3 i — Invert book placing at shelf i. This means that from every position at shelf i which has a book at it, the book should be removed, and at every position at shelf i which has not book at it, a book should be placed.
- 4 k — Return the books in the bookcase in a state they were after applying k-th operation. In particular, k = 0 means that the bookcase should be in initial state, thus every book in the bookcase should be removed from its position.
After applying each of operation Alina is interested in the number of books in the bookcase. Alina got 'A' in the school and had no problem finding this values. Will you do so?
The first line of the input contains three integers n, m and q (1 ≤ n, m ≤ 103, 1 ≤ q ≤ 105) — the bookcase dimensions and the number of operations respectively.
The next q lines describes operations in chronological order — i-th of them describes i-th operation in one of the four formats described in the statement.
It is guaranteed that shelf indices and position indices are correct, and in each of fourth-type operation the number k corresponds to some operation before it or equals to 0.
For each operation, print the number of books in the bookcase after applying it in a separate line. The answers should be printed in chronological order.
2 3 3
1 1 1
3 2
4 0
1
4
0
4 2 6
3 2
2 2 2
3 3
3 2
2 2 2
3 2
2
1
3
3
2
4
2 2 2
3 2
2 2 1
2
1 正解:搜索+操作树
解题报告:
这道题误导我思考可持久化数据结构。。。
先把操作离线,然后考虑当前操作由哪一步转过来。若是要完成第4个操作,我们需要保存下每步操作之后的全局局面,空间不够,不妨时间换空间。
考虑我们每步操作都走向他能走向的操作(应对return操作),然后修改状态,得出当前答案,dfs下去,只需要在dfs完之后回溯,减掉这次操作的贡献就可以了。
这种做法第一次接触,还是很神的。
//It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int MAXQ = ;
const int MAXN = ;
const int MAXM = ;
int n,m,q,now,ecnt;
int next[MAXM],first[MAXN],to[MAXM],f[MAXN];
int tag[],a[][];
int ans[MAXQ];
int cnt[];
int all_cnt;//全局
struct wen{
int type;
int x,y;
}Q[MAXQ]; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline void link(int x,int y){next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y;} inline void dfs(int k){
bool flag=false;//是否做过修改
int tp=Q[k].type,x=Q[k].x,y=Q[k].y;
if(tp==){ if(!(a[x][y]^tag[x])) cnt[x]++,all_cnt++,a[x][y]^=,flag=true; }
else if(tp==){ if(a[x][y]^tag[x]) cnt[x]--,all_cnt--,a[x][y]^=,flag=true; }
else if(tp==) flag=true,all_cnt-=cnt[x],tag[x]^=,cnt[x]=m-cnt[x],all_cnt+=cnt[x]; ans[k]=all_cnt;
for(int i=first[k];i;i=next[i]) dfs(to[i]);
if(!flag) return ;
if(tp==) cnt[x]--,all_cnt--,a[x][y]^=;
else if(tp==) cnt[x]++,all_cnt++,a[x][y]^=;
else if(tp==) all_cnt-=cnt[x],tag[x]^=,cnt[x]=m-cnt[x],all_cnt+=cnt[x];
} inline void work(){
n=getint(); m=getint(); q=getint();
now=;//为操作连边
for(int i=;i<=q;i++) {
Q[i].type=getint();
if(Q[i].type!=) link(now,i); f[i]=now=i;
if(Q[i].type==) Q[i].x=getint(),Q[i].y=getint();
else if(Q[i].type==) Q[i].x=getint(),Q[i].y=getint();
else if(Q[i].type==) Q[i].x=getint();
else Q[i].x=getint(),Q[i].x=f[Q[i].x],f[i]=now=Q[i].x;//返回到k次操作之前的那次之后
}
dfs();
for(int i=;i<=q;i++) printf("%d\n",ans[f[i]]);
} int main()
{
work();
return ;
}
codeforces 707D:Persistent Bookcase的更多相关文章
- codeforces 707D D. Persistent Bookcase(dfs)
题目链接: D. Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input ...
- 【21.28%】【codeforces 707D】Persistent Bookcase
time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- Codeforces-707D:Persistent Bookcase (离线处理特殊的可持久化问题&&Bitset)
Recently in school Alina has learned what are the persistent data structures: they are data structur ...
- 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
Persistent Bookcase Problem Description: Recently in school Alina has learned what are the persisten ...
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力
D. Persistent Bookcase 题目连接: http://www.codeforces.com/contest/707/problem/D Description Recently in ...
- CodeForces #368 div2 D Persistent Bookcase DFS
题目链接:D Persistent Bookcase 题意:有一个n*m的书架,开始是空的,现在有k种操作: 1 x y 这个位置如果没书,放书. 2 x y 这个位置如果有书,拿走. 3 x 反转这 ...
- D. Persistent Bookcase(Codeforces Round #368 (Div. 2))
D. Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input stand ...
- 【Codeforces-707D】Persistent Bookcase DFS + 线段树
D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...
随机推荐
- js识别不同浏览器
检測浏览器.注意浏览器推断顺序,主要是基于userAgent做推断. //检測浏览器 var client = function(){ var engine = { ie:0, ...
- hdu 3367 Pseudoforest(并查集)
题意:有一种叫作Pseudoforest的结构,表示在无向图上,每一个块中选取至多包含一个环的边的集合,又称“伪森林”.问这个集合中的所有边权之和最大是多少? 分析:如果没有环,那么构造的就是最大生成 ...
- 机器学习2—K近邻算法学习笔记
Python3.6.3下修改代码中def classify0(inX,dataSet,labels,k)函数的classCount.iteritems()为classCount.items(),另外p ...
- window mysql安装步骤
window安装mysql(本人系统win10 64位 安装mysql-5.7.10-winx64) 1. 官网下载mysql zip安装包,然后解压到你想安装的目录,假设解压的目录是P:\mysql ...
- Harbor的搭建(vmware企业级docker镜像私服)
1.下载harbor,地址https://github.com/vmware/harbor2.进入harbor-master/Deploy目录,修改harbor.cfg文件,主要修改以下信息 ...
- php 在linux 用file_exists() 函数判断 另外一台服务器映射过来的文件是否存在 总是返回false
php 在linux 用file_exists() 函数判断 另外一台服务器映射过来的文件是否存在 总是返回false .如下案例 $type="android"; $url=&q ...
- 函数的光滑化或正则化 卷积 应用 两个统计独立变量X与Y的和的概率密度函数是X与Y的概率密度函数的卷积
http://graphics.stanford.edu/courses/cs178/applets/convolution.html Convolution is an operation on t ...
- Android系统移植与调试之------->如何修改Android设备的桌面背景图片
1.切换到~/mx0831-0525/device/other/TBDG1073/overlay/frameworks/base/core/res/res目录 2.准备好一张相应尺寸的图片并且命名为d ...
- Javaweb--- EL表达式 JSTL标准标签库
一.EL表达式(expression language): 语法 ${...} jsp中page指令有一个属性叫isELIgnored, 用来标记此页面是否忽略EL表达式, 默认为false 举个例 ...
- python基础9 -----python内置函数2
一.python内置所以函数 Built-in Functions abs() divmod() input() open() staticmethod() all() enumera ...