感觉自己好像搞定了一个不得了得题呢。。

    对于这种区间性质合并的线段树,对于每个节点保存一下当前区间内1的个数,左右边界相邻的1个的个数与0的个数,还有当前区间最大连续的1和0的个数.

    合并的时候的细节:

    1.如果lson的右边界是1并且rson的左边界是1,那么当前节点的最大连续1的值应该更新为tr[n].res=max(tr[n].res,tr[LL].r+tr[RR].l);

    2.如果lson的连续1的个数等于它所表示的区间大小(这段区间的值全为1),那么当前节点的左区间连续1的个数应该更新为tr[n].l+=tr[RR].l;对于右区间的连续1的个数更新也一样。

    3.对于区间中0的性质更新与1一样

    更新时候的细节

    1.如果当前进行的是区间覆盖,那么是需要吧区间翻转的标记置0的。

    2.如果当前既需要更新区间覆盖与区间翻转,这时候只有一种可能情况就是区间覆盖是先进行赋值过的,所以在pushdown的时候区间覆盖应该先进行。

 #include<bits/stdc++.h>
using namespace std;
#define LL n<<1
#define RR n<<1|1
#define lson l,mid,LL
#define rson mid+1,r,RR
#define mid ((l+r)/2)
const int maxn=;
struct node{
int res,ret,num,l,r,ll,rr;//最长连续1的长度,最长连续0的长度,区间内1的个数,区间内左右边界连续1的个数,0的个数
};
node tr[maxn<<];
int lazy[maxn<<];//区间覆盖懒惰标记
int chan[maxn<<];//区间翻转懒惰标记
int a[maxn];
void up(int l,int r,int n){
tr[n].num=tr[LL].num+tr[RR].num;
tr[n].res=max(tr[LL].res,tr[RR].res);
tr[n].ret=max(tr[LL].ret,tr[RR].ret);
tr[n].l=tr[LL].l;
tr[n].ll=tr[LL].ll;
tr[n].r=tr[RR].r;
tr[n].rr=tr[RR].rr;
if(tr[LL].r&&tr[RR].l)
{
tr[n].res=max(tr[n].res,tr[LL].r+tr[RR].l);
if(mid-l+==tr[LL].res)
tr[n].l+=tr[RR].l;
if(r-mid==tr[RR].res)
tr[n].r+=tr[LL].r;
}
if(tr[LL].rr&&tr[RR].ll)
{
tr[n].ret=max(tr[n].ret,tr[LL].rr+tr[RR].ll);
if(mid-l+==tr[LL].ret)
tr[n].ll+=tr[RR].ll;
if(r-mid==tr[RR].ret)
tr[n].rr+=tr[LL].rr;
}
}
void built(int l,int r,int n){
lazy[n]=-;
chan[n]=;
if(l==r)
{
tr[n].l=tr[n].r=tr[n].res=tr[n].num=a[l];
tr[n].ret=tr[n].ll=tr[n].rr=!a[l];
return;
}
built(lson);
built(rson);
up(l,r,n);
}
void pushdown(int l,int r,int n){
if(lazy[n]!=-)
{
chan[LL]=chan[RR]=;
tr[LL].num=tr[LL].l=tr[LL].r=tr[LL].res=(mid-l+)*lazy[n];
tr[LL].ll=tr[LL].rr=tr[LL].ret=(mid-l+)*(!lazy[n]);
tr[RR].num=tr[RR].l=tr[RR].r=tr[RR].res=(r-mid)*lazy[n];
tr[RR].ll=tr[RR].rr=tr[RR].ret=(r-mid)*(!lazy[n]);
lazy[LL]=lazy[RR]=lazy[n];
lazy[n]=-;
}
if(chan[n])
{
chan[LL]^=;
chan[RR]^=;
tr[LL].num=mid-l+-tr[LL].num;
tr[RR].num=r-mid-tr[RR].num;
swap(tr[LL].res,tr[LL].ret);
swap(tr[LL].ll,tr[LL].l);
swap(tr[LL].rr,tr[LL].r);
swap(tr[RR].res,tr[RR].ret);
swap(tr[RR].ll,tr[RR].l);
swap(tr[RR].rr,tr[RR].r);
chan[n]=;
}
}
void update(int l,int r,int n,int left,int right,int num){
if(l>=left&&right>=r)
{
lazy[n]=num;
tr[n].num=num*(r-l+);
chan[n]=;
tr[n].l=tr[n].r=tr[n].res=(r-l+)*num;
tr[n].ll=tr[n].rr=tr[n].ret=(r-l+)*(!num);
return;
}
pushdown(l,r,n);
if(left<=mid)
update(lson,left,right,num);
if(right>mid)
update(rson,left,right,num);
up(l,r,n);
}
void change(int l,int r,int n,int left,int right){
if(l>=left&&right>=r)
{
chan[n]^=;
tr[n].num=(r-l+-tr[n].num);
swap(tr[n].res,tr[n].ret);
swap(tr[n].ll,tr[n].l);
swap(tr[n].rr,tr[n].r);
return;
}
pushdown(l,r,n);
if(left<=mid)
change(lson,left,right);
if(right>mid)
change(rson,left,right);
up(l,r,n);
}
int out1(int l,int r,int n,int left,int right){
if(l>=left&&right>=r)
return tr[n].num;
pushdown(l,r,n);
int res=;
if(left<=mid)
res+=out1(lson,left,right);
if(right>mid)
res+=out1(rson,left,right);
return res;
}
int out2(int l,int r,int n,int left,int right){
if(l>=left&&right>=r)
return tr[n].res;
pushdown(l,r,n);
int res=;
if(left<=mid)
res=max(res,out2(lson,left,right));
if(right>mid)
res=max(res,out2(rson,left,right));
if(tr[LL].r&&tr[RR].l&&mid>=left&&mid<right)
res=max(res,min(tr[LL].r,mid-left+)+min(tr[RR].l,right-mid));
return res;
}
int main(){
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
built(,n,);
for(int i=;i<=m;i++)
{ int x,y,z;
scanf("%d%d%d",&x,&y,&z);
y++;
z++;
if(x==)
update(,n,,y,z,);
else
if(x==)
update(,n,,y,z,);
else
if(x==)
change(,n,,y,z);
if(x==)
printf("%d\n",out1(,n,,y,z));
else
if(x==)
printf("%d\n",out2(,n,,y,z));
}
}
return ;
}

