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]);
}

最新文章

  1. 自定义UICollectionLayout布局 —— UIKit之学习UICollectionView记录一《瀑布流》
  2. android 评分条 RatingBar 使用及自定义
  3. spring.xml中的配置
  4. 基于httpClient的https的无秘钥登陆
  5. 2016C#模拟谷歌Google登陆Gmail&amp;Youtube小案例
  6. 字符串转Json对象
  7. asyn4j -- java 异步方法调用框架
  8. Linux 下的五种 IO 模型
  9. Get AD user 的三种方法
  10. Uncode-Schedule首页、文档和下载 - 分布式任务调度组件 - 开源中国社区
  11. 介绍linux下Source Insight强大代码编辑器sublime_text_3
  12. maven 3.3.9-bin 和 maven 3.3.9-src 的区别 以及 maven安装包的 .tar.gz后缀与.zip 后缀的区别
  13. thinkphp3.2开发网页实现第三方登录
  14. 用Python从零开始创建区块链
  15. Idea工具开发 SpringBoot整合JSP(毕设亲测可用)
  16. LeetCode算法题-Maximum Product of Three Numbers(Java实现)
  17. zt (stack overflow 介绍)
  18. shell脚本结构
  19. 【转】Android 关于arm64-v8a、armeabi-v7a、armeabi、x86下的so文件兼容问题
  20. Educational Codeforces Round 60 D dp + 矩阵快速幂

热门文章

  1. 那些年的 网络通信之 TCP/IP 传输控制协议 ip 加 端口 ---
  2. J2EE完全手册(一)
  3. 一些CSS3的乐趣 - 工作也能发现乐的源头
  4. TED_Topic10:The case for engineering our food
  5. 【LibreOJ】#6354. 「CodePlus 2018 4 月赛」最短路 异或优化建图+Dijkstra
  6. 20155207王雪纯 2016-2017-2 《Java程序设计》第六周学习总结
  7. Django安装配置
  8. 【项目部署】部署项目以war包部署和解开以目录部署的区别
  9. 安装informatic过程中的错误
  10. 洛谷 P4609: [FJOI2016] 建筑师