CF707D Persistent Bookcase

洛谷评测传送门

题目描述

Recently in school Alina has learned what are the persistent data structures: they are data structures that always preserves the previous version of itself and access to it when it is modified.

After reaching home Alina decided to invent her own persistent data structure. Inventing didn't take long: there is a bookcase right behind her bed. Alina thinks that the bookcase is a good choice for a persistent data structure. Initially the bookcase is empty, thus there is no book at any position at any shelf.

The bookcase consists of nn shelves, and each shelf has exactly mm positions for books at it. Alina enumerates shelves by integers from 11 to nn and positions at shelves — from 11 to mm . Initially the bookcase is empty, thus there is no book at any position at any shelf in it.

Alina wrote down qq operations, which will be consecutively applied to the bookcase. Each of the operations has one of four types:

  • 11 ii jj — Place a book at position jj at shelf ii if there is no book at it.
  • 22 ii jj — Remove the book from position jj at shelf ii if there is a book at it.
  • 33 ii — Invert book placing at shelf ii . This means that from every position at shelf ii which has a book at it, the book should be removed, and at every position at shelf ii which has not book at it, a book should be placed.
  • 44 kk — Return the books in the bookcase in a state they were after applying kk -th operation. In particular, k=0k=0 means that the bookcase should be in initial state, thus every book in the bookcase should be removed from its position.

After applying each of operation Alina is interested in the number of books in the bookcase. Alina got 'A' in the school and had no problem finding this values. Will you do so?

输入格式

The first line of the input contains three integers nn , mm and qq ( 1<=n,m<=10^{3}1<=n,m<=103 , 1<=q<=10^{5}1<=q<=105 ) — the bookcase dimensions and the number of operations respectively.

The next qq lines describes operations in chronological order — ii -th of them describes ii -th operation in one of the four formats described in the statement.

It is guaranteed that shelf indices and position indices are correct, and in each of fourth-type operation the number kk corresponds to some operation before it or equals to 00 .

输出格式

For each operation, print the number of books in the bookcase after applying it in a separate line. The answers should be printed in chronological order.

题意翻译

题目大意

维护一个二维零一矩阵(n,m<=1000),支持四种操作(不超过105次):

  • 将(i,j)置零
  • 将(i,j)置一
  • 将第i行零一反转
  • 回到第K次操作前的状态
  • 每次操作后输出全局一共有多少个一

输入输出样例

输入 #1复制

输出 #1复制

输入 #2复制

输出 #2复制

输入 #3复制

输出 #3复制

说明/提示

This image illustrates the second sample case.

题解:

2019.10.22模拟赛T2 50分场感谢出题人@zcs0724

PS:洛谷题目翻译有锅,操作1是将\((i,j)\)置\(1\),操作2是将\((i,j)\)置\(0\)。如果不看\(CF\)原题的小伙伴肯定会在这里挂掉,请求管理修改。

考场上一看,操作\(1,2,3\)都会,但是操作\(4\)毫无思路。所以这题挂了,还好出题人良心,有\(50\%\)的数据只有操作\(1,2,3\),所以让蒟蒻侥幸骗得\(50pts\)。

这道题的难点就是操作4的处理。对于操作4,我们发现是对其历史的一个回溯,那么就想到了做法1:可持久化数据结构。

但是蒟蒻并不会可持久化数据结构,所以只学了解法2:离线+操作树

一开始操作树这么高端的名词把蒟蒻搞蒙了。(难道,这是比可持久化树套树更难的知识点?瑟瑟发抖)但是后来仔细地揣摩一下思路,发现就是把先离线,输入进来所有询问,然后把操作建成一棵树,在树上进行处理(本题是深搜)的过程。

为什么这么做是对的呢?因为我们的操作4回溯到的是一个操作和其对应的状态。那么,用一个树状的数据结构来存操作次数显然是满足要求的,就拿样例1来举例:

当我们好好地进行1、2操作的时候,第三次操作4回到了操作0(即没有任何操作的情况) ,所以我们依据题意建立了这样一个操作树。

为什么我们在树上进行深搜对答案没有影响、能维护答案的正确性呢?这是深搜的性质决定的。学过深搜的小伙伴知道深搜有"搜索"和“还原现场”两大核心操作。搜索能帮我们得出每一个操作的答案,而还原现场就能保证这些答案互不影响。那么我们离线处理询问构建了这个操作树,这个树上的每个操作只会是操作1、操作2、操作3(操作4就是建这棵树)。那么我们就可以依据题意进行模拟了。

当然,在这里还为大家介绍简洁地解决这个题模拟部分的一个技巧:\(bitset\)容器。这是一种能够方便地处理01串操作类型题的\(STL\)大法。本蒟蒻也是用这个东西完成的对操作1、2、3的模拟。如果对\(bitset\)不太熟悉的小伙伴请移步本蒟蒻的这篇博客:

浅谈C++bitset容器

代码如下:

