题目链接:

D. 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 题意: 给四种操作,1是使位置(i,j)有一本书,2是使位置(i,j)为空,3是使第i行反转,4是使当前状态转变成第i步操作后的状态;每次操作输出操作完后有多少本书; 思路: 可以发现如果是1,2,3那么第i个状态就是第i-1个状态转移过来的,如果是4操作,那么就这状态就由x[i]转移过来,这样就可以得到的是一棵有根树,然后一遍dfs遍历
1,2,3操作都是可逆的,dfs在往下走的时候和往回走的时候1和2操作相反,3和3相反,所以就很好得到答案了; AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e5+10;
const int maxn=1e3+20;
const double eps=1e-12; int n,m,q;
int mp[maxn][maxn],ans[N],sum=0,op[N],x[N],y[N],flag[N];
vector<int>ve[N]; int dfs(int cur)
{
if(op[cur]==1)
{
if(!mp[x[cur]][y[cur]])flag[cur]=1,mp[x[cur]][y[cur]]=1,sum++;
ans[cur]=sum;
}
else if(op[cur]==2)
{
if(mp[x[cur]][y[cur]])flag[cur]=1,mp[x[cur]][y[cur]]=0,sum--;
ans[cur]=sum;
}
else if(op[cur]==3)
{
for(int i=1;i<=m;i++)
{
if(mp[x[cur]][i])mp[x[cur]][i]=0,sum--;
else mp[x[cur]][i]=1,sum++;
}
ans[cur]=sum;
}
else if(op[cur]==4)
{
ans[cur]=ans[x[cur]];
}
int len=ve[cur].size();
for(int i=0;i<len;i++)dfs(ve[cur][i]); if(op[cur]==2)
{
if(!mp[x[cur]][y[cur]]&&flag[cur])mp[x[cur]][y[cur]]=1,sum++;
}
else if(op[cur]==1)
{
if(mp[x[cur]][y[cur]]&&flag[cur])mp[x[cur]][y[cur]]=0,sum--;
}
else if(op[cur]==3)
{
for(int i=1;i<=m;i++)
{
if(mp[x[cur]][i])mp[x[cur]][i]=0,sum--;
else mp[x[cur]][i]=1,sum++;
}
}
}
int main()
{
read(n);read(m);read(q);
op[0]=0;
for(int i=1;i<=q;i++)
{
read(op[i]);read(x[i]);
if(op[i]==4){ve[x[i]].push_back(i);continue;}
else if(op[i]==1||op[i]==2)read(y[i]);
ve[i-1].push_back(i);
}
dfs(0);
for(int i=1;i<=q;i++)print(ans[i]); return 0;
}

  

codeforces 707D D. Persistent Bookcase(dfs)的更多相关文章

  1. codeforces 707D:Persistent Bookcase

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

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

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

  3. CodeForces #368 div2 D Persistent Bookcase DFS

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

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

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

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

    题目链接:http://codeforces.com/contest/707/my 看了这位大神的详细分析,一下子明白了.链接:http://blog.csdn.net/queuelovestack/ ...

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

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

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

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

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

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

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

随机推荐

  1. selenium 遇到chrome 弹出是否保存密码框

    --待验证(以下是java脚本) ChromeOptions options = new ChromeOptions(); options.addArguments("--start-max ...

  2. 一种关键字搜索---edu.cn

    比如要搜索知识点最小二乘,可以这样: 曲线拟合的最小二乘法 edu.cn 然后就一大片关于edu的相关链接,很多知名学校链接 http://www.bb.ustc.edu.cn/jpkc/xiaoji ...

  3. Web前端开发规范【HTML/JavaScript/CSS】

    前言 这是一份旨在增强团队的开发协作,提高代码质量和打造开发基石的编码风格规范,其中包含了 HTML, JavaScript 和 CSS/SCSS 这几个部分.我们知道,当一个团队开始指定并实行编码规 ...

  4. 九度OJ 1333:考研海报 (区间操作)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:738 解决:299 题目描述: sun是万千考研学子中的一员,他每天过着三点一线的生活. 学校里有一个公告栏,他每天都看到上面张贴着各种考研 ...

  5. go语言之并发编程 channel

    前面介绍了goroutine的用法,如果有多个goroutine的话相互之间是如何传递数据和通信的呢.在C语言或者JAVA中,传输的方法包括共享内存,管道,信号.而在Go语言中,有了更方便的方法,就是 ...

  6. git clone了整个远程仓库分支

    git之远程标签下载(远程分支) 一般我们发布一个新版本到线上服务器时都会在版本库中打一个标签,这样我们可以随时查看这个打标签的版本,就是说标签其实是版本库中一个快照.git的标签与分支类似,区别是分 ...

  7. LeetCode:寻找重复数【287】

    LeetCode:寻找重复数[287] 题目描述 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数 ...

  8. rails dependent

    dependent 可以設定當物件刪除時,也會順便刪除它的 has_many 物件: class Event < ActiveRecord::Base has_many :attendees, ...

  9. 人生要golang

    第一篇 : 初识golang 第二篇 : 下载及安装 未完待续 ............................................

  10. PHP操作MySQL事务处理

    PHP操作MySQL事务处理 /*************** 用begin,rollback,commit来实现 ***************/ /*方法二*/ $conn = mysqli_co ...