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: ...
随机推荐
- Google 全球 IP 地址库
## Google 全球 IP 地址库 IP 地址来源:http://www.kookle.co.nr Bulgaria 93.123.23.1 93.123.23.2 93.123.23.3 93. ...
- lua学习笔记(四)
表达式 算术操作符 +(加法) -(减法) *(乘法) /(除法) ^(指数) %(取模) -(负号) x%1的结果是x的小数部分,x-x%1是整数部分 关系操作符 ...
- 50 years of Computer Architecture: From the Mainframe CPU to the Domain-Specific TPU and the Open RISC-V Instruction Set
1.1960年代(大型机) IBM发明了具有二进制兼容性的ISA——System/360,可以兼容一系列的8到64位的硬件产品,而不必更换操作系统.这是通过微编程实现的,每个计算机模型都有各自的ISA ...
- mybaits返回插入成功后的自增值
mybaits返回插入成功后的自增值 在项目中,我们经常遇到这样的情况:insert语句成功后,需要自增的id值,这个时候,我们可以通过mybatis的 useGeneratedKeys 来实现,具体 ...
- SQL-SQL基础
SQL(Structured Query Language)是通用的数据库查询语言,各个数据库厂商均对SQL-92标准做了支持,同一时候各家又再次基础上做了相应扩展,比如oracle的PL/SLQ. ...
- [c++]对象指针,引用的操作
1.time类保存在"htime.h"中.要求: ⑴ 数据成员包括时(hour).分(minute).秒(second),为私有成员: ⑵ 能给数据成员提供值的成员函数(默认值为0 ...
- 解决from lxml import etree 导入的时候,显示etree不存在
问题: 当安装完lxml之后,发现使用 from lxml import etree 时,etree不可用 原因 :是lxml中没有etree包 解决: 去官网下载对应包:官网地址:http://l ...
- GitHub 小试牛刀(踩坑记录)
首先要在GitHub上创建好远程仓库,把README,LISCENCE,.gitignore三个文件在远程仓库初始化好. 然后在创建本地仓库,先要cd到自己的项目目录下,然后: $ git init ...
- 序列DP(输出有要求)
DP Time Limit:10000MS Memory Limit:165888KB 64bit IO Format:%lld & %llu Submit Status De ...
- iOS启动页加载广告
1.定义全局成员变量 @interface AppDelegate () @property (strong, nonatomic) UIImageView *adImageView; @proper ...