CodeForces - 707D (离线+DFS模拟)

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 ...
摘要由CSDN通过智能技术生成

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.

Examples
input

2 3 3
1 1 1
3 2
4 0

output

1
4
0

input

4 2 6
3 2
2 2 2
3 3
3 2
2 2 2
3 2

output

2
1
3
3
2
4

input

2 2 2
3 2
2 2 1

output

2
1

题目大意:有n个书架,每个书架有m个空间,然后有四个操作,
1 i j:表示如果第i个书架的第j个位置没有书的话,就放入一本书。
2 i j:表示如果第i个书架的第j个位置有书的话,那么就将这本书拿掉。
3 i:表示将第i个书架所有的书都拿走,并且将之前没有书的位置都放入一本书。
4 x:表示回到第x次操作的位置
然后问每次操作之后书架上有多少书。

解题思路:本来我想是写主席树,但是这样的话就需要建立n棵主席树,空间肯定炸了,然后看聚聚的博客说的方法,真的有种耳目一新的感觉。那么言归正传,我们开始讲解题思路。
因为第一次操作和第二次操作对答案的贡献都最多为1,操作三对于答案的贡献是和m有关的,所以也可以求出来,只要用一个数组记录目前该书架上有多少颗树就可以了,然后我们就可以来模拟这个过程了,我们运用一个时间戳来记录该时间会做什么操作,然后在上一次时间和这个时间建一条边,表示这个操作是在上一个操作之后得到的,如果我们遇到op=4的情况,那么我们就将这个操作发生的时间赋值到它回到的那种情况中,然后得到了q次操作的关系之后,我们就去模拟这个过程,这个过程是用dfs递归进行的,其实就是这样处理之后形成了一棵树,然后我们去求这棵树在这个规则中的前缀和就可以啦。
代码:

#include <iostream>
#include <cstdio>
#include <cstring
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值