Persistent Bookcase
2 seconds
512 megabytes
standard input
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:
- 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?
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.
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.
2 3 3
1 1 1
3 2
4 0
1
4
0
4 2 6
3 2
2 2 2
3 3
3 2
2 2 2
3 2
2
1
3
3
2
4
2 2 2
3 2
2 2 1
2
1
This image illustrates the second sample case.
#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的更多相关文章
- CodeForces #368 div2 D Persistent Bookcase DFS
题目链接:D Persistent Bookcase 题意:有一个n*m的书架,开始是空的,现在有k种操作: 1 x y 这个位置如果没书,放书. 2 x y 这个位置如果有书,拿走. 3 x 反转这 ...
- 【Codeforces-707D】Persistent Bookcase DFS + 线段树
D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase
Persistent Bookcase Problem Description: Recently in school Alina has learned what are the persisten ...
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力
D. Persistent Bookcase 题目连接: http://www.codeforces.com/contest/707/problem/D Description Recently in ...
- codeforces 707D D. Persistent Bookcase(dfs)
题目链接: D. Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input ...
- CF707D Persistent Bookcase
CF707D Persistent Bookcase 洛谷评测传送门 题目描述 Recently in school Alina has learned what are the persistent ...
- Persistent Bookcase CodeForces - 707D (dfs 离线处理有根树模型的问题&&Bitset)
Persistent Bookcase CodeForces - 707D time limit per test 2 seconds memory limit per test 512 megaby ...
- 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 ...
- codeforces 707D:Persistent Bookcase
Description Recently in school Alina has learned what are the persistent data structures: they are d ...
随机推荐
- 以excel方式输出数据
主类Test: public class D201 {//get set 方法略去 private String d201_01; private String d201_02; private St ...
- think in uml 2.1
业务建模
- UITextfield的一些属性
//设置左视图 不用设置frame UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@&quo ...
- 在idea的maven项目使用el或jstl表达式
必须加上这句: <%@ page isELIgnored="false" %> 否则无法解析el或jstl表达式 <%@ taglib prefix=" ...
- 【dp】 poj 1157
不错的dp入门题 画出dp矩阵 每个dp[i][j]是由“其上”的状态或是“其左上”的状态转化而来,那我们选对角线和上边进行三角dp推导 #include<stdio.h> #incl ...
- Slow HTTP Denial of Service Attack 漏洞解决
修改tomcat conf 下 server.xml 文件 <Connector port="8080" protocol="HTTP/1.1" con ...
- [转]于Fragment和Activity之间onCreateOptionsMenu的问题
Fragment和Activity一样,可以重写onCreateOptionsMenu方法来设定自己的菜单,其实这两个地方使用onCreateOptionsMenu的目的和效果都是完全一样的,但是由于 ...
- JS定时器的使用--数码时钟
<title>无标题文档</title> <script> function toDou(n){ if(n<10){ return '0'+n; }else{ ...
- 平移关节(Prismatic Joint)
package{ import Box2D.Common.Math.b2Vec2; import Box2D.Dynamics.b2Body; import Box2D.Dynamics.Joints ...
- 性能更好的js动画实现方式---requestAnimationFrame
用js来实现动画,我们一般是借助setTimeout或setInterval这两个函数,css3动画出来后,我们又可以使用css3来实现动画了,而且性能和流畅度也得到了很大的提升.但是css3动画还是 ...