P4344 [SHOI2015] 脑洞治疗仪
题目链接P4344 [SHOI2015] 脑洞治疗仪
P4344 [SHOI2015] 脑洞治疗仪
题目描述
曾经发明了自动刷题机的发明家 SHTSC 又公开了他的新发明:脑洞治疗仪——一种可以治疗他因为发明而日益增大的脑洞的神秘装置。
为了简单起见,我们将大脑视作一个 01 序列。
1 0 1 0 0 0 1 1 1 0
脑洞治疗仪修补某一块脑洞的基本工作原理就是将另一块连续区域挖出,将其中正常工作的脑组织填补在这块脑洞中。(所以脑洞治疗仪是脑洞的治疗仪?)
例如,用上面第
1 1 1 1 0 0 1 0 0 0
如果再用第
0 0 0 0 0 0 1 1 1 1
这是因为脑洞治疗仪会把多余出来的脑组织直接扔掉。
如果再用第
1 1 1 1 0 0 0 0 0 0
这是因为如果新脑洞挖出来的脑组织不够多,脑洞治疗仪仅会尽量填补位置比较靠前的脑洞。
假定初始时 SHTSC 并没有脑洞,给出一些挖脑洞和脑洞治疗的操作序列,你需要即时回答 SHTSC 的问题:在大脑某个区间中最大的连续脑洞区域有多大。
输入格式
第一行两个整数
以下
上述区间均在
输出格式
对于每个询问,输出一行一个整数,表示询问区间内最大连续脑洞区域有多大。
输入输出样例 #1
输入 #1
10 10
0 2 2
0 4 6
0 10 10
2 1 10
1 8 10 1 4
2 1 10
1 1 4 8 10
2 1 10
1 7 10 1 6
2 1 10
输出 #1
3
3
6
6
说明/提示
对于
对于
对于
题目思路
存在区间修改和区间查询,很典型的线段树题。
线段树中每个点维护
先考虑合并的情况: 已知
一种很显然的想法是直接取两个区间最大脑洞大小中的较大值,但是仍然忽略了一种情况,即左边出一段, 右边出一段, 中间拼起来的情况。
所以我们需要维护
因此线段树中的每个节点需要维护三个信息: 最长的连续
然后就有一个很常规的建树操作:
//线段树tree数组的类型为node,以下为node的成员函数
void initialize(int x){ //将叶子节点存入线段树
len = 1; lazy = -1;
if(x == 0){
longest_0 = longest_left = longest_right = 1;
count1 = 0;
}else{
longest_0 = longest_left = longest_right = 0;
count1 = 1;
}
}
void merge(node L,node R){
if(L.longest_left == L.len){ // 左边节点对应的区间全部是0
longest_left = L.len + R.longest_left; // 会延续到右边的节点
}else longest_left = L.longest_left;
if(R.longest_right == R.len){ // 右边节点对应的区间全部是0
longest_right = R.len + L.longest_right; // 会延续到左边的节点
}else longest_right = R.longest_right;
//合并longest_0, 三种情况: 只在左边, 只在右边, 跨越两边
longest_0 = max(L.longest_0,max(R.longest_0,L.longest_right+R.longest_left));
len = L.len + R.len;
count1 = L.count1 + R.count1;
lazy = -1;
}
//以下为全局函数
void build(int p,int l,int r){
if(l == r)tree[p].initialize(a[l]); // 叶子节点
else{
int mid = (l+r)/2;
build(lson,l,mid);
build(rson,mid+1,r);
tree[p].merge(tree[lson],tree[rson]);
}
}
再来看题目中的三种操作。
0\quad l\quad r :SHTSC 挖了一个范围为 [l, r] 的脑洞。
区间修改,将
对线段树中懒惰标记的取值进行规定,
于是就有将区间修改为
//以下是node的成员函数,merge见上方
void modify(int x){
if(x == 0){
lazy = 0;
longest_0 = longest_left = longest_right = len;
count1 = 0;
}else{
lazy = 1;
longest_0 = longest_left = longest_right = 0;
count1 = len;
}
}
//以下是全局函数
void pushdown(int p){
if(tree[p].lazy != -1){
tree[lson].modify(tree[p].lazy);
tree[rson].modify(tree[p].lazy);
tree[p].lazy = -1;
}
}
void zero(int p,int l,int r,int zl,int zr){
if(zl<=l && r<=zr){
tree[p].modify(0);
}else{
int mid = (l+r)/2;
pushdown(p);
if(zl <= mid)zero(lson,l,mid,zl,zr); // 需要用到左空间
if(zr >= mid+1)zero(rson,mid+1,r,zl,zr); // 需要用到右空间
tree[p].merge(tree[lson],tree[rson]);
}
}
1\quad l_0\quad r_0\quad l_1\quad r_1 :SHTSC 进行了一次脑洞治疗,用从 l_0 到 r_0 的脑组织修补 l_1 到 r_1 的脑洞。
先统计
再设计一个用来把区间修改为全
代码如下:
void query(int p,int l,int r,int ql,int qr){
if(ql<=l && r<=qr){
ans.merge(ans,tree[p]); // 将ans节点的信息和当前节点的信息合并,然后放到ans节点当中
}else{
int mid = (l+r)/2;
pushdown(p);
if(ql <= mid)query(lson,l,mid,ql,qr); // 需要用到左空间
if(qr >= mid+1)query(rson,mid+1,r,ql,qr); // 需要用到右空间
}
}
void fill(int p,int l,int r,int fl,int fr){
if(count == 0)return; // 材料耗尽,无法继续填充,返回
if(fl<=l && r<=fr){
int need = tree[p].len - tree[p].count1;
if(count >= need){
count -= need;
tree[p].modify(1);
return; // 这一块填充完毕,返回。否则需要继续向下递归,填充更小的部分
}
}
int mid = (l+r)/2;
pushdown(p);
if(fl <= mid)fill(lson,l,mid,fl,fr);
if(fr >= mid+1)fill(rson,mid+1,r,fl,fr);
tree[p].merge(tree[lson],tree[rson]);
}
2\quad l\quad r :SHTSC 询问 [l, r] 区间内最大的脑洞有多大。
直接用
完整代码:
#include<iostream>
using namespace std;
const int N = 2e5+5;
#define lson (p<<1)
#define rson ((p<<1)|1)
struct node{
int longest_0,longest_left,longest_right;
int len; // 区间长度
int lazy; // 懒惰标记,-1表示没有标记,0表示修改为0,1表示修改为1
int count1; // 当前区间有多少个1
void initialize(int x){ //将叶子节点存入线段树
len = 1; lazy = -1;
if(x == 0){
longest_0 = longest_left = longest_right = 1;
count1 = 0;
}else{
longest_0 = longest_left = longest_right = 0;
count1 = 1;
}
}
void merge(node L,node R){
if(L.longest_left == L.len){ // 左边节点对应的区间全部是0
longest_left = L.len + R.longest_left; // 会延续到右边的节点
}else longest_left = L.longest_left;
if(R.longest_right == R.len){ // 右边节点对应的区间全部是0
longest_right = R.len + L.longest_right; // 会延续到左边的节点
}else longest_right = R.longest_right;
//合并longest_0, 三种情况: 只在左边, 只在右边, 跨越两边
longest_0 = max(L.longest_0,max(R.longest_0,L.longest_right+R.longest_left));
len = L.len + R.len;
count1 = L.count1 + R.count1;
lazy = -1;
}
void empty(){
len = 0; lazy = -1; count1 = 0;
longest_0 = longest_left = longest_right = 0;
}
void modify(int x){
if(x == 0){
lazy = 0;
longest_0 = longest_left = longest_right = len;
count1 = 0;
}else{
lazy = 1;
longest_0 = longest_left = longest_right = 0;
count1 = len;
}
}
}tree[N*4]; // 线段树,四倍空间
int n,m,a[N];
void pushdown(int p){
if(tree[p].lazy != -1){
tree[lson].modify(tree[p].lazy);
tree[rson].modify(tree[p].lazy);
tree[p].lazy = -1;
}
}
void build(int p,int l,int r){
if(l == r)tree[p].initialize(a[l]); // 叶子节点
else{
int mid = (l+r)/2;
build(lson,l,mid);
build(rson,mid+1,r);
tree[p].merge(tree[lson],tree[rson]);
}
}
node ans;
void query(int p,int l,int r,int ql,int qr){
if(ql<=l && r<=qr){
ans.merge(ans,tree[p]); // 将ans节点的信息和当前节点的信息合并,然后放到ans节点当中
}else{
int mid = (l+r)/2;
pushdown(p);
if(ql <= mid)query(lson,l,mid,ql,qr); // 需要用到左空间
if(qr >= mid+1)query(rson,mid+1,r,ql,qr); // 需要用到右空间
}
}
void zero(int p,int l,int r,int zl,int zr){
if(zl<=l && r<=zr){
tree[p].modify(0);
}else{
int mid = (l+r)/2;
pushdown(p);
if(zl <= mid)zero(lson,l,mid,zl,zr); // 需要用到左空间
if(zr >= mid+1)zero(rson,mid+1,r,zl,zr); // 需要用到右空间
tree[p].merge(tree[lson],tree[rson]);
}
}
int count = 0; // 消耗材料
void fill(int p,int l,int r,int fl,int fr){
if(count == 0)return; // 材料耗尽,无法继续填充,返回
if(fl<=l && r<=fr){
int need = tree[p].len - tree[p].count1;
if(count >= need){
count -= need;
tree[p].modify(1);
return; // 这一块填充完毕,返回。否则需要继续向下递归,填充更小的部分
}
}
int mid = (l+r)/2;
pushdown(p);
if(fl <= mid)fill(lson,l,mid,fl,fr);
if(fr >= mid+1)fill(rson,mid+1,r,fl,fr);
tree[p].merge(tree[lson],tree[rson]);
}
int main(){
cin>>n;
for(int i=1;i<=n;i++)a[i] = 1;
build(1,1,n);
//cout<<tree[1].lazy<<endl;
cin>>m;
int pos,l1,r1,l2,r2;
for(int i=1;i<=m;i++){
cin>>pos;
if(pos == 0){
cin>>l1>>r1;
zero(1,1,n,l1,r1);
}else if(pos == 1){
cin>>l1>>r1>>l2>>r2;
ans.empty();
query(1,1,n,l1,r1);
count = ans.count1;
zero(1,1,n,l1,r1);
fill(1,1,n,l2,r2);
}else if(pos == 2){
cin>>l1>>r1;
ans.empty();
query(1,1,n,l1,r1);
cout<<ans.longest_0<<"\n";
}
}
return 0;
}
错点记录
见下方代码及注释。
void merge(node L,node R){
// 传参的时候写成(const node &L,const node &R),常数会小一点。但是如果存在query中ans那样自身合并到自身的情况,就不能这么写
// 在 ans.merge(ans, tree[p]) 这个调用里,L 是 ans 的引用,R 是 tree[p] 的引用。在 merge 函数执行过程中,它会尝试根据 L 和 R 的值来更新当前对象(也就是 ans)。然而,由于 L 是 ans 的引用,在更新 ans 的过程中,L 所引用的对象(即 ans 本身)也会同步改变。这就可能导致在后续使用 L 的信息时,使用到的是已经被修改过的 ans 的信息,从而使得合并逻辑出现混乱,产生错误的结果。
if(L.longest_left == L.len){
longest_left = L.len + R.longest_left;
}else longest_left = L.longest_left;
if(R.longest_right == R.len){
longest_right = R.len + L.longest_right;
}else longest_right = R.longest_right;
longest_0 = max(L.longest_0,max(R.longest_0,L.longest_right+R.longest_left));
len = L.len + R.len;
count1 = L.count1 + R.count1;
lazy = -1; // 不能没有
//在build中执行merge时,在这个过程中,并没有调用pushdown函数。如果不在merge函数中设置lazy = -1,
//那么新合并的节点的lazy标记不为-1(测试得为0),这会导致后续的操作出现错误。
}