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.

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 正解:搜索+操作树
解题报告:
  这道题误导我思考可持久化数据结构。。。
  先把操作离线,然后考虑当前操作由哪一步转过来。若是要完成第4个操作,我们需要保存下每步操作之后的全局局面,空间不够,不妨时间换空间。
  考虑我们每步操作都走向他能走向的操作(应对return操作),然后修改状态,得出当前答案,dfs下去,只需要在dfs完之后回溯,减掉这次操作的贡献就可以了。
  这种做法第一次接触,还是很神的。
 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int MAXQ = ;
const int MAXN = ;
const int MAXM = ;
int n,m,q,now,ecnt;
int next[MAXM],first[MAXN],to[MAXM],f[MAXN];
int tag[],a[][];
int ans[MAXQ];
int cnt[];
int all_cnt;//全局
struct wen{
int type;
int x,y;
}Q[MAXQ]; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline void link(int x,int y){next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y;} inline void dfs(int k){
bool flag=false;//是否做过修改
int tp=Q[k].type,x=Q[k].x,y=Q[k].y;
if(tp==){ if(!(a[x][y]^tag[x])) cnt[x]++,all_cnt++,a[x][y]^=,flag=true; }
else if(tp==){ if(a[x][y]^tag[x]) cnt[x]--,all_cnt--,a[x][y]^=,flag=true; }
else if(tp==) flag=true,all_cnt-=cnt[x],tag[x]^=,cnt[x]=m-cnt[x],all_cnt+=cnt[x]; ans[k]=all_cnt;
for(int i=first[k];i;i=next[i]) dfs(to[i]);
if(!flag) return ;
if(tp==) cnt[x]--,all_cnt--,a[x][y]^=;
else if(tp==) cnt[x]++,all_cnt++,a[x][y]^=;
else if(tp==) all_cnt-=cnt[x],tag[x]^=,cnt[x]=m-cnt[x],all_cnt+=cnt[x];
} inline void work(){
n=getint(); m=getint(); q=getint();
now=;//为操作连边
for(int i=;i<=q;i++) {
Q[i].type=getint();
if(Q[i].type!=) link(now,i); f[i]=now=i;
if(Q[i].type==) Q[i].x=getint(),Q[i].y=getint();
else if(Q[i].type==) Q[i].x=getint(),Q[i].y=getint();
else if(Q[i].type==) Q[i].x=getint();
else Q[i].x=getint(),Q[i].x=f[Q[i].x],f[i]=now=Q[i].x;//返回到k次操作之前的那次之后
}
dfs();
for(int i=;i<=q;i++) printf("%d\n",ans[f[i]]);
} int main()
{
work();
return ;
}

codeforces 707D:Persistent Bookcase的更多相关文章

  1. codeforces 707D D. Persistent Bookcase(dfs)

    题目链接: D. Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input ...

  2. 【21.28%】【codeforces 707D】Persistent Bookcase

    time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...

  3. Codeforces-707D:Persistent Bookcase (离线处理特殊的可持久化问题&&Bitset)

    Recently in school Alina has learned what are the persistent data structures: they are data structur ...

  4. Persistent Bookcase CodeForces - 707D (dfs 离线处理有根树模型的问题&&Bitset)

    Persistent Bookcase CodeForces - 707D time limit per test 2 seconds memory limit per test 512 megaby ...

  5. Codeforces Round #368 (Div. 2) D. Persistent Bookcase

    Persistent Bookcase Problem Description: Recently in school Alina has learned what are the persisten ...

  6. Codeforces Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力

    D. Persistent Bookcase 题目连接: http://www.codeforces.com/contest/707/problem/D Description Recently in ...

  7. CodeForces #368 div2 D Persistent Bookcase DFS

    题目链接:D Persistent Bookcase 题意:有一个n*m的书架,开始是空的,现在有k种操作: 1 x y 这个位置如果没书,放书. 2 x y 这个位置如果有书,拿走. 3 x 反转这 ...

  8. 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 ...

  9. 【Codeforces-707D】Persistent Bookcase DFS + 线段树

    D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...

随机推荐

  1. MySQL三:存储引擎

    阅读目录 一 什么是存储引擎 二 mysql支持的存储引擎 三 使用存储引擎 一 什么是存储引擎 mysql中建立的库===>文件夹 库中建立的表===>文件 现实生活中我们用来存储数据的 ...

  2. ntp服务及其配置

    集群中使用NTP服务 简介 之前搭建zookeeper时报了一个错,我以为是ntp的问题,结果不是.这里详细学习一下如何在集群中使用ntp服务. 什么是ntp服务 来自ntp的百度百科: NTP服务器 ...

  3. linux无线网络配置_转

    转自:http://www.cnblogs.com/dartagnan/archive/2010/12/05/2003521.html   一位资生linux 原文:http://www.hpl.hp ...

  4. Boost学习总结(一)VS2010环境下编译STLport和Boost

    Boost学习总结(一)VS2010环境下编译STLport和Boost Boost简介 Boost库是一个功能强大.构造精巧.跨平台.开源并且完全免费的C++程序库.1998年,Beman G.Da ...

  5. 如何通过Google访问外网

    修改host: https://laod.cn/hosts/2017-google-hosts.html google中文: https://www.google.com.hk/ 弄好前两项后,可以再 ...

  6. Jmeter监控Linux服务器性能

    ①.下载JMeterPlugins相关的jar包,放jmeter的安装路径\lib\ext下——这个时候启动jmeter会发现,添加监听器时,出现了一堆的jp@jc……,这些就是插件的功劳. JMet ...

  7. python 可变参数函数定义* args和**kwargs的用法

    python函数可变参数 (Variable Argument) 的方法:使用*args和**kwargs语法.其中,*args是可变的positional arguments列表,**kwargs是 ...

  8. 虚拟化构建二分图(BZOJ2080 题解+浅谈几道双栈排序思想的题)

    虚拟化构建二分图 ------BZOJ2080 题解+浅谈几道双栈排序思想的题 本题的题解在最下面↓↓↓ 不得不说,第一次接触类似于双栈排序的这种题,是在BZOJ的五月月赛上. [BZOJ4881][ ...

  9. Is this its limit?

    import sys import os curPath = os.path.abspath(os.path.dirname(__file__)) rootPath = os.path.split(c ...

  10. 【python】-- 初识python

    Python 安装 windows: 1.下载安装包 https://www.python.org/downloads/ 2.安装 默认安装路径:C:\python27 3.配置环境变量 [右键计算机 ...