Persistent Bookcase
time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

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 1to 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:

  • i j — Place a book at position j at shelf i if there is no book at it.
  • i j — Remove the book from position j at shelf i if there is a book at it.
  • 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.
  • 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 nm 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
Note

This image illustrates the second sample case.

分析:难点是询问为4的时候;
   根据询问建树,回到某个时刻就把他设为那个时刻的儿子即可,后面dfs就很轻松了;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e5+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,s[maxn][],ans[maxn],sum[],q[],p[][];
vi a[maxn];
void dfs(int now)
{
for(int x:a[now])
{
if(s[x][]==)
{
ans[x]=ans[now];
dfs(x);
}
else if(s[x][]==)
{
if((q[s[x][]]^p[s[x][]][s[x][]])==)
{
p[s[x][]][s[x][]]^=;
sum[s[x][]]++;
ans[x]=ans[now]+;
dfs(x);
p[s[x][]][s[x][]]^=;
sum[s[x][]]--;
}
else ans[x]=ans[now],dfs(x);
}
else if(s[x][]==)
{
if((q[s[x][]]^p[s[x][]][s[x][]])==)
{
p[s[x][]][s[x][]]^=;
sum[s[x][]]--;
ans[x]=ans[now]-;
dfs(x);
p[s[x][]][s[x][]]^=;
sum[s[x][]]++;
}
else ans[x]=ans[now],dfs(x);
}
else
{
q[s[x][]]^=;
ans[x]=ans[now]-*sum[s[x][]]+m;
sum[s[x][]]=m-sum[s[x][]];
dfs(x);
q[s[x][]]^=;
sum[s[x][]]=m-sum[s[x][]];
}
}
}
int main()
{
int i,j;
scanf("%d%d%d",&n,&m,&t);
rep(i,,t)
{
scanf("%d",&s[i][]);
if(s[i][]==||s[i][]==)
{
scanf("%d%d",&s[i][],&s[i][]);
a[i-].pb(i);
}
else if(s[i][]==)
{
scanf("%d",&s[i][]);
a[i-].pb(i);
}
else
{
scanf("%d",&s[i][]);
a[s[i][]].pb(i);
}
}
dfs();
rep(i,,t)printf("%d\n",ans[i]);
//system("Pause");
return ;
}

Persistent Bookcase的更多相关文章

  1. CodeForces #368 div2 D Persistent Bookcase DFS

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

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

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

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

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

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

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

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

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

  6. CF707D Persistent Bookcase

    CF707D Persistent Bookcase 洛谷评测传送门 题目描述 Recently in school Alina has learned what are the persistent ...

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

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

  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

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

随机推荐

  1. IOS设备上网页中的页面滚动效果模拟

    可能咋一看不知道我说的是个啥,因为iOS本来就用这功能的啊,还模拟它干啥?先听我说下项目背景哈 我现在开发的是一个webapp,主要是用在ipad上,这个app的大小是固定大小的,为了防止触摸它出现弹 ...

  2. 用 openSSL 生成 公钥 私钥

    支付宝app接口需要 RSA加密通讯 https://doc.open.alipay.com/doc2/detail?treeId=58&articleId=103242&docTyp ...

  3. eclipse关联源码的方法

    1.在项目的libs目录下,新建一个android-support-v4.jar.properties文件 2.打开android-support-v4.jar.properties,编辑. 输入源码 ...

  4. 多个dropdownlist只有第一个能选中,其他选不中之我见

    前段时间遇到这个问题,发现在页面中的源代码已经显示selected=“selected" 可是还是选中的第一项,试过很多办法,都不行,最后只好靠js来解决了,获取所有的dropdownlis ...

  5. MULE-ET0 、 ET1、ET2、PT1、PT2

    设计验证阶段中的五个样车试制概念 骡子车( mule car ) ET0 第一轮设计工程样车试制 ET1 第二轮设计工程样车试制 ET2 第一轮产品工装样车试制 PT1 第二轮产品工装样车试制 PT2 ...

  6. perl-cgi-form

    来源: http://www.cnblogs.com/itech/archive/2012/09/23/2698595.html http://www.cnblogs.com/itech/archiv ...

  7. iptables查看、添加、删除规则

    1.查看iptables -nvL –line-number -L 查看当前表的所有规则,默认查看的是filter表,如果要查看NAT表,可以加上-t NAT参数-n 不对ip地址进行反查,加上这个参 ...

  8. sql server中单引号拼接字符串(书写错误会出现错误"浮点值 XXXX 超出了计算机表示范围(8 个字节)。“XX”附近有语法错误。")

    " ' "(单引号)的运用:在sql server中,两个" ' "(单引号)在拼接字符串的情况下运用,就是表示拼接上了一个" ' "单引号 ...

  9. 用ifconfig命令,只有lo,没有eth0的解决方案

    解决方案: 1. 进入/etc/sysconfig/network-scripts 目录,发现有ifcfg-eth0,即网卡(驱动)存在但未启用. 2. 输入ifconfig -a命令,可显示eth0 ...

  10. div+css网页本地上和上传到服务器后在IE11上看到的效果不一样?

    div+css网页本地上和上传到服务器后在IE11上看到的效果不一样? 解决办法在html的head里加上一段:<meta http-equiv="X-UA-Compatible&qu ...