bzoj1858: [Scoi2010]序列操作
lazy-tag线段树。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 800000 + 10; struct Segtree {
#define lc(x) ((x)<<1)
#define rc(x) (((x)<<1)|1) int l[maxn],r[maxn];
int l1[maxn],r1[maxn],con1[maxn],cnt1[maxn];
int l0[maxn],r0[maxn],con0[maxn];
int t1[maxn],t0[maxn],tr[maxn]; void set1(int x) {
l1[x]=r1[x]=con1[x]=cnt1[x]=r[x]-l[x]+1;
l0[x]=r0[x]=con0[x]=0;
t1[x]=1; t0[x]=tr[x]=0;
} void set0(int x) {
l0[x]=r0[x]=con0[x]=r[x]-l[x]+1;
l1[x]=r1[x]=con1[x]=cnt1[x]=0;
t0[x]=1; t1[x]=tr[x]=0;
} void inv(int x) {
swap(l1[x],l0[x]);
swap(r1[x],r0[x]);
swap(con1[x],con0[x]);
cnt1[x]=r[x]-l[x]+1-cnt1[x];
tr[x]^=1;
} void push(int x) {
if(t0[x]) {
set0(lc(x));
set0(rc(x));
t0[x]=0;
}
if(t1[x]) {
set1(lc(x));
set1(rc(x));
t1[x]=0;
}
if(tr[x]) {
inv(lc(x));
inv(rc(x));
tr[x]=0;
}
} void update(int x) {
l1[x]=(l1[lc(x)]==r[lc(x)]-l[lc(x)]+1?l1[lc(x)]+l1[rc(x)]:l1[lc(x)]);
r1[x]=(r1[rc(x)]==r[rc(x)]-l[rc(x)]+1?r1[rc(x)]+r1[lc(x)]:r1[rc(x)]);
l0[x]=(l0[lc(x)]==r[lc(x)]-l[lc(x)]+1?l0[lc(x)]+l0[rc(x)]:l0[lc(x)]);
r0[x]=(r0[rc(x)]==r[rc(x)]-l[rc(x)]+1?r0[rc(x)]+r0[lc(x)]:r0[rc(x)]);
con1[x]=max(max(con1[lc(x)],con1[rc(x)]),r1[lc(x)]+l1[rc(x)]);
con0[x]=max(max(con0[lc(x)],con0[rc(x)]),r0[lc(x)]+l0[rc(x)]);
cnt1[x]=cnt1[lc(x)]+cnt1[rc(x)];
} void op0(int x,int L,int R) {
push(x);
if(R<l[x] || L>r[x]) return;
if(L<=l[x] && r[x]<=R) {
set0(x);
return;
}
op0(lc(x),L,R);
op0(rc(x),L,R);
update(x);
} void op1(int x,int L,int R) {
push(x);
if(R<l[x] || L>r[x]) return;
if(L<=l[x] && r[x]<=R) {
set1(x);
return;
}
op1(lc(x),L,R);
op1(rc(x),L,R);
update(x);
} void op2(int x,int L,int R) {
push(x);
if(R<l[x] || L>r[x]) return;
if(L<=l[x] && r[x]<=R) {
inv(x);
return;
}
op2(lc(x),L,R);
op2(rc(x),L,R);
update(x);
} int op3(int x,int L,int R) {
push(x);
if(R<l[x] || L>r[x]) return 0;
if(L<=l[x] && r[x]<=R) return cnt1[x];
return(op3(lc(x),L,R)+op3(rc(x),L,R));
} int op4(int x,int L,int R) {
push(x);
if(R<l[x] || L>r[x]) return 0;
if(L<=l[x] && r[x]<=R) return con1[x];
return max(max(op4(lc(x),L,R),op4(rc(x),L,R)),min(r1[lc(x)],r[lc(x)]-L+1)+min(l1[rc(x)],R-l[rc(x)]+1));
} void build(int x,int L,int R) {
l[x]=L; r[x]=R;
if(L==R) {
int k;
scanf("%d",&k);
if(k) l1[x]=r1[x]=con1[x]=cnt1[x]=1;
else l0[x]=r0[x]=con0[x]=1;
return;
}
int mid=(L+R)>>1;
build(lc(x),L,mid);
build(rc(x),mid+1,R);
update(x);
}
}seg;
int n,m; int main() {
scanf("%d%d",&n,&m);
seg.build(1,0,n-1);
for(int i=1,op,a,b;i<=m;i++) {
scanf("%d%d%d",&op,&a,&b);
if(op==0) seg.op0(1,a,b);
else if(op==1) seg.op1(1,a,b);
else if(op==2) seg.op2(1,a,b);
else if(op==3) printf("%d\n",seg.op3(1,a,b));
else if(op==4) printf("%d\n",seg.op4(1,a,b));
}
return 0;
}
bzoj1858: [Scoi2010]序列操作的更多相关文章
- bzoj1858[Scoi2010]序列操作 线段树
1858: [Scoi2010]序列操作 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 3079 Solved: 1475[Submit][Statu ...
- BZOJ1858 [Scoi2010]序列操作(线段树)
题目链接 [Scoi2010]序列操作 考验代码能力的一道好题. 思想还是很简单的(直接上线段树),但是比较难写. #include <bits/stdc++.h> using names ...
- BZOJ1858[Scoi2010]序列操作 题解
题目大意: 有一个01序列,现在对于这个序列有五种变换操作和询问操作: 0 a b 把[a, b]区间内的所有数全变成0:1 a b 把[a, b]区间内的所有数全变成1:2 a b 把[a,b]区间 ...
- [BZOJ1858] [SCOI2010] 序列操作 解题报告 (线段树)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1858 Description lxhgww最近收到了一个01序列,序列里面包含了n个数, ...
- 【分块】bzoj1858 [Scoi2010]序列操作
分块 Or 线段树 分块的登峰造极之题 每块维护8个值: 包括左端点在内的最长1段: 包括右端点在内的最长1段: 该块内的最长1段: 该块内1的个数: 包括左端点在内的最长0段://这四个是因为可能有 ...
- bzoj千题计划177:bzoj1858: [Scoi2010]序列操作
http://www.lydsy.com/JudgeOnline/problem.php?id=1858 2018 自己写的第1题,一遍过 ^_^ 元旦快乐 #include<cstdio> ...
- [待码][BZOJ1858]SCOI2010序列操作 jzyzoj1655
待码的线段树.....太长了看上去不是很想写 [ 什么破理由啊摔,不要脸 ] 嗯先水几道再写
- bzoj1858 [Scoi2010]序列操作——线段树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1858 线段树...调了一个上午...(后面带 // 的都是改出来的) lazy 标记的下放好 ...
- 【BZOJ-1858】序列操作 线段树
1858: [Scoi2010]序列操作 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1961 Solved: 991[Submit][Status ...
随机推荐
- [Linux]学习笔记(2)
本节主要学习: whoami who am i who w users tty 6个命令的用法. (1)whoami whoami用于查询当前是以哪个用户登录Linux系统: [root@linuxf ...
- linux实现nginx按照日期存储日志
通过shell脚本实现+定时任务+nginx信号管理实现日志按日期存储. 1.编写shell脚本,实现日志按日期存储 #!/bin/bash base_path='/home/wwwlogs/' lo ...
- Oracle 执行计划说明
生成SQL的执行计划是Oracle在对SQL做硬解析时的一个非常重要的步骤,它制定出一个方案告诉Oracle在执行这条SQL时以什么样的方式访问数据:索引还是全表扫描,是Hash Join还是Nest ...
- MFC VC6++学习笔记
一.mfc中基于对话框程序添加菜单栏 1打开对话框资源,然后右键->属性->常规 里面有个"菜单" 下拉框,然后选择IDM_USER! 2打开对话框,右键属性,选择刚才 ...
- 自学JAVA总结
2.在定义常量的时候C语言中定义为const而JAVA中为final3.在JAVA声明成员变量的时候,使用static来定义.4.在JAVA中的boolean类型只包括true和false,但是在C中 ...
- 【趟坑】公共引用的jar包 pom的配置方法
http://www.cnblogs.com/viewcozy/p/4789877.html 接上一篇 ,内部moudule生成jar的方式 上一篇已经实现了,想把jar作为公共的部分让任何项目都可以 ...
- C# 使用winForm的TreeView显示中国城镇四级联动
直接上代码吧,这里 MySql.Data.MySqlClient;需要到mysql官网下载mysql-connector-net-6.9.8-noinstall.zip 访问密码 6073 usi ...
- easy ui 异步上传文件,跨域
easy ui 跨域上传文件,代码如下: 1.html代码:(这段代码是个win窗体,我在点击上传图片按钮然后弹出一个上传图片的窗体,选择图片再进行上传,这样在form提交时,提交的参数会少一点.) ...
- myEclipse中的web项目直接引入到eclipse中运行
首先打开项目属性(Properties),如果动态web项目被作为普通java项目引进去,需要首先修改为web项目,如下图: 确定后即可在eclipse中看到转换为了动态的web项目,然后继续属性(P ...
- WCF入门教程一[什么是WCF]--转载只为学习收藏
一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...