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*/
0 0
原创粉丝点击