time limit per test2 seconds

memory limit per test512 megabytes

inputstandard input

outputstandard 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 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

Note



This image illustrates the second sample case.

【题解】



要用一个数据结构要求支持以下操作

1 i j 如果i,j没有书,则在这个位置放一本书

2 i j 如果i,j有书,把这本书给去掉

3 i 把第i行,有书的地方的书去掉,原来没书的地方放上书

4 i 把书柜的状态转换到第i次操作之后;



把一个一个询问看成是树的节点。

相当于从根节点开始进行dfs一直走到树的最低端。

然后往回再回溯。再找找其他的路径。

每次节点的答案往下传。然后在新的节点开始进行操作。再往下传。

一个节点有多个儿子节点也同理;

如果不是4号操作则前面一个询问和这个询问所代表的节点连起来;

这些修改操作和异或的操作都可以用bitset实现.

#include <cstdio>
#include <iostream>
#include <bitset>
#include <vector> using namespace std; const int MAXN = 1200;
const int MAXQ = 1e5 + 100; int n, m, q, opt[MAXQ] = { 0 }, key[MAXQ][2] = { 0 };
int ans[MAXQ];
bitset <MAXN> book[MAXN] = { 0 };
bitset <MAXN> temp;
vector <int> a[MAXQ]; void input(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
} void dfs(int x)
{
int x1 = key[x][0], y1 = key[x][1],len = a[x].size();
if (x == 0)
{
for (int i = 0; i <= len - 1; i++)
{
ans[a[x][i]] = ans[x];//答案往下传
dfs(a[x][i]);
}
}
if (opt[x] == 1)
{
bool changed = false;
if (!book[x1][y1])//如果原来没书
{
changed = true;
book[x1][y1] = true;
ans[x]++;
}
for (int i = 0; i <= len - 1; i++)
{
ans[a[x][i]] = ans[x];//答案往下传
dfs(a[x][i]);
}
if (changed)
book[x1][y1] = false;
}
if (opt[x] == 2)
{
bool changed = false;
if (book[x1][y1])//如果原来有书
{
changed = true;
book[x1][y1] = false;
ans[x]--;
}
for (int i = 0; i <= len - 1; i++)
{
ans[a[x][i]] = ans[x];
dfs(a[x][i]);
}
if (changed)//回溯
book[x1][y1] = true;
}
if (opt[x] == 3)//相当于异或操作
{
ans[x] -= book[key[x][0]].count();
book[key[x][0]] ^= temp;
ans[x] += book[key[x][0]].count();
for (int i = 0; i <= len - 1; i++)
{
ans[a[x][i]] = ans[x];
dfs(a[x][i]);
}
book[key[x][0]] ^= temp;//一定要进行回溯操作
}
if (opt[x] == 4)
{
for (int i = 0; i <= len - 1; i++)
{
ans[a[x][i]] = ans[x];
dfs(a[x][i]);
}
}
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
input(n); input(m); input(q);
for (int i = 1; i <= m; i++)
temp[i] = 1;
for (int i = 1; i <= q; i++)
{
input(opt[i]);
if (opt[i] == 1)
input(key[i][0]), input(key[i][1]);
if (opt[i] == 2)
input(key[i][0]), input(key[i][1]);
if (opt[i] == 3)
input(key[i][0]);
if (opt[i] == 4)
{
input(key[i][0]);
a[key[i][0]].push_back(i);
}
else
a[i - 1].push_back(i);//如果操作是1或2或3就把当前这个操作接到前一个操作后面
}
ans[0] = 0;
dfs(0);
for (int i = 1; i <= q; i++)
printf("%d\n", ans[i]);
return 0;
}

【21.28%】【codeforces 707D】Persistent Bookcase的更多相关文章

  1. codeforces 707D:Persistent Bookcase

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

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

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

  3. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  4. 【离线】【深搜】【树】Codeforces 707D Persistent Bookcase

    题目链接: http://codeforces.com/problemset/problem/707/D 题目大意: 一个N*M的书架,支持4种操作 1.把(x,y)变为有书. 2.把(x,y)变为没 ...

  5. 【21.37%】【codeforces 579D】"Or" Game

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

  6. 【21.21%】【codeforces round 382D】Taxes

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

  7. 【75.28%】【codeforces 764B】Decoding

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. 【21.58%】【codeforces 746D】Green and Black Tea

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. 【77.78%】【codeforces 625C】K-special Tables

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

随机推荐

  1. 作为一个新人,怎样学习嵌入式Linux?(韦东山)

    这篇文章是引用韦老师的部分关于新人怎么学习嵌入式Linux的经验,引用如下: 1.电脑一开机,那些界面是谁显示的?是BIOS,它做什么?一些自检,然后从硬盘上读入windows,并启动它. 类似的, ...

  2. JS学习笔记 - fgm练习 - 网页换肤

    总结: 1. 点击按钮,div内部变色,边框保持颜色不变. 实现原理:其实本来就把background 和 border 分别设置了同一个颜色,看似是一个整体,其实本来就是分开的. 那么点击的时候,只 ...

  3. PythonOOP面向对象编程2

    编程语言的特征: 继承 封装 多态 如:C++ / Java / Python / Swift / C# inheritance 继承 drived 派生 概念: 继承是指从已有的类中衍生出新类,新类 ...

  4. android 5.x system.img 大于2G导致编译otapackage时报错怎样处理

    当system分区预制过多apk时假设img size超过2G 在make otapackage时会报例如以下错误  zipfile.LargeZipFile: Zipfile size would ...

  5. javascript进阶课程--第三章--匿名函数和闭包

    javascript进阶课程--第三章--匿名函数和闭包 一.总结 二.学习要点 掌握匿名函数和闭包的应用 三.匿名函数和闭包 匿名函数 没有函数名字的函数 单独的匿名函数是无法运行和调用的 可以把匿 ...

  6. JavaScript系列--JavaScript数组高阶函数reduce()方法详解及奇淫技巧

    一.前言 reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值. reduce() 可以作为一个高阶函数,用于函数的 compose. reduce()方 ...

  7. [Ramda] Declaratively Map Predicates to Object Properties Using Ramda where

    Sometimes you need to filter an array of objects or perform other conditional logic based on a combi ...

  8. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

  9. vs2008,2010,2012安装包下载

    近期在csdn学院当老师啦.把自己以学到的东西总结一下,录个视频给大家,也当发一下福利.这些以后都是自己无形的財产.哈哈. 安装与下载编程工具 Vs2008下载地址:http://pan.baidu. ...

  10. php 面试题一(看视频的学习量比网上瞎转悠要清晰和明了很多)(看视频做好笔记)(注重复习)

    php 面试题一(看视频的学习量比网上瞎转悠要清晰和明了很多)(看视频做好笔记)(注重复习) 一.总结 1.无线分类的本质是树(数据结构)(数的话有多种储存结构可以实现,所以对应的算法也有很多),想到 ...