D. Persistent Bookcase(Codeforces Round #368 (Div. 2))
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
This image illustrates the second sample case.
思路:dfs+bitset;
每个状态都是由前面的状态而得到的,但当4操作的时候,这个时候这个状态就是k那个状态,所以本状态可以看成是有k状态得来,所以我们可以建图,确定当前状态的前一个状态
,然后dfs就行了,并且用bitset记录状态。
- 1 #include<stdio.h>
- 2 #include<algorithm>
- 3 #include<iostream>
- 4 #include<string.h>
- 5 #include<queue>
- 6 #include<set>
- 7 #include<bitset>
- 8 using namespace std;
- 9 typedef struct node
- 10 {
- 11 int val;
- 12 int x;
- 13 int y;
- 14 } ss;
- 15 ss ak[100005];
- 16 bitset<1005>bit[1005],c;
- 17 vector<int>vec[100005];
- 18 int ask[100005];
- 19 bool T[100005];
- 20 int all;
- 21 void ou(int s);
- 22 void dfs(int s);
- 23 void in(int s);
- 24 int main(void)
- 25 {
- 26 int i,j;
- 27 int n,m,k;
- 28 while(scanf("%d %d %d",&n,&m,&k)!=EOF)
- 29 {
- 30 all = 0;
- 31 memset(T,0,sizeof(T));
- 32 for(i = 1; i <= m ; i++)
- 33 {
- 34 bit[i].reset();
- 35 c.set(i);
- 36 }
- 37 for(i = 0; i < 10005; i++)
- 38 vec[i].clear();
- 39 for(i = 1; i <= k; i++)
- 40 {
- 41 scanf("%d",&ak[i].val);
- 42 if(ak[i].val<= 2)
- 43 {
- 44 scanf("%d %d",&ak[i].x,&ak[i].y);
- 45 vec[i-1].push_back(i);
- 46 }
- 47 else
- 48 {
- 49 scanf("%d",&ak[i].x);
- 50 if(ak[i].val == 3)
- 51 {
- 52 vec[i-1].push_back(i);
- 53 }
- 54 else vec[ak[i].x].push_back(i);
- 55 }
- 56 }
- 57 dfs(0);
- 58 for(i = 1; i <=k ; i++)
- 59 printf("%d\n",ask[i]);
- 60 }
- 61 return 0;
- 62 }
- 63 void dfs(int s)
- 64 {
- 65 int i,j;
- 66 for(i = 0; i < vec[s].size() ; i++)
- 67 {in(vec[s][i]);dfs(vec[s][i]);ou(vec[s][i]);}
- 68 }
- 69 void in(int s)
- 70 {
- 71 if(ak[s].val == 1)
- 72 {
- 73 if(!bit[ak[s].x][ak[s].y])
- 74 {
- 75 T[s] = true;
- 76 bit[ak[s].x].set(ak[s].y);
- 77 ask[s] = all+1;
- 78 all++;
- 79 }
- 80 else ask[s] = all;
- 81 }
- 82 else if(ak[s].val == 2)
- 83 {
- 84 if(bit[ak[s].x][ak[s].y])
- 85 {
- 86 T[s] = true;
- 87 bit[ak[s].x].reset(ak[s].y);
- 88 ask[s] = all-1;
- 89 all--;
- 90 }
- 91 else ask[s] = all;
- 92 }
- 93 else if(ak[s].val == 3)
- 94 {
- 95 all -=bit[ak[s].x].count();
- 96 bit[ak[s].x] ^= c;
- 97 all += bit[ak[s].x].count();
- 98 ask[s] = all;
- 99
- 100 }
- 101 else ask[s] = all;
- 102 }
- 103 void ou(int s)
- 104 {
- 105 if(ak[s].val == 1)
- 106 {
- 107 if(T[s])
- 108 {
- 109 all --;
- 110 bit[ak[s].x].reset(ak[s].y);
- 111 }
- 112 }
- 113 else if(ak[s].val == 2)
- 114 {
- 115 if(T[s])
- 116 {
- 117 all++;
- 118 bit[ak[s].x].set(ak[s].y);
- 119 }
- 120 }
- 121 else if(ak[s].val == 3)
- 122 {
- 123 all -=bit[ak[s].x].count();
- 124 bit[ak[s].x] ^= c;
- 125 all += bit[ak[s].x].count();
- 126 }
- 127 }
D. Persistent Bookcase(Codeforces Round #368 (Div. 2))的更多相关文章
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)
Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...
- Codeforces Round #368 (Div. 2) B. Bakery (模拟)
Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...
- Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)
Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...
- 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 Round #368 (Div. 2)D. Persistent Bookcase DFS
题目链接:http://codeforces.com/contest/707/my 看了这位大神的详细分析,一下子明白了.链接:http://blog.csdn.net/queuelovestack/ ...
- Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组 暴力
E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...
- Codeforces Round #368 (Div. 2) C. Pythagorean Triples 数学
C. Pythagorean Triples 题目连接: http://www.codeforces.com/contest/707/problem/C Description Katya studi ...
随机推荐
- 05 Windows安装python3.6.4+pycharm环境
windows安装python3.6.4环境 使用微信扫码关注微信公众号,并回复:"Python工具包",免费获取下载链接! 一.卸载python环境 卸载以下软件: 二.安装py ...
- WebRTC本地分享屏幕,录制屏幕
WebRTC有分享屏幕的功能.使用的是getDisplayMedia方法.用户同意分享屏幕后,可以拿到视频流. 再结合MediaRecorder和Blob,把视频流数据存下来,就能得到录制屏幕的视频. ...
- (亿级流量)分布式防重复提交token设计
大型互联网项目中,很多流量都达到亿级.同一时间很多的人在使用,而每个用户提交表单的时候都可能会出现重复点击的情况,此时如果不做好控制,那么系统将会产生很多的数据重复的问题.怎样去设计一个高可用的防重复 ...
- 输入URL展示过程
一. 输入URL,回车 敲击某个键时,键盘内的处理器会先对键矩阵进行分析,然后将数据发送到计算机 计算机接收到来自键盘的信号,由键盘控制器(一种集成电路)进行处理,发送给操作系统 操作系统会分析,这些 ...
- Linux系统根目录下各文件夹介绍
参考自:[1]Linux 系统根目录下各个文件夹的作用 https://www.cnblogs.com/jiangfeilong/p/10538795.html[2]了解Linux根目录"/ ...
- Linux学习 - 条件判断
一.判断格式 test -e /root/install.log 或 [ -e /root/install.log ] 使用echo $?查看是否正确,当返回0时表示返回正确 1 按照文件类型进行判断 ...
- Activity 详解
1.活动的生命周期 1.1.返回栈 Android是使用任务(Task)来管理活动的,一个任务就是一组存放在栈里的活动的集合,这个栈也被称作返回栈.栈是一种先进后出的数据结构,在默认情况下,每当我们启 ...
- When does compiler create default and copy constructors in C++?
In C++, compiler creates a default constructor if we don't define our own constructor (See this). Co ...
- 【Linux】【Services】【SaaS】Docker+kubernetes(9. 安装consul实现服务注册发现)
1. 简介 1.1. 官方网站: https://www.consul.io 1.2. Consul的功能: 服务发现:通过DNS或HTTP接口使得消费者发现服务,应用程序可以轻松找到所依赖的服务. ...
- 【Java 调优】Java性能优化
Java性能优化的50个细节(珍藏版) 1. 尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时间,提高加载的效率,但并不是所有地方都适用于单例,简单来说,单例主要适用于以下三个方面: ...