这个是去年遗留历史问题,之前思路混乱,搞了好多发都是WA,就没做了

自从上次做了大白书上那个双重懒惰标记的题目,做这个就思路很清晰了

跟上次大白上那个差不多,这个也是有一个sets标记,代表这个区间全部置为0或者1,没有置位的时候为-1

还有个rev标记,代表翻转操作,0代表当前不翻,1代表当前翻

要注意一下优先级,发现有不同的弄法,我是这个弄得,有set操作的时候,set标记设值,并把当前节点的rev标记设为0,因为不管要不要rev,当前set操作肯定直接覆盖了

rev操作不改变set操作,在pushdown的时候,先考虑set标记再弄rev标记,这也是很好理解的,因为一旦rev和set共存,肯定是rev在set后面。

有几个细节要注意一下,一开始行云流水一气呵成,发现还是WA了,就是这几个地方,

1.除了set的时候强制给rev弄成0,其他任何时候对rev标记操作都是^1,取反,这个也很好理解,之前在pushdown里面我就是直接传值,肯定不对嘛

2.在pushdown里面的set下传操作也要记得把子树的rev标记抹除,一开始只在主修改函数里写了,这里没写,WA的不明不白,理由跟上面的一样

然后就基本上没问题了

#include <iostream>
#include <cstdio>
#include <cstring>
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
using namespace std;
const int N=100000+10;
int d[N*3],maxc[N*3],lc[N*3],rc[N*3],maxc0[N*3],lc0[N*3],rc0[N*3];
int sets[N*3],rev[N*3];
int n,Q;
int A[N];
void up(int rt,int l,int r)
{int mid=(l+r)>>1;d[rt]=d[rt<<1]+d[rt<<1|1];maxc[rt]=max(maxc[rt<<1],maxc[rt<<1|1]);maxc[rt]=max(maxc[rt],lc[rt<<1|1]+rc[rt<<1]);lc[rt]=lc[rt<<1];rc[rt]=rc[rt<<1|1];if (lc[rt<<1]==mid-l+1) lc[rt]+=lc[rt<<1|1];if (rc[rt<<1|1]==r-mid) rc[rt]+=rc[rt<<1];maxc0[rt]=max(maxc0[rt<<1],maxc0[rt<<1|1]);maxc0[rt]=max(maxc0[rt],lc0[rt<<1|1]+rc0[rt<<1]);lc0[rt]=lc0[rt<<1];rc0[rt]=rc0[rt<<1|1];if (lc0[rt<<1]==mid-l+1) lc0[rt]+=lc0[rt<<1|1];if (rc0[rt<<1|1]==r-mid) rc0[rt]+=rc0[rt<<1];
}
void build(int rt,int l,int r)
{sets[rt]=-1;rev[rt]=0;if (l>=r){d[rt]=maxc[rt]=A[l];lc[rt]=rc[rt]=A[l];lc0[rt]=rc0[rt]=maxc0[rt]=1-A[l];return;}int mid=(l+r)>>1;build(lson);build(rson);up(rt,l,r);
}
void pushdown(int rt,int l,int r)
{if (l>=r) return;int mid=(l+r)>>1;if (sets[rt]>=0){maxc[rt<<1]=lc[rt<<1]=rc[rt<<1]=d[rt<<1]=(mid-l+1)*sets[rt];maxc[rt<<1|1]=lc[rt<<1|1]=rc[rt<<1|1]=d[rt<<1|1]=(r-mid)*sets[rt];maxc0[rt<<1]=lc0[rt<<1]=rc0[rt<<1]=(mid-l+1)*(1-sets[rt]);maxc0[rt<<1|1]=lc0[rt<<1|1]=rc0[rt<<1|1]=(r-mid)*(1-sets[rt]);sets[rt<<1]=sets[rt<<1|1]=sets[rt];rev[rt<<1]=rev[rt<<1|1]=0;sets[rt]=-1;}if (rev[rt]>0){d[rt<<1]=(mid-l+1)-d[rt<<1];d[rt<<1|1]=(r-mid)-d[rt<<1|1];int t1,t2,t3;t1=maxc[rt<<1];t2=lc[rt<<1];t3=rc[rt<<1];maxc[rt<<1]=maxc0[rt<<1];lc[rt<<1]=lc0[rt<<1];rc[rt<<1]=rc0[rt<<1];maxc0[rt<<1]=t1;lc0[rt<<1]=t2;rc0[rt<<1]=t3;t1=maxc[rt<<1|1];t2=lc[rt<<1|1];t3=rc[rt<<1|1];maxc[rt<<1|1]=maxc0[rt<<1|1];lc[rt<<1|1]=lc0[rt<<1|1];rc[rt<<1|1]=rc0[rt<<1|1];maxc0[rt<<1|1]=t1;lc0[rt<<1|1]=t2;rc0[rt<<1|1]=t3;rev[rt<<1]^=1;rev[rt<<1|1]^=1;rev[rt]=0;}
}
void change(int val,int L,int R,int rt,int l,int r)
{if (L<=l && r<=R){d[rt]=(r-l+1)*val;lc[rt]=rc[rt]=(r-l+1)*val;maxc[rt]=(r-l+1)*val;lc0[rt]=rc0[rt]=maxc0[rt]=(r-l+1)*(1-val);sets[rt]=val;rev[rt]=0;return;}pushdown(rt,l,r);int mid=(l+r)>>1;if (L>mid) change(val,L,R,rson);elseif (R<=mid) change(val,L,R,lson);else{change(val,L,R,rson);change(val,L,R,lson);}up(rt,l,r);
}
void revers(int L,int R,int rt,int l,int r)
{if (L<=l && r<=R){d[rt]=(r-l+1)-d[rt];int t1,t2,t3;t1=maxc[rt];t2=lc[rt];t3=rc[rt];maxc[rt]=maxc0[rt];lc[rt]=lc0[rt];rc[rt]=rc0[rt];maxc0[rt]=t1;lc0[rt]=t2;rc0[rt]=t3;rev[rt]^=1;return;}pushdown(rt,l,r);int mid=(l+r)>>1;if (R<=mid) revers(L,R,lson);elseif (L>mid) revers(L,R,rson);else{revers(L,R,lson);revers(L,R,rson);}up(rt,l,r);
}
int output(int op,int L,int R,int rt,int l,int r)
{if (L==l && r==R){if (op==3) return d[rt];else  return maxc[rt];}pushdown(rt,l,r);int mid=(l+r)>>1;if (L>mid) return output(op,L,R,rson);elseif (R<=mid) return output(op,L,R,lson);else{int ret1=output(op,L,mid,lson);int ret2=output(op,mid+1,R,rson);if (op==3) return ret1+ret2;else{int ret=max(ret1,ret2);int t1=min(rc[rt<<1],mid-L+1);int t2=min(lc[rt<<1|1],R-mid);ret=max(ret,t1+t2);return ret;}}
}
int main()
{int t,op,a,b;scanf("%d",&t);while (t--){scanf("%d%d",&n,&Q);for (int i=1;i<=n;i++) scanf("%d",&A[i]);build(1,1,n);while (Q--){scanf("%d%d%d",&op,&a,&b);a++;b++;if (op<=1){change(op,a,b,1,1,n);}elseif (op==2){revers(a,b,1,1,n);}else{int ans=output(op,a,b,1,1,n);printf("%d\n",ans);}}}return 0;
}

  

