CodeForces 707D Persistent Bookcase (操作建樹DFS|主席樹+主席樹)
題意:
給出一個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 200002
int 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 200005
struct 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 15
3 4
1 1 1
1 5 1
1 6 1
2 6 1
4 3
2 10 1
1 9 1
3 1
2 1 1
2 5 1
2 13 1
1 1 1
1 14 1
2 14 1
19 16 14
3 10
3 8
3 3
2 5 3
1 7 15
4 0
2 11 2
1 16 3
1 16 3
3 13
1 13 3
4 9
1 5 11
3 1
*/