CodeForces 707D Persistent Bookcase (操作建树DFS|主席树+主席树)
来源:互联网 发布:lnmp php日志 编辑:程序博客网 时间:2024/05/16 09:24
题意:
给出一个n*m的书柜,有四种操作
1 i j — Place a book at position j at shelf i if there is no book at it.
在i行j列放一本书,如果已有书则不放
2 i j — Remove the book from position j at shelf i if there is a book at it.
拿走i行j列的书,如果没有则不拿
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.
将第i行的所有书取反,有变无,无变有
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.
回到第k的操作之后的状态
对于每一次操作后都要求输出当前书柜中总共有多少本书
题解:
解法1:对于所有操作离线,建一个操作树,如果是1,2,3操作,则与前一操作建边,如果是4操作则与返回的版本建边,那么每个结点存的就是当前操作,DFS一下整颗树就能得到答案
解法2:对于每一行建一棵主席树,维护每一列的书本数目,在对所有行建一棵主席树,维护线段树版本,对于1,2操作,直接在外层主席树找到对应的行,在对行中的主席树进行更新,对于3操作,标记翻转tag再进行修改操作,对于4操作,直接更换当前外层主席树版本
解法1:
#include<cstring>#include<string>#include<iostream>#include<queue>#include<cstdio>#include<algorithm>#include<map>#include<cstdlib>#include<cmath>#include<vector>//#pragma comment(linker, "/STACK:1024000000,1024000000");using namespace std;#define INF 0x3f3f3f3f#define maxn 200002int n,m,q;int ans[maxn];int fir[maxn],nex[maxn],v[maxn],e_max;bool g[1004][1004];void init(){ e_max=0; memset(fir,-1,sizeof fir);}void add_edge(int s,int t){ int e=e_max++; v[e]=t; nex[e]=fir[s]; fir[s]=e;}struct Q{ int id; int p; int i,j;}op[maxn];bool update(int pos,int j,int val){ if(val==1) { if(!g[pos][j]) {g[pos][j]=1;return true;} else return false; } else if(val==-1) { if(g[pos][j]) {g[pos][j]=0;return true;} else return false; }}int update1(int pos){ int temp=0; for(int i=1;i<=m;i++) { temp+=g[pos][i]; g[pos][i]^=1; } return (m-temp)-temp;}void dfs(int k,int sum){ for(int i=fir[k];~i;i=nex[i]) { int e=v[i]; if(op[e].p==1) { bool f=update(op[e].i,op[e].j,1); ans[op[e].id]=sum+f; dfs(e,sum+f); if(f) update(op[e].i,op[e].j,-1); } else if(op[e].p==2) { bool f=update(op[e].i,op[e].j,-1); ans[op[e].id]=sum-f; dfs(e,sum-f); if(f) update(op[e].i,op[e].j,1); } else if(op[e].p==3) { int f=update1(op[e].i); ans[op[e].id]=sum+f; dfs(e,sum+f); update1(op[e].i); } else if(op[e].p==4) { ans[op[e].id]=sum; dfs(e,sum); } }}int main(){ while(scanf("%d%d%d",&n,&m,&q)!=EOF) { init(); for(int i=1;i<=q;i++) { op[i].id=i; scanf("%d",&op[i].p); if(op[i].p==1) { scanf("%d%d",&op[i].i,&op[i].j); add_edge(i-1,i); } else if(op[i].p==2) { scanf("%d%d",&op[i].i,&op[i].j); add_edge(i-1,i); } else if(op[i].p==3) { scanf("%d",&op[i].i); add_edge(i-1,i); } else { scanf("%d",&op[i].i); add_edge(op[i].i,i); } } dfs(0,0); for(int i=1;i<=q;i++) { printf("%d\n",ans[i]); } } return 0;}
解法2:
#include<cstring>#include<cstdio>#include<iostream>using namespace std;#define maxn 200005struct node{ int l,r; int sum; int tag; node() { l=r=sum=tag=0; }} t[maxn*50];int n,m,q;int root[maxn],cnt;int update1(int &i,int pre,int j,int l,int r,int kind){ t[i=++cnt]=t[pre]; if(l==r&&r==j) { if(kind==1&&t[i].sum==0) { t[i].sum=1; return 1; } else if(kind==-1&&t[i].sum==1) { t[i].sum=0; return -1; } else return 0; } int mid=l+r>>1; if(j<=mid) update1(t[i].l,t[pre].l,j,l,mid,kind); else update1(t[i].r,t[pre].r,j,mid+1,r,kind);}void update(int &rt,int pre,int pos,int j,int l,int r,int kind){ t[rt=++cnt]=t[pre]; if(l==r&&r==pos) { if(!kind) { t[rt].tag^=1; t[rt].sum=m-t[rt].sum; return ; } if(t[rt].tag&&m!=1) kind=-kind; int f=update1(rt,pre,j,1,m,kind); if(t[rt].tag&&m!=1) f=-f; if(m!=1) t[rt].sum+=f; return ; } int mid=l+r>>1; if(pos<=mid) update(t[rt].l,t[pre].l,pos,j,l,mid,kind); else update(t[rt].r,t[pre].r,pos,j,mid+1,r,kind); t[rt].sum=t[t[rt].l].sum+t[t[rt].r].sum;}int main(){ scanf("%d%d%d",&n,&m,&q); for(int i=1; i<=q; i++) { int op; scanf("%d",&op); if(op==1) { int a,b; scanf("%d%d",&a,&b); update(root[i],root[i-1],a,b,1,n,1); printf("%d\n",t[root[i]].sum); } else if(op==2) { int a,b; scanf("%d%d",&a,&b); update(root[i],root[i-1],a,b,1,n,-1); printf("%d\n",t[root[i]].sum); } else if(op==3) { int a; scanf("%d",&a); update(root[i],root[i-1],a,a,1,n,0); printf("%d\n",t[root[i]].sum); } else { int a; scanf("%d",&a); root[i]=root[a]; printf("%d\n",t[root[i]].sum); } }}/*19 1 153 41 1 11 5 11 6 12 6 14 32 10 11 9 13 12 1 12 5 12 13 11 1 11 14 12 14 119 16 143 103 83 32 5 31 7 154 02 11 21 16 31 16 33 131 13 34 91 5 113 1*/
- CodeForces 707D Persistent Bookcase (操作建树DFS|主席树+主席树)
- Codeforces 707D Persistent Bookcase(离线dfs或在线主席树)
- Codeforces Round #368-D. Persistent Bookcase-(离线建树+暴力xjb搞)/(主席树在线乱搞)
- Codeforces 707D Persistent Bookcase(DFS)
- Codeforces 707D.Persistent Bookcase(离线算法,dfs树,好题!)
- Codeforces 707D Persistent Bookcase(bitset+dfs)★
- codeforces 707 D Persistent Bookcase(dfs+bitset)
- 树(Persistent Bookcase,cf 707d)
- 【离线】【深搜】【树】Codeforces 707D Persistent Bookcase
- [可持久化线段树] codeforces 707D. Persistent Bookcase
- codeforces 707D Persistent Bookcase
- CodeForces 707D - Persistent Bookcase
- codeforces--707D. Persistent Bookcase
- Codeforces Problem 707D Persistent Bookcase(dfs+bitset)
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase(dfs)
- CF #368 D Persistent Bookcase 操作树~?
- Codeforces 840D [主席树]
- codeforces 840D 主席树
- Spark 之 Stage划分、数据本地性算法实现
- 工作的核心
- HDU 2066 SPFA/起点集与终点集
- Centos7中安装中文输入法
- epoll的两种触发模式
- CodeForces 707D Persistent Bookcase (操作建树DFS|主席树+主席树)
- 高逼格"程序员"产品-奇点v1.10新版本上线
- fatal: --author '' is not 'Name <email>' and matches no existing author
- 数字化工厂解决方案——OA办公自动化与ERP
- java设计模式进阶_builder
- javascript, 原型链,javascript 原型链方法,原型链继承,原型链和作用域链
- TestFlight使用帮助文档(非开发人员)
- JSON基础
- Linux下OOM Killer机制详解(一)