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. cocos2dx3.0戳青蛙游戏(打地鼠)

    1戳青蛙项目描写叙述 1.1功能描写叙述 实现类似打地鼠游戏.青蛙随机出如今屏幕左边5*3的格子中,并会向屏幕右边移动,在青蛙逃离之前,手指点击实现戳灭青蛙的效果.随着分数添加,青蛙越来越多,当青蛙逃 ...

  2. 在iOS中实现sticky header

    经常在网页中看到这样一种效果,当页面滚动一段距离后,页面中的某个部分固定在一个区域(通常是头部导航),这种效果一般称为Sticky Header,如下图所示: 上述操作在Android系统中非常好实现 ...

  3. Java学习笔记之equals和Objects.equals

    equals 相信大家就知道,就是比较,我们平时也会在自己定义的类中加入自己重写的equals用来比较两个类是否相同,例如这样 public class Person { private String ...

  4. web 开发之js---js 实现自动添加input text 编辑框

    <html><head><script type="text/javascript">function addNewLine(){var for ...

  5. js自己定义插件-选项卡

    该功能比較简单.巩固一下jquery插件写法,注意引入的jquery.js  . 自己定义插件路径代码例如以下: 页面: <!doctype html> <html> < ...

  6. 转载:python基础之模块

    作者:武沛齐 出处:http://www.cnblogs.com/wupeiqi/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接. 模块,用一 ...

  7. python接口自动化(四十二)- 项目结构设计之大结局(超详解)

    简介 这一篇主要是将前边的所有知识做一个整合,把各种各样的砖块---模块(post请求,get请求,logging,参数关联,接口封装等等)垒起来,搭建一个房子.并且有很多小伙伴对于接口项目测试的框架 ...

  8. springmvc demo

    [说明]今天上午稍稍偏了一下方向,看了看servlet的相关知识,下午做maven+spring+springMVC的整合,晚上成功实现了一个小demo(可以在jsp动态页面上获得通过地址栏输入的参数 ...

  9. Webpack探索【3】--- loader详解

    本文主要说明Webpack的loader相关内容.

  10. [note]最近公共祖先

    最近公共祖先(LCA)https://www.luogu.org/problemnew/show/P3379 #define RG register #include<cstdio> #i ...