#include<cstdio>
#include<bitset>
using namespace std;
const int maxn=1010;
const int maxq=1e5+10;
int n,m,q;
int ans[maxq],opt[maxq],x[maxq],y[maxq],cnt;
bitset<maxn> map[maxn],all_one;
int tot,head[maxq],nxt[maxq],to[maxq];
void add(int x,int y)
{
to[++tot]=y;
nxt[tot]=head[x];
head[x]=tot;
}
void dfs(int pos)
{
bool flag=0;
if(opt[pos]==1 && map[x[pos]][y[pos]]==0)
{
flag=1;
map[x[pos]][y[pos]]=1;
cnt++;
}
if(opt[pos]==2 && map[x[pos]][y[pos]]==1)
{
flag=1;
map[x[pos]][y[pos]]=0;
cnt--;
}
if(opt[pos]==3)
{
flag=1;
cnt-=map[x[pos]].count();
map[x[pos]]^=all_one;
cnt+=map[x[pos]].count();
}
ans[pos]=cnt;
for(int i=head[pos];i;i=nxt[i])
dfs(to[i]);
if(flag)//flag就是还原现场的操作,如果某一个节点是操作4,那么直接继续就可以
{
if(opt[pos]==1)
cnt--,map[x[pos]][y[pos]]=0;
if(opt[pos]==2)
cnt++,map[x[pos]][y[pos]]=1;
if(opt[pos]==3)
{
cnt-=map[x[pos]].count();
map[x[pos]]^=all_one;
cnt+=map[x[pos]].count();//数1,取反,统计答案
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&q);
for(int i=1;i<=m;i++)
all_one.set(i);//构造一个全1的数列,为操作三所用
for(int i=1;i<=q;i++)
{
scanf("%d%d",&opt[i],&x[i]);
if(opt[i]==4)
add(x[i],i);
else
if(opt[i]<3)
scanf("%d",&y[i]);
if(opt[i]<4)
add(i-1,i);
}
dfs(0);
for(int i=1;i<=q;i++)
printf("%d\n",ans[i]);
return 0;
}

CF707D Persistent Bookcase的更多相关文章

  1. cf707D. Persistent Bookcase(离线+dfs)

    题目链接:http://codeforces.com/problemset/problem/707/D  有一个n*m的书架,有K个操作,求每个操作后一共有多少本书:有4种操作: 1:x y 如果 x ...

  2. CF707D Persistent Bookcase 可持久化线段树

    维护一个二维零一矩阵(n,m<=1000),支持四种操作(不超过10^5次): 将(i,j)置一 将(i,j)置零 将第i行零一反转yu 回到第K次操作前的状态 每次操作后输出全局一共有多少个一 ...

  3. CodeForces #368 div2 D Persistent Bookcase DFS

    题目链接:D Persistent Bookcase 题意:有一个n*m的书架,开始是空的,现在有k种操作: 1 x y 这个位置如果没书,放书. 2 x y 这个位置如果有书,拿走. 3 x 反转这 ...

  4. 【Codeforces-707D】Persistent Bookcase DFS + 线段树

    D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...

  5. Codeforces Round #368 (Div. 2) D. Persistent Bookcase

    Persistent Bookcase Problem Description: Recently in school Alina has learned what are the persisten ...

  6. Persistent Bookcase

    Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input standard ...

  7. Codeforces Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力

    D. Persistent Bookcase 题目连接: http://www.codeforces.com/contest/707/problem/D Description Recently in ...

  8. codeforces 707D D. Persistent Bookcase(dfs)

    题目链接: D. Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input ...

  9. [CF707D]Persistent Bookcase_主席树_bitset

    Persistent Bookcase 题目链接:http://codeforces.com/contest/707/problem/D 注释:略. 题解: 发现虽然$q\le 10^5$但是网格是$ ...

随机推荐

  1. python直接赋值、浅拷贝、深拷贝的区别

    一:直接赋值 赋值,就是对象的引用,给对象起别名. i = 8j = iprint("值是:",i, "地址:",id(i))print("值是:&q ...

  2. <Stack> (高频)394 ( 高频)224

    394. Decode String 四种情况: 1. 数字,把之前有的数字乘以10再加本数字 2. ' [ ', 入口, 把之前的数字压入栈中并num归零. 3. ' ] ' ,出口,归零.用dfs ...

  3. golang+vscode开发环境的搭建

    一.windows下安装golang开发环境 (1)配置GOROOT变量,在系统变量中点击新建,变量值是golang安装文件夹目录 (2)配置Path变量,选中Path点编辑即可,在变量值后面追加;% ...

  4. 'index.js' does not match the corresponding name on disk: '.\node_modules\

    跟着视频教程学习react的时候,遇到一个问题,困扰了一阵,最后发现真的很傻! 问题如下: 最后发现是import语句写的有问题,把react写成了React: 正确的引入语句是: import Re ...

  5. ReactNative: 使用Text文本组件

    一.简言 初学RN,一切皆新.Text组件主要用于显示文本,Text组件的重要性不言而喻,无论是Web开发还是客户端开发,都离不开它.它具有响应特性,也即表现为当它被触摸时是否显示为高亮状态.在Web ...

  6. Mybatis和Hibernate框架的区别

    Mybatis和Hibernate框架的区别1 简单简介 1.1 Hibernate 框架 Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,建立对象与数据 ...

  7. 还在担心网聊相亲的小姐姐,美女变恐龙!Python帮你"潜伏"侦查

    ​ 最近,小编的一个朋友很是苦恼,他在Python交流的群里,认识了一个妹子,看头像感觉挺不错的,大家都喜欢摄影,蛮谈得来的!但是想要约见面却不敢,因为他看过<头号玩家>,深知躲在电脑背后 ...

  8. 最热门的 10 个 Java 微服务框架

    1.Spring Boot Java 构建 Spring 应用程序已经有很长一段时间了,Spring Boot 是 Spring 的一个特定版本,它通过对配置细节的处理,使微服务构建更加简便.创建 S ...

  9. pandas 学习 第2篇:Series -(创建,属性,转换和索引)

    序列(Series)是由一组数据(各种NumPy数据类型),以及一组与之相关的数据标签(索引)组成,序列不要求数据类型是相同的. 序列是一个一维数组,只有一个维度(或称作轴)是行(row),在访问序列 ...

  10. SPA项目开发之CRUD+表单验证

    表单验证 Form组件提供了表单验证的功能,只需要通过 rules 属性传入约定的验证规则, 并将Form-Item的prop属性设置为需校验的字段名即可 <el-form-item label ...