此文章可以使用目录功能哟↑(点击上方[+])
比赛链接→Codeforces Round #368 (Div. 2)
Codeforces Problem 707D Persistent Bookcase
Accept: 0 Submit: 0
Time Limit: 2 seconds Memory Limit : 512 megabytes
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?≤?10^3, 1?≤?q?≤?10^5) — 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
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
4
0
2
1
3
3
2
4
2
1
Hint
This image illustrates the second sample case.
Problem Idea
解题思路:
【题意】
n层(标记1~n)书架,每层有m个位置(标记1~m),现有以下四种操作:
①1 i j:在书架的第i层第j个位置放置一本书(若此处原本没有书);
②2 i j:取走书架的第i层第j个位置的书(若此处原本有书);
③3 i:将书架第i层每个位置作如下处理,若该位置有书,则拿走;若没有书,则放置一本书;
④4 k:将书架的状态恢复至第k次操作之后的状态,若k=0,则恢复初始状态,即书架上没有一本书
问每次操作之后,书架上有多少本书
【类型】
dfs+bitset
【分析】
我们首先要理清的问题是操作④该如何实现?
为什么只考虑操作④呢?因为操作①、②、③实现比较简单,即便是最暴力的遍历做法,一次操作也只有O(m)的时间复杂度
但操作④不同,因为它要回到之前的状态,如果我们还是暴力重新求解前k次操作之后书架的状态,显然时间复杂度上就很不可取
那我们应该如何避免这重复累赘的操作呢?比如如下测试数据:
7 17 11
3 2
2 5 7
2 7 17
1 5 10
4 0
3 7
4 1
1 1 6
4 3
3 6
4 0
我们可以根据上述数据获得如下状态图
观察以上状态图,我们可以发现,在执行操作④之后,书架的状态其实已经回到了之前的某种状态k
那么,如果我们从状态k出发,直接去推算出所有经操作④会回到状态k的状态,那么状态k之前的那些状态就不需要重复计算了
也就是说将上述状态图的表示操作④的那条边去掉,可得新的状态图
显然,这种状态图就省去了重复累赘的计算,进而变成了一棵树的形式
那此时,我们只需要深度优先遍历这棵树,就能获得每一状态书架上书的数量了
当然,当一条路走到底之后,也就要进行回溯,回溯的过程要把书架状态往前恢复
具体看代码,有不理解的地方再提出
【时间复杂度&&优化】
O(mq)
题目链接→Codeforces Problem 707D Persistent Bookcase
Source Code
/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 1001;
const int M = 100005;
const int inf = 1000000007;
const int mod = 1000000007;
vector<int> v[M];
bitset<N> b[N],c;
int op[M],l[M],r[M],ans[M];
void dfs(int x)
{int i;if(!x)//初始状态,即书架为空for(i=0;i<v[x].size();i++)dfs(v[x][i]);if(op[x]==1)//1 i j{bool flag=false;if(b[l[x]].test(r[x]))//书架第i层第j个位置有书flag=true;else//书架第i层第j个位置没有书b[l[x]].set(r[x]),ans[x]++;//在书架第i层第j个位置放置一本书for(i=0;i<v[x].size();i++)ans[v[x][i]]=ans[x],dfs(v[x][i]);//下一状态if(!flag)//当前状态之前,书架第i层第j个位置没有书b[l[x]].reset(r[x]);//恢复之前状态,将放在此处的书拿走}if(op[x]==2)//2 i j{bool flag=true;if(b[l[x]].test(r[x]))//书架第i层第j个位置有书b[l[x]].reset(r[x]),ans[x]--;//取走书架第i层第j个位置的书else//书架第i层第j个位置没有书flag=false;for(i=0;i<v[x].size();i++)ans[v[x][i]]=ans[x],dfs(v[x][i]);//下一状态if(flag)//当前状态之前,书架第i层第j个位置有书b[l[x]].set(r[x]);//恢复之前状态,将拿走的书放回去}if(op[x]==3)//3 i{ans[x]-=b[l[x]].count();b[l[x]]^=c;//按位取反ans[x]+=b[l[x]].count();for(i=0;i<v[x].size();i++)ans[v[x][i]]=ans[x],dfs(v[x][i]);b[l[x]]^=c;}if(op[x]==4)//4 k{for(i=0;i<v[x].size();i++)ans[v[x][i]]=ans[x],dfs(v[x][i]);}
}
int main()
{int n,m,q,i;scanf("%d%d%d",&n,&m,&q);for(i=1;i<=m;i++)c.set(i);for(i=1;i<=q;i++){scanf("%d",&op[i]);if(op[i]<3)scanf("%d%d",&l[i],&r[i]);elsescanf("%d",&l[i]);//记录当前状态的下一状态if(op[i]<4)v[i-1].push_back(i);elsev[l[i]].push_back(i);}dfs(0);for(i=1;i<=q;i++)printf("%d\n",ans[i]);return 0;
}
菜鸟成长记