codeforces 707D D. Persistent Bookcase(dfs)
题目链接:
2 seconds
512 megabytes
standard input
standard output
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 1to 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- 题意:
- 给四种操作,1是使位置(i,j)有一本书,2是使位置(i,j)为空,3是使第i行反转,4是使当前状态转变成第i步操作后的状态;每次操作输出操作完后有多少本书;
- 思路:
- 可以发现如果是1,2,3那么第i个状态就是第i-1个状态转移过来的,如果是4操作,那么就这状态就由x[i]转移过来,这样就可以得到的是一棵有根树,然后一遍dfs遍历
1,2,3操作都是可逆的,dfs在往下走的时候和往回走的时候1和2操作相反,3和3相反,所以就很好得到答案了;- AC代码:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <cmath>
- #include <bits/stdc++.h>
- #include <stack>
- #include <map>
- using namespace std;
- #define For(i,j,n) for(int i=j;i<=n;i++)
- #define mst(ss,b) memset(ss,b,sizeof(ss));
- typedef long long LL;
- template<class T> void read(T&num) {
- char CH; bool F=false;
- for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
- for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
- F && (num=-num);
- }
- int stk[70], tp;
- template<class T> inline void print(T p) {
- if(!p) { puts("0"); return; }
- while(p) stk[++ tp] = p%10, p/=10;
- while(tp) putchar(stk[tp--] + '0');
- putchar('\n');
- }
- const LL mod=1e9+7;
- const double PI=acos(-1.0);
- const int inf=1e9;
- const int N=1e5+10;
- const int maxn=1e3+20;
- const double eps=1e-12;
- int n,m,q;
- int mp[maxn][maxn],ans[N],sum=0,op[N],x[N],y[N],flag[N];
- vector<int>ve[N];
- int dfs(int cur)
- {
- if(op[cur]==1)
- {
- if(!mp[x[cur]][y[cur]])flag[cur]=1,mp[x[cur]][y[cur]]=1,sum++;
- ans[cur]=sum;
- }
- else if(op[cur]==2)
- {
- if(mp[x[cur]][y[cur]])flag[cur]=1,mp[x[cur]][y[cur]]=0,sum--;
- ans[cur]=sum;
- }
- else if(op[cur]==3)
- {
- for(int i=1;i<=m;i++)
- {
- if(mp[x[cur]][i])mp[x[cur]][i]=0,sum--;
- else mp[x[cur]][i]=1,sum++;
- }
- ans[cur]=sum;
- }
- else if(op[cur]==4)
- {
- ans[cur]=ans[x[cur]];
- }
- int len=ve[cur].size();
- for(int i=0;i<len;i++)dfs(ve[cur][i]);
- if(op[cur]==2)
- {
- if(!mp[x[cur]][y[cur]]&&flag[cur])mp[x[cur]][y[cur]]=1,sum++;
- }
- else if(op[cur]==1)
- {
- if(mp[x[cur]][y[cur]]&&flag[cur])mp[x[cur]][y[cur]]=0,sum--;
- }
- else if(op[cur]==3)
- {
- for(int i=1;i<=m;i++)
- {
- if(mp[x[cur]][i])mp[x[cur]][i]=0,sum--;
- else mp[x[cur]][i]=1,sum++;
- }
- }
- }
- int main()
- {
- read(n);read(m);read(q);
- op[0]=0;
- for(int i=1;i<=q;i++)
- {
- read(op[i]);read(x[i]);
- if(op[i]==4){ve[x[i]].push_back(i);continue;}
- else if(op[i]==1||op[i]==2)read(y[i]);
- ve[i-1].push_back(i);
- }
- dfs(0);
- for(int i=1;i<=q;i++)print(ans[i]);
- return 0;
- }
codeforces 707D D. Persistent Bookcase(dfs)的更多相关文章
- codeforces 707D:Persistent Bookcase
Description Recently in school Alina has learned what are the persistent data structures: they are d ...
- 【21.28%】【codeforces 707D】Persistent Bookcase
time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- 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: ...
- Codeforces Round #368 (Div. 2)D. Persistent Bookcase DFS
题目链接:http://codeforces.com/contest/707/my 看了这位大神的详细分析,一下子明白了.链接:http://blog.csdn.net/queuelovestack/ ...
- 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 ...
- 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 ...
随机推荐
- ASIHTTP
本文转载至 http://www.th7.cn/Program/IOS/201303/128223.shtml 向服务器端上传数据 ASIFormDataRequest ,模拟 Form表单提 ...
- SWD下载调试填坑,SWD连接丢失问题解决
野火SWD下载器,设置好以后,第一次下载成功,莫名其妙丢失连接,发现在复位状态可以连接(惊奇) 网络上搜索到把Boot0和Boot1置高,就可以把程序下载到RAM里, 能下载以后就好办了,把程序里SW ...
- iOS Load方法 和 initialize方法的比较
一.load方法特点: 1. 当类被引用进程序的时候会执行这个函数 2.一个类的load方法不用写明[super load],父类就会收到调用,并且在子类之前. 3.Category的load也会收到 ...
- apche安装教程
从Apache官网下载windows安装版的Apache服务器了, 现在分享给大家. 1 进入apache服务器官网http://httpd.apache.org/,这里我们以下载稳定版的 htt ...
- linux系统环境下搭建coreseek(+mmseg3) (good)
1.下载并解压coreseek软件,操作命令如下: wget http://www.coreseek.cn/uploads/csft/3.2/coreseek-3.2.14.tar.gz 说明:文件下 ...
- 纪念下自学QT 第十天 终于写成了串口调试助手
- 波浪分析数据转换:大智慧、钱龙、胜龙可用Advanced GET ToGet 数据转换器V3.05特别版
http://www.55188.com/thread-4185427-1-1.html Advanced GET ToGet 数据转换器V3.05特别版,大智慧可用软件数据类型选“分析家”源软件数据 ...
- GUI菜单——菜单条、菜单、子条目之间关系
菜单:注意区分三个概念:菜单条.菜单.菜单项 将菜单条添加到窗体,菜单条下面包括菜单,菜单下面可以使菜单或者菜单项 菜单项是最后一个.菜单后面有三角标示. 菜单条[文件] 子菜单--子条目 子条目 示 ...
- mysql高可用研究(一) 主从+MHA架构
最近在研究mysql的高可用架构,自己想总结下常用的高可用方案都有哪些.有哪些优缺点以及应用的场景?搞得是头昏脑涨,天昏地暗,看了诸多资料,每次都觉得公说公有理婆说婆有理.其实嘛,大家说的都有一定的道 ...
- RabbitMQ高级应用
高级应用一: 手动模式和自动应答模式 1. 了确保消息不会丢失,RabbitMQ支持消息应答.消费者发送一个消息应答,告诉RabbitMQ这个消息已经接收并且处理完毕了.RabbitMQ就可以删除它了 ...