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. macbook 下SDK Manager 的更新[即使Google被屏蔽了也无所谓]

    废话少说,直接说操作步骤: 1. 改动/etc/hosts文件: 右键单击Finder,选择前往目录,输入/private/etc/,敲回车键.将文件/etc/hosts复制到桌面(由于权限受限.无法 ...

  2. JSON解析工具-org.json使用教程

    转自:http://www.open-open.com/lib/view/open1381566882614.html 一.简介  org.json是Java常用的Json解析工具,主要提供JSONO ...

  3. cat 命令

    cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 1.命令格式: cat [选项] [文件] ...

  4. 【数据挖掘】分类之kNN(转载)

    [数据挖掘]分类之kNN 1.算法简介 kNN的思想很简单:计算待分类的数据点与训练集所有样本点,取距离最近的k个样本:统计这k个样本的类别数量:根据多数表决方案,取数量最多的那一类作为待测样本的类别 ...

  5. 把flask部署到服务器

    1.新建一个wsgi.py文件 # -*- coding:utf-8 -*- import sys from os.path import abspath from os.path import di ...

  6. linux 上拷贝文件到windows 上 文件出现锁的文件

    要在linux上拷贝出文件到windows上,那么文件必须是777的最高权限. chmod wb_redis -R

  7. git入门五(分支合并冲突和衍合)

    分支合并冲突的处理   合并分支的冲突时在不同的分支中修改了同一个文件的同一部分,程序无法把两份有差异的文件合并,这时候需要人为的干预解决冲突.当前处于master 分支,当dev 分支和master ...

  8. 将非递减有序排列(L L1)归并为一个新的线性表L2 线性表L2中的元素仍按值非递减

    #include "stdio.h"#include "stdlib.h"#include "function.h"void main(){ ...

  9. sgu Theodore Roosevelt【判断点是否在凸多边形内模板】

    链接: http://acm.sgu.ru/problem.php?contest=0&problem=253 http://acm.hust.edu.cn/vjudge/contest/vi ...

  10. centos7 PXE自动安装环境搭建

    原理: 要进行自动安装的主机A,加电启动时以网卡为第一启动设备 1.启动时会向网络广播,找到dhcp服务器B请求分配IP地址信息,服务器B除了给其分配基本的IP信息(ip.netmask.getewa ...