Codeforces Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力
D. Persistent Bookcase
题目连接:
http://www.codeforces.com/contest/707/problem/D
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?
Input
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.
Output
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.
Sample Input
2 3 3
1 1 1
3 2
4 0
Sample Output
1
4
0
Hint
题意
你有n个书架,每个书架有m层。
有四个操作
1 i j,在第i个书架第j层放一本书。
2 i j,把第i个书架第j层的书扔掉
3 i,把第三层的所有书的状态取反,就有的变没,没的变有
4 k,回到第k个询问时候的状态。
题解:
n才1000,所以nq随便过,xjb拿个bitset搞一搞就好了
至于第四个操作,我们根据询问的顺序建树,然后dfs去搞一波就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int n,m,q;
vector<int> E[maxn];
int op[maxn],a[maxn],b[maxn],c[maxn];
int ans[maxn];
bitset<1001>Bit[1001];
bitset<1001>C;
void dfs(int x)
{
if(x==0)
{
for(int i=0;i<E[x].size();i++)
{
ans[E[x][i]]=ans[x];
dfs(E[x][i]);
}
}
if(op[x]==1)
{
int flag = 0;
if(!Bit[a[x]][b[x]])
{
flag = 1;
ans[x]++;
Bit[a[x]][b[x]]=1;
}
for(int i=0;i<E[x].size();i++)
{
ans[E[x][i]]=ans[x];
dfs(E[x][i]);
}
if(flag)
Bit[a[x]][b[x]]=0;
}
if(op[x]==2)
{
int flag = 0;
if(Bit[a[x]][b[x]])
{
flag = 1;
ans[x]--;
Bit[a[x]][b[x]]=0;
}
for(int i=0;i<E[x].size();i++)
{
ans[E[x][i]]=ans[x];
dfs(E[x][i]);
}
if(flag)
Bit[a[x]][b[x]]=1;
}
if(op[x]==3)
{
ans[x]=ans[x]-Bit[a[x]].count();
Bit[a[x]]^=C;
ans[x]=ans[x]+Bit[a[x]].count();
for(int i=0;i<E[x].size();i++)
{
ans[E[x][i]]=ans[x];
dfs(E[x][i]);
}
Bit[a[x]]^=C;
}
if(op[x]==4)
{
for(int i=0;i<E[x].size();i++)
{
ans[E[x][i]]=ans[x];
dfs(E[x][i]);
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&q);
for(int i=1;i<=m;i++)
C[i]=1;
for(int i=1;i<=q;i++)
{
scanf("%d",&op[i]);
if(op[i]==1)
scanf("%d%d",&a[i],&b[i]);
if(op[i]==2)
scanf("%d%d",&a[i],&b[i]);
if(op[i]==3)
scanf("%d",&a[i]);
if(op[i]==4)
scanf("%d",&a[i]),
E[a[i]].push_back(i);
else
E[i-1].push_back(i);
}
dfs(0);
for(int i=1;i<=q;i++)
printf("%d\n",ans[i]);
}
Codeforces Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力的更多相关文章
- 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 DFS
题目链接:http://codeforces.com/contest/707/my 看了这位大神的详细分析,一下子明白了.链接:http://blog.csdn.net/queuelovestack/ ...
- 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 #297 (Div. 2)D. Arthur and Walls 暴力搜索
Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- 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 Round #368 (Div. 2) E. Garlands 二维树状数组 暴力
E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...
随机推荐
- 在Ubuntu14.04 64bit上搭建单机Spark环境,IDE为Intelli IDEA
在Ubuntu14.04 64bit上搭建单机Spark环境,IDE为Intelli IDEA 一. 环境 Ubuntu14.04 64位 JDK 1.8.0_73 scala-2.10. ...
- javascript类式继承函数最优版
直接上代码: klass函数 var klass = function (Parent, props) { var Child, F, i; //1.新构造函数 Child = function () ...
- plsql免安装客户端的配置
不安装oracle,在安装了plsql之后,需要连接数据库,连接数据库需要在tns中tnsnames.ora中配置 首先需要两个文件: network instantclient-basic-win3 ...
- 第10月第5天 v8
1. brew install v8 http://www.cnblogs.com/tinyjian/archive/2017/01/17/6294352.html http://blog.csdn. ...
- C++ socket 网络编程 简单聊天室
操作系统里的进程通讯方式有6种:(有名/匿名)管道.信号.消息队列.信号量.内存(最快).套接字(最常用),这里我们来介绍用socket来实现进程通讯. 1.简单实现一个单向发送与接收 这是套接字的工 ...
- KNN实现手写数字识别
KNN实现手写数字识别 博客上显示这个没有Jupyter的好看,想看Jupyter Notebook的请戳KNN实现手写数字识别.ipynb 1 - 导入模块 import numpy as np i ...
- Maven从私服上下载所需jar包——(十四)
1.修改settings.xml 将下面代码添加到settings.xml中 <profile> <!--profile的id--> <id>dev</id& ...
- C#使用mybatis学习笔记
1.官网:http://mybatis.org/index.html 2.代码:https://code.google.com/p/mybatisnet/ 3.wiki:http://zh.wikip ...
- css初始化minireset.css
一个很小的现代CSS重置,涵盖了基本内容: 重置字体大小:这样使用语义标记不会影响样式 重置块边距:所以只有在需要时才应用间距 重置表格:这样表格数据只占用它所需的空间 保留了行内间距:因此,按钮和输 ...
- SSH2框架搭建 和 配置文件详解
-----------补充说明----------- 文章中所列出的struts2的2.2jar包已经不是最新的了,这个版本有严重漏洞, 现在最新版本为2.3.15,所以.你懂的http://stru ...