hdu3397 Sequence operation的更多相关文章

  1. hdu3397 Sequence operation 线段树

    hdu3397 Sequence operation #include <bits/stdc++.h> using namespace std; ; struct node { /// l ...

  2. hdu-3397 Sequence operation 线段树多种标记

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3397 题目大意: 0 a b表示a-b区间置为0 1 a b表示a-b区间置为1 2 a b表示a- ...

  3. hdu 3397 Sequence operation(很有意思的线段树题)

    Sequence operation Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  4. Sequence operation(线段树区间多种操作)

    Sequence operation Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  5. HDU 3397 Sequence operation(线段树)

    HDU 3397 Sequence operation 题目链接 题意:给定一个01序列,有5种操作 0 a b [a.b]区间置为0 1 a b [a,b]区间置为1 2 a b [a,b]区间0变 ...

  6. 线段树 区间合并 F - Sequence operation

    F - Sequence operation 题解:这个题目不是一个特别难的题目,但是呢,写了好久,首先线段树难敲,其次就是bug难找,最后这个代码都被我改的乱七八糟的了,这个有两个地方要注意一下,一 ...

  7. 【30.01%】【hdu 3397】Sequence operation

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...

  8. (简单) HDU 3397 Sequence operation,线段树+区间合并。

    Problem Description lxhgww got a sequence contains n characters which are all '0's or '1's. We have ...

  9. hdu 3397 Sequence operation(线段树:区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3397 题意:给你一个长度为n的0,1序列,支持下列五种操作, 操作0(0 a b):将a到b这个区间的 ...

随机推荐

  1. [置顶] 强制访问控制内核模块Smack

    Smack(Simplified Mandatory Access Control Kernel)是Casey Schaufler[15]于2007年在LSM基础上实现的Linux强制访问控制安全模块 ...

  2. 安装MySQL和Navicat,并与MyEclipse连接

    1.下载安装MySQL 1)到http://dev.mysql.com/downloads/下载MySQL社区版Server,和用于JDBC的Connector.一路默认安装就可以,须要注意的是记住M ...

  3. Linux目录和权限

    1. rmdir -p  用来删除一串目录,是否可以成功删除?   rmdir -p  删除一个不存在的目录时是否报错呢?rmdir -p 不能成功删除非空目录,rmdir -p 删除一个不存在的目录 ...

  4. VS2010 rdlc 被除数为0 显示错误号

    =Sum(Fields!ROCKNUM.Value/Fields!SEND.Value*100) 当Fields!SEND.Value为0或者空时,显示错误号 修改: =IIF(isnothing(F ...

  5. Objective-C set/get方法

    主要内容set get方法的使用 关键字 @property 全自动生成set get方法 // 类的声名 @interface People : NSObject{ int _age; // 成员变 ...

  6. java 中解析xml的技术

    最初,XML 语言仅仅是意图用来作为 HTML 语言的替代品而出现的,但是随着该语言的不断发展和完善,人们越来越发现它所具有的优点:例如标记语言可扩展,严格的语法规定,可使用有意义的标记,内容存储和表 ...

  7. vector的用法总结

    Reference Constructors vector Constructs a vector of a specific size or with elements of a specific ...

  8. No2_5.类的高级特性_Java学习笔记_抽象类和成员内部类

    一.抽象类1.所谓抽象类,只声明方法的存在而不去实现它的类:2.抽象类不能被实例化,即不能实现其对象:3.abstract class 类名{ 类体 }4.包含一个或多个抽象方法的类必须声明成抽象类: ...

  9. 对于JavaScript对象的prototype和__proto__的理解

    一.Object和Function的关系: 刚学JavaScript的时候,看书上说JavaScript中万物皆对象,而javascript中的其他对象都是从Object继承而来,包括内置对象.瞬间觉 ...

  10. python之pandas模块

    一.pandas模块是基于Numpy模块的,pandas的主要数据结构是Series和DadaFrame,下面引入这样的约定: from pandas import Series,DataFrame ...