Describe:
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 ≤ 1e3, 1 ≤ q ≤ 1e5) — 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
sample output:
1
4
0
题意:
n个书架,每个书架有m本书,四种操作:
操作1:在第i个书架第j个位置放书(如果已经有书就不管)
操作2:在第i个书架第j个位置移除书(如果没书就不管)
操作3:把第i个书架有书的地方变没书,没书的地方变有书
操作4:恢复到第k个操作之后的状态
每次操作之后需要知道当前书的总数。
题解:
前三个操作很容易用线段树实现,关键是第4个操作,这需要保存以前有过的所有状态,自然的想到可持久化的数据结构。
这里把每个书架封装成结构,数组p代表书架上书的状态,val代表书架书的数目,r代表书架是否为翻转状态。然后建一个线段树用来可持久化,叶子节点代表一个书架,非叶子结点保存书的数目。然后支持以上三种操作即可。
题目链接
ac代码:
#include<bits/stdc++.h>
#define mid ((l+r)>>1)
#define lson ls[rt], l, mid
#define rson rs[rt], mid+1, r
#define ll long long
using namespace std;
const int maxn = 1e3 + 50;
int n, m ,q;
struct node{
int p[maxn];
int r;
int val;
}e[maxn*100];
int T[maxn*101];
int sum[2000*maxn], ls[maxn*2000], rs[maxn*2000];
int pos;
int tot = 0;
int cnt = 0;
void build(int pre, int cur, int l, int r, int op, int x){
ls[cur] = ls[pre];
rs[cur] = rs[pre];
sum[cur] = sum[pre];
if(l == r){
sum[cur] = ++cnt;
for(int i = 1; i <= m; ++i) e[sum[cur]].p[i] = e[sum[pre]].p[i];
e[sum[cur]].r = e[sum[pre]].r;
e[sum[cur]].val = e[sum[pre]].val;
if(op == 1){//op1
if(e[sum[cur]].r == 0){//没翻转过
if(e[sum[cur]].p[pos] == 0){
e[sum[cur]].p[pos] = 1;
e[sum[cur]].val++;
}
}
else{//翻转过
if(e[sum[cur]].p[pos] == 1){
e[sum[cur]].p[pos] = 0;
e[sum[cur]].val++;
}
}
}
else if(op == 2){
if(e[sum[cur]].r == 0){//没翻转过
if(e[sum[cur]].p[pos] == 1){
e[sum[cur]].p[pos] = 0;
e[sum[cur]].val--;
}
}
else{//翻转过
if(e[sum[cur]].p[pos] == 0){
e[sum[cur]].p[pos] = 1;
e[sum[cur]].val--;
}
}
}
else{
e[sum[cur]].r ^= 1;
e[sum[cur]].val = m - e[sum[cur]].val;
}
return;
}
if(x <= mid) build(ls[pre], ls[cur] = ++tot, l, mid, op, x);
else build(rs[pre], rs[cur] = ++tot, mid+1, r, op, x);
int s1, s2;
if(mid-l+1 == 1) s1 = e[sum[ls[cur]]].val;
else s1 = sum[ls[cur]];
if(r-mid == 1) s2 = e[sum[rs[cur]]].val;
else s2 = sum[rs[cur]];
sum[cur] = s1 + s2;
return;
}
int main()
{
scanf("%d%d%d",&n, &m, &q);
for(int i = 1; i <= q; ++i){
int op, x;
scanf("%d%d",&op, &x);
if(op < 3) scanf("%d", &pos);
if(op < 4) build(T[i-1], T[i] = ++tot, 1, n, op, x);
else T[i] = T[x];
if(n > 1) printf("%d\n",sum[T[i]]);
else printf("%d\n", e[sum[T[i]]].val);
}
}