转载于:https://www.cnblogs.com/kkrisen/p/3864311.html

HDU 3397 线段树 双懒惰标记相关推荐

  1. hdu 3397 线段树

    题意: Change operations: 0 a b change all characters into '0's in [a , b] 1 a b change all characters ...

  2. POJ 2777 ZOJ 1610 HDU 1698 --线段树--区间更新

    直接将这3题 放一起了  今天在做线段树的东西 这3个都是区间更新的 查询方式互相不同 反正都可以放到一起吧 直接先上链接了 touch me touch me touch me 关于涉及到区间的修改 ...

  3. HDU - 4578Transformation——线段树+区间加法修改+区间乘法修改+区间置数+区间和查询+区间平方和查询+区间立方和查询

    [题目描述] HDU - 4578Transformation Problem Description Yuanfang is puzzled with the question below: The ...

  4. poj 2777 AND hdu 5316 线段树

    区间染色问题,用线段树可以解决.颜色总类不多,故考虑用二进制数的每一位表示一种颜色,然后父节点的颜色就是孩子节点颜色"或"起来,加上lazy标记,轻松AC. poj 2777: 1 ...

  5. FZU 2171(线段树的延迟标记)

    题意:容易理解. 分析:时隔很久,再一次写了一道线段树的代码,之前线段树的题也做了不少,包括各种延迟标记,但是在组队分任务之后,我们队的线段树就交给了另外一个队友在搞, 然后我就一直没去碰线段树的题了 ...

  6. hdu 5367(线段树+区间合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5367 官方题解: 对于求"高山脉"长度,可以利用线段树.树节点中保存左高度连续长度 ...

  7. hdu 5266(线段树+LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5266 解题思路: 考虑dfs序,通过在简单的证明可知L~R的LCA为L ~R 中dfs序较小的那个位置 ...

  8. hdu 5124(线段树区间更新+lazy思想)

    http://acm.hdu.edu.cn/showproblem.php?pid=5124 题意:区间覆盖次数问题. 解题思路:线段树水之. #include<iostream> #in ...

  9. hdu 3954(线段树区间更新)

    转载标记处:http://www.cnblogs.com/wang-jue/articles/2920341.html 思路:这道题所得到的经验与每个英雄的等级有关,一般的可能就用线段树一直更新到每一 ...

最新文章

  1. SpringBoot静态获取 bean的三种方式,你学会了吗?
  2. redux常见问题答疑
  3. 职称计算机windows 7,2017职称计算机考试Windows训练题
  4. vscode 结束_21 个VSCode 快捷键,让代码更快,更有趣
  5. matlab有限域多项式除法_MATLAB极小值优化
  6. php 和 java 美刀,通过GOOGLE(谷歌) API获取实时货币汇率(人民币和美金)的PHP代码
  7. Games 图形学 L2线性代数
  8. C语言实现HDB3编码与译码
  9. 混合动力系统的整车经济性开发与能量管理策略高级技术
  10. 人工智能和大数据案例课程
  11. silvaco的石墨烯fet仿真_COMSOL Multiphysics多物理场仿真光电学习必看的内容
  12. linux内核打补丁步骤
  13. 带K线的macd选股指标详解 优化MACD王牌指标 通达信macd选股指标源码
  14. opencv 打开摄像头
  15. 软件工程——初识文档
  16. IDEA自定义模板:快速指定一个数据库映射字段(驼峰生成下划线命名)
  17. 深入理解CAS算法原理
  18. Java 堆默认大小
  19. 随手记_英语_学术写作_英文科技论文
  20. 洪涝有源淹没算法及淹没结果分析

热门文章

  1. 0.《沉浸式线性代数》:前言
  2. mysql innodb 全表锁,Mysql InnoDB行锁及表锁分享
  3. 计算机英语反思总结,计算机在英语教学中辅助作用的反思
  4. 无法开启计算机,Win7下鼠标右键无法开启计算机属性怎么办?
  5. MySQL 索引 :哈希索引、B+树索引、全文索引
  6. 2019我做成的事情
  7. redis——持久化
  8. python 人气高的项目_给大家推荐:五个Python小项目,Github上的人气很高的!
  9. 使用named_mutex和named_condition配合实现读写锁
  10. codeforces 467A-C语言解题报告