http://codeforces.com/contest/707/problem/D

先说一下离线和在线:在线的意思就是每一个询问单独处理复杂度O(多少多少),离线是指将所有的可能的询问先一次都处理出来,最后对于每个询问O(1)回答。

然后说一下cf的这题:

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
Note

This image illustrates the second sample case.

题目大意:给你一个n*m的书架,刚开始书架上面都是空的。然后有如下四种操作

第一种:放一本书在[i][j]这个位置,如果这里有书的话,那就不用放了。

第二种:拿走[i][j]这个位置的书,如果没有书的话,那就不用拿了。

第三种:将第i行取亦或。

第四种:回到第i步操作的状态

有q个询问,输出该询问下目前书架上还有几本书?

思路:一次性全部取出来,然后建图进行dfs。所以这里的复杂度是O(q)

 //看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
const int maxn = + ;
const int maxq = + ;
bitset<maxn> bite[maxn];
bitset<maxn> tmp;
int n, m, q;
int a[maxq], b[maxq], op[maxq], ans[maxq];
vector<int> E[maxq]; void dfs(int u){
//printf("u = %d\n", u);
int len = E[u].size();
if (u == ){
ans[u] = ;
for (int i = ; i < len; i++){
ans[E[u][i]] = ans[u];
dfs(E[u][i]);
}
}
else if (op[u] == ){
bool flag = false;
if (!bite[a[u]][b[u]]) flag = true, bite[a[u]][b[u]] = , ans[u] += ;
for (int i = ; i < len; i++){
ans[E[u][i]] = ans[u];
dfs(E[u][i]);
}
if (flag) bite[a[u]][b[u]] = ;
}
else if (op[u] == ){
bool flag = false;
if (bite[a[u]][b[u]]) flag = true, bite[a[u]][b[u]] = , ans[u] -= ;
for (int i = ; i < len; i++){
ans[E[u][i]] = ans[u];
dfs(E[u][i]);
}
if (flag) bite[a[u]][b[u]] = ;
}
else if (op[u] == ){
ans[u] = ans[u] + m - bite[a[u]].count() - bite[a[u]].count();
bite[a[u]] ^= tmp;
for (int i = ; i < len; i++){
ans[E[u][i]] = ans[u];
dfs(E[u][i]);
}
bite[a[u]] ^= tmp;
}
else if (op[u] == ){
for (int i = ; i < len; i++){
ans[E[u][i]] = ans[u];
dfs(E[u][i]);
}
}
} int main(){
cin >> n >> m >> q;
for (int i = ; i <= m; i++)
tmp[i] = ;
for (int i = ; i <= q; i++){
scanf("%d", op + i);
if (op[i] == || op[i] == ){
scanf("%d%d", a + i, b + i);//表示摆放的位置
}
if (op[i] == ){
scanf("%d", a + i);//表示第i行取反
}
if (op[i] == ){
scanf("%d", a + i);
E[a[i]].pb(i);
}
else
E[i - ].pb(i);///让每个询问之间相连
}
dfs();
for (int i = ; i <= q; i++){
printf("%d\n", ans[i]);
}
return ;
}

离线dfs CF div2 707 D的更多相关文章

  1. lca 欧拉序+rmq(st) 欧拉序+rmq(线段树) 离线dfs 倍增

    https://www.luogu.org/problemnew/show/P3379 1.欧拉序+rmq(st) /* 在这里,对于一个数,选择最左边的 选择任意一个都可以,[left_index, ...

  2. cf707D. Persistent Bookcase(离线+dfs)

    题目链接:http://codeforces.com/problemset/problem/707/D  有一个n*m的书架,有K个操作,求每个操作后一共有多少本书:有4种操作: 1:x y 如果 x ...

  3. cf div2 234 D

    D. Dima and Bacteria time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  4. poj 1986LCA离线dfs+并查集

    题意,给出边和权值,求出两个点间的最短距离. 用离线算法的时候有个地方不知道怎么处理了.在线的本来想用倍增的,但发现倍增算法貌似需要预处理深度而不是权值,不知道怎么处理.套一个rmq的模板吧,用来处理 ...

  5. 牛客小白月赛12 H 华华和月月种树 (离线dfs序+线段树)

    链接:https://ac.nowcoder.com/acm/contest/392/H 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 131072K,其他语言2621 ...

  6. cf div2 239 D

    D. Long Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  7. cf div2 236 D

    D. Upgrading Array time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. cf div2 237 D

    D. Minesweeper 1D time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...

  9. cf div2 238 D

    D. Toy Sum time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

随机推荐

  1. 《JavaScript高级程序设计》读书笔记 ---操作符一

    一元操作符只能操作一个值的操作符叫做一元操作符.一元操作符是ECMAScript 中最简单的操作符. 1. 递增和递减操作符递增和递减操作符直接借鉴自C,而且各有两个版本:前置型和后置型.顾名思义,前 ...

  2. github 上传项目基本步骤

    说来也惭愧,我是最近开始用github,小白一个,昨天研究了一个下午.终于可以上传了,所以今天写点,一来分享是自己的一些经验,二来也是做个记录,万一哪天又不记得了:) 废话不多说,直接来,这次主要介绍 ...

  3. Hibernate的查询,二级缓存,连接池

    Hibernate的查询,二级缓存,连接池 1.Hibernate查询数据 Hibernate中的查询方法有5中: 1.1.Get/Load主键查询 使用get或者load方法来查询,两者之间的区别在 ...

  4. 【转】使用DirectUI技术实现QQ界面

    转自http://bbs.csdn.net/topics/350023031 一.介绍 DirectUI技术说白了就是XML配置文件+图片+JavaScript控制界面.这点与网页css+图片+Jav ...

  5. loadunner使用socket协议来实现多客户端连接同一服务器脚本(使用到IP欺骗技术)

    第一部分: #include "lrs.h" vuser_init(){ lrs_startup(257); return 0;} 第二部分: Action(){ char *Re ...

  6. H5的新应用-在地图上标识附近加油站的地址

    ------------------------ <style type="text/css">          html{height:100%}         ...

  7. MySQL安装--ubuntu

    1. 执行命令: $ apt-get install mysql-server 执行上述命令会自动安装mysql-client,mysql-common等包. 2. 安装过程中会让你给root账号设置 ...

  8. opencv 一堆算法,图像处理等

    http://blog.csdn.net/wangzhebupt/article/category/1675453 数据挖掘十大经典实用算法及OpenCV算法 http://www.xuebuyuan ...

  9. hdu_2665_Kth number(主席树)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2665 题意:给你一个区间,让你找这个区间第K大的数 题解:主席树模版题,也可以用划分树 #includ ...

  10. Ansible9:条件语句【转】

    在有的时候play的结果依赖于变量.fact或者是前一个任务的执行结果,从而需要使用到条件语句. 一.when    有的时候在特定的主机需要跳过特定的步骤,例如在安装包的时候,需要指定主机的操作系统 ...