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.

思路:dfs+bitset;

每个状态都是由前面的状态而得到的,但当4操作的时候,这个时候这个状态就是k那个状态,所以本状态可以看成是有k状态得来,所以我们可以建图,确定当前状态的前一个状态

,然后dfs就行了,并且用bitset记录状态。

  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<set>
7 #include<bitset>
8 using namespace std;
9 typedef struct node
10 {
11 int val;
12 int x;
13 int y;
14 } ss;
15 ss ak[100005];
16 bitset<1005>bit[1005],c;
17 vector<int>vec[100005];
18 int ask[100005];
19 bool T[100005];
20 int all;
21 void ou(int s);
22 void dfs(int s);
23 void in(int s);
24 int main(void)
25 {
26 int i,j;
27 int n,m,k;
28 while(scanf("%d %d %d",&n,&m,&k)!=EOF)
29 {
30 all = 0;
31 memset(T,0,sizeof(T));
32 for(i = 1; i <= m ; i++)
33 {
34 bit[i].reset();
35 c.set(i);
36 }
37 for(i = 0; i < 10005; i++)
38 vec[i].clear();
39 for(i = 1; i <= k; i++)
40 {
41 scanf("%d",&ak[i].val);
42 if(ak[i].val<= 2)
43 {
44 scanf("%d %d",&ak[i].x,&ak[i].y);
45 vec[i-1].push_back(i);
46 }
47 else
48 {
49 scanf("%d",&ak[i].x);
50 if(ak[i].val == 3)
51 {
52 vec[i-1].push_back(i);
53 }
54 else vec[ak[i].x].push_back(i);
55 }
56 }
57 dfs(0);
58 for(i = 1; i <=k ; i++)
59 printf("%d\n",ask[i]);
60 }
61 return 0;
62 }
63 void dfs(int s)
64 {
65 int i,j;
66 for(i = 0; i < vec[s].size() ; i++)
67 {in(vec[s][i]);dfs(vec[s][i]);ou(vec[s][i]);}
68 }
69 void in(int s)
70 {
71 if(ak[s].val == 1)
72 {
73 if(!bit[ak[s].x][ak[s].y])
74 {
75 T[s] = true;
76 bit[ak[s].x].set(ak[s].y);
77 ask[s] = all+1;
78 all++;
79 }
80 else ask[s] = all;
81 }
82 else if(ak[s].val == 2)
83 {
84 if(bit[ak[s].x][ak[s].y])
85 {
86 T[s] = true;
87 bit[ak[s].x].reset(ak[s].y);
88 ask[s] = all-1;
89 all--;
90 }
91 else ask[s] = all;
92 }
93 else if(ak[s].val == 3)
94 {
95 all -=bit[ak[s].x].count();
96 bit[ak[s].x] ^= c;
97 all += bit[ak[s].x].count();
98 ask[s] = all;
99
100 }
101 else ask[s] = all;
102 }
103 void ou(int s)
104 {
105 if(ak[s].val == 1)
106 {
107 if(T[s])
108 {
109 all --;
110 bit[ak[s].x].reset(ak[s].y);
111 }
112 }
113 else if(ak[s].val == 2)
114 {
115 if(T[s])
116 {
117 all++;
118 bit[ak[s].x].set(ak[s].y);
119 }
120 }
121 else if(ak[s].val == 3)
122 {
123 all -=bit[ak[s].x].count();
124 bit[ak[s].x] ^= c;
125 all += bit[ak[s].x].count();
126 }
127 }

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

  1. Codeforces Round #368 (Div. 2)

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

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

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

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

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

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

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

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

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

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

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

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

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

  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. EPOLL原理详解(图文并茂)

    文章核心思想是: 要清晰明白EPOLL为什么性能好. 本文会从网卡接收数据的流程讲起,串联起CPU中断.操作系统进程调度等知识:再一步步分析阻塞接收数据.select到epoll的进化过程:最后探究e ...

  2. Shell 打印文件的最后5行

    目录 Shell 打印文件的最后5行 题解-awk 题解-tail Shell 打印文件的最后5行 经常查看日志的时候,会从文件的末尾往前查看,于是请你写一个 bash脚本以输出一个文本文件 nowc ...

  3. mongDB进阶

    Mongo进阶 聚合 聚合操作将来自多个文档的值组合在一起,并且可以对分组数据执行各种操作以返回单个结果. 文档进入多阶段管道,将文档转换为聚合结果 聚合管道 例子: 第一阶段:过滤,$match 第 ...

  4. C逗号表达式

    c语言提供一种特殊的运算符,逗号运算符,优先级别最低,它将两个及其以上的式子联接起来,从左往右逐个计算表达式,整个表达式的值为最后一个表达式的值.如:(3+5,6+8)称为逗号表达式,其求解过程先表达 ...

  5. oracle 拆分字符串

    WITH t AS (SELECT '1-2-3-4' a FROM dual)SELECT Regexp_Substr(a, '[^-]+', 1, LEVEL) i FROM tCONNECT B ...

  6. Linux服务器---drupal

    Drupal Drupal为用户提供各种工具来管理网站,它可以帮助用户入门,建立自己的网站 1.下载drupal软件(https://www.drupal.org/project/drupal/rel ...

  7. 最小化安装centos ubuntu基础命令

    # yum install vim iotop bc gcc gcc-c++ glibc glibc-devel pcre \ pcre-devel openssl openssl-devel zip ...

  8. Linux运维实战之磁盘分区、格式化及挂载(一)

    在网络系统中,磁盘和文件系统管理是两个非常基本.同时也是非常重要的管理任务,特别是文件系统管理,因为它与用户权限和整个网络系统的安全息息相关.本次博文的主题是关于Linux系统中磁盘分区.格式化及挂载 ...

  9. my39_InnoDB锁机制之Gap Lock、Next-Key Lock、Record Lock解析

    MySQL InnoDB支持三种行锁定方式: 行锁(Record Lock):锁直接加在索引记录上面,锁住的是key. 间隙锁(Gap Lock): 锁定索引记录间隙,确保索引记录的间隙不变.间隙锁是 ...

  10. highchars操作集合

    一.tooltip 与鼠标指针的距离想调整tooltip和鼠标指针的距离,官方api 和中文api中都没写,只有轴 label.distance . 但我觉得应该有这个,看源码果然有 tooltip ...