CF #368 D Persistent Bookcase 操作树~?
来源:互联网 发布:淘宝代销经营地址 编辑:程序博客网 时间:2024/06/06 00:26
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.
一个n*m书架,
4种操作,1,2单点修改;3整行修改;4 撤回操作;
每次操作求剩余书数量。
这是新学的(不知道叫啥);
下面是题解连接,讲的真心好
http://blog.csdn.net/queuelovestack/article/details/52269321
粘代码
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;void read(int &a){ a=0; char c=getchar(); while(c<'0'||c>'9') c=getchar(); while(c>='0'&&c<='9') { a*=10; a+=c-'0'; c=getchar(); }}struct quiry{ int op,x,y,ans;}q[100005];struct bian{ int to;}b[100005];int fst[100005],nxt[100005];int tot=0;void build(int f,int t){ b[++tot].to=t; nxt[tot]=fst[f]; fst[f]=tot;}int n,m,Q;int a[1005][1005];int ans=0;void dfs(int x){ int i=x; int oq=-1,X,Y; if(q[i].op==1) { if(!a[q[i].x][q[i].y]) { a[q[i].x][q[i].y]++; ans++;oq=0; X=q[i].x;Y=q[i].y; } } if(q[i].op==2) { if(a[q[i].x][q[i].y]) { a[q[i].x][q[i].y]--; ans--;oq=0; X=q[i].x;Y=q[i].y; } } if(q[i].op==3) { for(int j=1;j<=m;j++) { if(a[q[i].x][j]) { a[q[i].x][j]--; ans--; } else { a[q[i].x][j]++; ans++; } } oq=q[i].x; } q[i].ans=ans; int v; for(int j=fst[x];j;j=nxt[j]) { v=b[j].to; dfs(v); } if(oq>=0) { if(oq>0) { for(int j=1;j<=m;j++) { if(a[oq][j]) { a[oq][j]--; ans--; } else { a[oq][j]++; ans++; } } } else { if(a[X][Y]) { a[X][Y]--; ans--; } else { a[X][Y]++; ans++; } } }}int main(){ scanf("%d%d%d",&n,&m,&Q); for(int i=1;i<=Q;i++) { scanf("%d",&q[i].op); if(q[i].op<=2) { read(q[i].x);read(q[i].y); build(i-1,i); } else { read(q[i].x); if(q[i].op==3)build(i-1,i); else build(q[i].x,i); } } dfs(0); for(int i=1;i<=Q;i++) printf("%d\n",q[i].ans);}
- CF #368 D Persistent Bookcase 操作树~?
- 树(Persistent Bookcase,cf 707d)
- CF368 D - Persistent Bookcase
- CodeForces 707D Persistent Bookcase (操作建树DFS|主席树+主席树)
- codeforces 707D Persistent Bookcase
- CodeForces 707D - Persistent Bookcase
- codeforces--707D. Persistent Bookcase
- Codeforces Round #368-D. Persistent Bookcase-(离线建树+暴力xjb搞)/(主席树在线乱搞)
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase【可持久化线段树,区间取反
- Codeforces Round #368 (Div. 2)D Persistent Bookcase
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase(离线)
- Codeforces Round #368 (Div. 2) [D] Persistent Bookcase
- Codeforces Round #368 (Div. 2) D - Persistent Bookcase
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase(dfs)
- 【离线】【深搜】【树】Codeforces 707D Persistent Bookcase
- [可持久化线段树] codeforces 707D. Persistent Bookcase
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase(n层m个的书架的四种操作)
- Android并发-Future,FutureTask和Callable及AsyncTask
- Android的缓存技术:LruCache和DiskLruCache
- Codeforces 705B.Spider Man
- pip安装lxml时报错
- Find the Difference —— leetcode
- CF #368 D Persistent Bookcase 操作树~?
- 于堆栈的(Stack-based ) 和基于寄存器(Register-based) 的虚拟机区别
- 值传递引用传递及指针传递
- Windows核心编程 第五章 作业(下)
- 筛选法找素数算法的一点改进
- 机房收费系统(一)登录
- Java - 华为机试训练 - 密码验证合格程序(正则表达式判断)
- py-faster-rcnn源码解读系列(五)——stage1_rpn_train.pt
- 机房收费系统(四)-上下机总结