Persistent Bookcase

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

Sample Input:

2 3 3

1 1 1

3 2

4 0

4 2 6

3 2

2 2 2

3 3

3 2

2 2 2

3 2

2 2 2

3 2

2 2 1

Sample Output:

1

4

0

2

1

3

3

2

4

2

1

一般的dfs都要回溯。

【题目链接】Codeforces 707D

【题目类型】dfs+bitset

&题意:

n层(标记1n)书架,每层有m个位置(标记1m),现有以下四种操作:

①1 i j:在书架的第i层第j个位置放置一本书(若此处原本没有书);

②2 i j:取走书架的第i层第j个位置的书(若此处原本有书);

③3 i:将书架第i层每个位置作如下处理,若该位置有书,则拿走;若没有书,则放置一本书;

④4 k:将书架的状态恢复至第k次操作之后的状态,若k=0,则恢复初始状态,即书架上没有一本书

问每次操作之后,书架上有多少本书

&题解:

首先你要去看看这个博客:博客链接 这上面把思路已经说的很清楚了,我在补充点:

这题是一个离线的题,什么叫离线呢?就是存到数组里处理,之后在输出的题(貌似是这样的- -)然而知道这个并没有什么卵用。这题有两个难点。

1.建树:应该学习下这种建树的方式。vector[i]中i是父亲,后面的是儿子,还要注意是把这一个状态接到父亲那里,所以每次push_back的都是i。并且qu询问数组要从1开始存,因为我们要找一个最大的父亲,是0,他只有儿子,所以要从1开始。

2.dfs:dfs的时候,必须要清楚的知道dfs的参数代表的是什么?我的代码里的x代表的是父亲,循环里的v代表的是儿子,也可以想成x的下一个状态就是v,之后就是处理3种情况了,但为什么没有第4种呢?因为第4种在建树的时候就已经处理了,连上了以前的边,每次dfs的时候都会走到,所以就不用在特判第4种了。

还有,要注意dfs是给ans赋值tot的时候,一定是给ans[v]=tot 而不是ans[x]=tot,这块我调了好长时间才发现的,因为我们求的是第v次询问的答案,而不是第x次的,因为v代表的是这个状态,而x是上个状态。

【时间复杂度】O(n*q)

&代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
#define cle(a,val) memset(a,(val),sizeof(a))
const int MAXN = 1000 + 5 ;
const int MAXQ = 100000 + 5 ;
bitset<MAXN> pos[MAXN];
int qu[MAXQ][3], sum[MAXN], n, m, q;
ll ans[MAXQ];
vector<int>tree[MAXQ];
ll tot;
void dfs(int x) {
for (int i = 0; i < tree[x].size(); i++) {
int v = tree[x][i];
bool did = 0;
if (qu[v][0] == 1 && !pos[qu[v][1]].test(qu[v][2])) {
pos[qu[v][1]].set(qu[v][2]);
sum[qu[v][1]]++;
tot++;
did = 1;
}
if (qu[v][0] == 2 && pos[qu[v][1]].test(qu[v][2])) {
pos[qu[v][1]].reset(qu[v][2]);
sum[qu[v][1]]--;
tot--;
did = 1;
}
if (qu[v][0] == 3) {
int d = sum[qu[v][1]];
sum[qu[v][1]] = m - d;
tot += sum[qu[v][1]] - d;
pos[qu[v][1]].flip();
}
ans[v] = tot;
dfs(v);
if (qu[v][0] == 1 && did) {
pos[qu[v][1]].reset(qu[v][2]);
sum[qu[v][1]]--;
tot--;
}
if (qu[v][0] == 2 && did) {
pos[qu[v][1]].set(qu[v][2]);
sum[qu[v][1]]++;
tot++;
}
if (qu[v][0] == 3) {
int d = sum[qu[v][1]];
sum[qu[v][1]] = m - d;
tot += sum[qu[v][1]] - d;
pos[qu[v][1]].flip();
}
}
}
void Solve() {
while (~scanf("%d %d %d", &n, &m, &q)) {
for (int i = 1; i <= q; i++) {
scanf("%d", &qu[i][0]);
if (qu[i][0] <= 2) {
scanf("%d %d", &qu[i][1], &qu[i][2]);
tree[i - 1].push_back(i);
}
else {
scanf("%d", &qu[i][1]);
if (qu[i][0] == 3) tree[i - 1].push_back(i);
else tree[qu[i][1]].push_back(i);
}
}
tot = 0;
dfs(0);
for (int i = 1; i <= q; i++) {
printf("%I64d\n", ans[i]);
}
}
}
int main() {
Solve();
return 0;
}

Codeforces Round #368 (Div. 2) D. Persistent Bookcase的更多相关文章

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

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

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

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

  3. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  4. Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)

    Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...

  5. Codeforces Round #368 (Div. 2) B. Bakery (模拟)

    Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...

  6. Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)

    Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...

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

  8. Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组 暴力

    E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...

  9. Codeforces Round #368 (Div. 2) C. Pythagorean Triples 数学

    C. Pythagorean Triples 题目连接: http://www.codeforces.com/contest/707/problem/C Description Katya studi ...

随机推荐

  1. 黑马程序员——JAVA基础之简述集合collection

    ------- android培训.java培训.期待与您交流! ---------- 集合: 为什么出现集合类? •  面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就对对 ...

  2. Visual C++ 设置适合自己的解决方案目录结构

    Visual C++ 使用解决方案来管理项目,项目之间还可能有依赖关系,设置适合自己的解决方案目录结构,便于代码的管理.程序的发布. 下面开始一个虚拟解决方案设计:         假设此解决方案有应 ...

  3. tar学习使用心得

    tar如何解压文件到指定的目录?#tar zxvf mysql.tar.gz -C /home/aaa 暂时用到这,所以学到这,日后再更新 近日用tar解压一个文件时发现这样的错误: # tar -z ...

  4. Cycles_per_instruction

    https://en.wikipedia.org/wiki/Cycles_per_instruction

  5. httplib

    可爆破目录 import httplib import urllib def sendhttp(): data = urllib.urlencode({'@number': 12524, '@type ...

  6. ABBYY可以给我们解决那些问题

    不同的行业组织和企业有不同的业务流程和规定,在OCR文字识别领域,ORC文字识别软件ABBYY给各个行业都提供了有效解决方案,满足其特定需求的同时还帮助他们提高业务流程处理效率,降低成本,全球大量的纸 ...

  7. python_Day_02[数组、列表、元组之篇]

    一.对python中.pyc的理解 1).pyc文件可以理解为是python编译好的字节码文件,即只有python解释器才能读懂,类似于java中class文件 2)python运转过程: 当pyth ...

  8. shell之eval-command

    本文将会讲解一些linux中命令的使用与技巧希望对新手给予帮助一 e v a l命令将会首先扫描命令行进行所有的置换,然后再执行该命令.该命令适用于那些一次扫描无法实现其功能的变量.该命令对变量进行两 ...

  9. 无法启动:此实现不是Windows平台FIPS验证的加密算法的一部分

    个别同学可能会在启动订票助手.NET的时候发现这个提示: 出现这个问题的原因是订票助手.NET使用了MD5算法,而系统的组策略安全设置导致无法使用此算法.要修正此问题,请按照如下操作(两种方法任选其一 ...

  10. HadoopDoctor:来自腾讯数据仓库TDW的MR诊断系统

    TDW是基于Hadoop生态圈研发的大数据处理平台,MapReduce计算引擎在TDW平台中承担了所有的离线数据计算,是TDW最重要的底层支撑平台之一.在TDW 平台中,除了MR程序会生成MapRed ...