CF #368 D Persistent Bookcase 操作树~?
2016-09-04 20:32
330 查看
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
粘代码
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); }
相关文章推荐
- Find the Difference —— leetcode
- 寻找徐峥小游戏
- 面试题-多线程编程
- 无密码通过ssh执行rsync
- Android的缓存技术:LruCache和DiskLruCache
- Android进程保活
- Java爬虫——抓取静态网页
- 编写SQL语句操作数据库
- Windows核心编程 第五章 作业(上)
- 虚拟机下centos6.5系统输入ifconfig命令不能显示ip地址
- Javascript之对象组合继承
- Java学习总结记录(2)——JVM、JRE和JDK
- 使用 xcodebuild 命令导出 ipa 文件
- Codeforces Round #353 (Div. 2) E. Trains and Statistic (线段树+dp)
- 图片预加载
- noip模拟赛(一)宠物之战
- noip模拟赛(一)宠物之战
- 代码片段 - Golang 实现集合操作
- mysql常用命令
- Lightoj 1178