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
*/
### Codeforces Problem 1014D 解答与解释 当前问题并未提供关于 **Codeforces Problem 1014D** 的具体描述或相关背景信息。然而,基于常见的竞赛编程问题模式以及可能涉及的主题领域(如数据结构、算法优化等),可以推测该问题可能属于以下类别之一: #### 可能的解法方向 如果假设此问题是典型的计算几何或者图论类题目,则通常会涉及到如下知识点: - 图遍历(DFS 或 BFS) - 贪心策略的应用 - 动态规划的状态转移方程设计 由于未给出具体的输入输出样例和约束条件,这里无法直接针对Problem 1014D 提供精确解答。但是可以根据一般性的解决思路来探讨潜在的方法。 对于类似的复杂度较高的题目,在实现过程中需要注意边界情况处理得当,并且要充分考虑时间效率的要求[^5]。 以下是伪代码框架的一个简单例子用于说明如何构建解决方案逻辑流程: ```python def solve_problem(input_data): n, m = map(int, input().split()) # 初始化必要的变量或数组 graph = [[] for _ in range(n)] # 构建邻接表或其他形式的数据表示方法 for i in range(m): u, v = map(int, input().split()) graph[u].append(v) result = [] # 执行核心算法部分 (比如 DFS/BFS 遍历) visited = [False]*n def dfs(node): if not visited[node]: visited[node] = True for neighbor in graph[node]: dfs(neighbor) result.append(node) for node in range(n): dfs(node) return reversed(result) ``` 上述代码仅为示意用途,实际应用需依据具体题目调整细节参数设置及其功能模块定义[^6]。 #### 关键点总结 - 明确理解题意至关重要,尤其是关注特殊测试用例的设计意图。 - 对于大规模数据集操作时应优先选用高效的时间空间性能表现良好的技术手段。 - 结合实例验证理论推导过程中的每一步骤是否合理有效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值