CodeForces 707D Persistent Bookcase (操作建树DFS|主席树+主席树)

23 篇文章 0 订阅
6 篇文章 0 订阅

题意:
给出一个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
*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值