操作

Change operations:

0 a b change all characters into '0's in [a , b]

1 a b change all characters into '1's in [a , b]

2 a b change all '0's into '1's and change all '1's into '0's in [a, b]

Output operations:

3 a b output the number of '1's in [a, b]

4 a b output the length of the longest continuous '1' string in [a , b]

不太熟悉异或操作

用两种标记 col表示 区间变成0/1  。 ox表示 区间异或

0 1 操作跟普通线段树一样

异或操作须要 ox[rt]^=1;

......搞了一个晚上 错这里了..

4 操作普通的区间合并啦

lsum记录左边開始连续的1的个数 rsum记录右边開始连续的1的个数

zero就记录连续的0啦

还有在延迟的时候须要先pushdown   col标记  再 pushdown ox标记

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define IN freopen ("in.txt" , "r" , stdin);
#define OUT freopen ("out.txt" , "w" , stdout);
typedef long long LL;
const int MAXN = 100110;//点数的最大值
const int MAXM = 20006;//边数的最大值
const int INF = 11521204;
const int mod=1000000007;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int num1[MAXN<<2],num2[MAXN<<2],ans[MAXN];
int sum[MAXN<<2],lsum[MAXN<<2],rsum[MAXN<<2];
int zero[MAXN<<2],lzero[MAXN<<2],rzero[MAXN<<2];
int col[MAXN<<2],ox[MAXN<<2];
void pushup(int rt,int m)
{
int lx=m-(m>>1);
int rx=(m>>1);
//1的向上更新
lsum[rt]=lsum[rt<<1];
rsum[rt]=rsum[rt<<1|1];
if(lsum[rt]==lx) lsum[rt]+=lsum[rt<<1|1];
if(rsum[rt]==rx) rsum[rt]+=rsum[rt<<1];
sum[rt]=max(max(sum[rt<<1],sum[rt<<1|1]),rsum[rt<<1]+lsum[rt<<1|1]);
//0的向上更新
lzero[rt]=lzero[rt<<1];
rzero[rt]=rzero[rt<<1|1];
if(lzero[rt]==lx) lzero[rt]+=lzero[rt<<1|1];
if(rzero[rt]==rx) rzero[rt]+=rzero[rt<<1];
zero[rt]=max(max(zero[rt<<1],zero[rt<<1|1]),rzero[rt<<1]+lzero[rt<<1|1]);
//总数的更新
num1[rt]=num1[rt<<1]+num1[rt<<1|1];
num2[rt]=num2[rt<<1]+num2[rt<<1|1];
}
void pushdown(int rt,int m,int l,int r)
{
if(col[rt]!=-1)
{
ox[rt<<1]=ox[rt<<1|1]=0;
col[rt<<1]=col[rt<<1|1]=col[rt];
sum[rt<<1]=lsum[rt<<1]=rsum[rt<<1]=col[rt]?m-(m>>1):0;
sum[rt<<1|1]=lsum[rt<<1|1]=rsum[rt<<1|1]=col[rt]?m>>1:0;
zero[rt<<1]=lzero[rt<<1]=rzero[rt<<1]=col[rt]?0:m-(m>>1);
zero[rt<<1|1]=lzero[rt<<1|1]=rzero[rt<<1|1]=col[rt]?0:m>>1;
num1[rt<<1]=col[rt]? m-(m>>1):0;
num2[rt<<1]=col[rt]?0:m-(m>>1);
num1[rt<<1|1]=col[rt]?m>>1:0;
num2[rt<<1|1]=col[rt]? 0:m>>1;
col[rt]=-1;
}
if(ox[rt])
{
ox[rt<<1]^=1;
ox[rt<<1|1]^=1;
swap(sum[rt<<1],zero[rt<<1]);
swap(sum[rt<<1|1],zero[rt<<1|1]);
swap(lsum[rt<<1],lzero[rt<<1]);
swap(lsum[rt<<1|1],lzero[rt<<1|1]);
swap(rsum[rt<<1],rzero[rt<<1]);
swap(rsum[rt<<1|1],rzero[rt<<1|1]);
swap(num1[rt<<1],num2[rt<<1]);
swap(num1[rt<<1|1],num2[rt<<1|1]);
ox[rt]=0;
}
}
void build(int l,int r,int rt)
{
col[rt]=-1;
ox[rt]=0;
if(l==r)
{
scanf("%d",&num1[rt]);
if(num1[rt])
{
num2[rt]=0;
sum[rt]=lsum[rt]=rsum[rt]=1;
zero[rt]=lzero[rt]=rzero[rt]=0;
}
else
{
sum[rt]=lsum[rt]=rsum[rt]=0;
zero[rt]=lzero[rt]=rzero[rt]=1;
num2[rt]=1;
}
return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
pushup(rt,r-l+1);
}
void update(int L,int R,int c,int l,int r,int rt)//更新 0 1 2 分别表示区间更新为0 , 1。异或
{
if(L<=l&&r<=R)
{
if(c==2)//异或
{
swap(num1[rt],num2[rt]);//0 1 个数交换
swap(sum[rt],zero[rt]);
swap(lsum[rt],lzero[rt]);
swap(rsum[rt],rzero[rt]);
ox[rt]^=1;
}
else
{
sum[rt]=lsum[rt]=rsum[rt]=c==1?r-l+1:0;
zero[rt]=lzero[rt]=rzero[rt]=c==1? 0:r-l+1;
num1[rt]=c==1?r-l+1:0;
num2[rt]=c==1?0:r-l+1;
col[rt]=c;
ox[rt]=0;
}
return ;
}
int m=(l+r)>>1;
pushdown(rt,r-l+1,l,r);
if(L<=m) update(L,R,c,lson);
if(m<R) update(L,R,c,rson);
pushup(rt,r-l+1);
}
int query1(int L,int R,int l,int r,int rt)//输出1的个数
{
if(L<=l&&r<=R)
{
return num1[rt];
}
int m=(l+r)>>1,res=0;
pushdown(rt,r-l+1,l,r);
if(L<=m) res+=query1(L,R,lson);
if(m<R) res+=query1(L,R,rson);
return res;
}
int query2(int L,int R,int l,int r,int rt)//输出连续的
{
if(L<=l&&r<=R)
{
return sum[rt];
}
int m=(l+r)>>1,res=0;
pushdown(rt,r-l+1,l,r);
if(L<=m) res=max(res, query2(L,R,lson));
if(m<R) res=max(res,query2(L,R,rson));
res = max(res, min(m-L+1, rsum[rt<<1])+min(R-m, lsum[rt<<1|1]));
return res;
}
int main()
{
int n,m,t;
//IN;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
build(1,n,1);
while(m--)
{
int a,b,c;
scanf("%d%d%d",&c,&a,&b);
a++,b++;
if(c <=2)
update(a,b,c,1,n,1);
else
{
if(c==3)
printf("%d\n",query1(a,b,1,n,1));
else printf("%d\n",query2(a,b,1,n,1));
}
}
}
return 0;
}
/*
1
4 3
0 0 1 1
1 1 2
2 1 2
3 1 1
*/

【线段树】HDU 3397 Sequence operation 区间合并的更多相关文章

  1. 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变 ...

  2. hdu 3397 Sequence operation (线段树 区间合并 多重标记)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=3397 题意: 给你一串01串,有5种操作 0. 区间全部变为0 1.区间全部变为1 2.区间异或 3.询问 ...

  3. hdu 3397 Sequence operation 线段树 区间更新 区间合并

    题意: 5种操作,所有数字都为0或1 0 a b:将[a,b]置0 1 a b:将[a,b]置1 2 a b:[a,b]中的0和1互换 3 a b:查询[a,b]中的1的数量 4 a b:查询[a,b ...

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

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

  5. HDU 3397 Sequence operation(区间合并 + 区间更新)

    题目链接:pid=3397">http://acm.hdu.edu.cn/showproblem.php?pid=3397 题意:给定n个数,由0,1构成.共同拥有5种操作. 每一个操 ...

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

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

  7. HDU 3308 LCIS (线段树&#183;单点更新&#183;区间合并)

    题意  给你一个数组  有更新值和查询两种操作  对于每次查询  输出相应区间的最长连续递增子序列的长度 基础的线段树区间合并  线段树维护三个值  相应区间的LCIS长度(lcis)  相应区间以左 ...

  8. HDU 1540 Tunnel Warfare 平衡树 / 线段树:单点更新,区间合并

    Tunnel Warfare                                  Time Limit: 4000/2000 MS (Java/Others)    Memory Lim ...

  9. 线段树 HDU 3397(真)

    5 种操作  0 1 然后 异或 似乎这种2个更新的先后每次都搞不清 覆盖有覆盖就可以不异或 也不知道为什么 #include<stdio.h> #include<string.h& ...

随机推荐

  1. 英特尔老款CPU支持虚拟化对照表(转)

    说明:一般来说新款的挤牙膏公司出的CPU都基本支持虚拟化,但不包括Atom系列的,也就是小主机低功耗机器使用的CPU. Intel® Virtualization Technology List YE ...

  2. CentOS 7下配置安装KVM

    注意:KVM一切安装和运行都是在root用户下完成的,并且只有root才能支持某些软件. 一.准备工作: 1.关闭selinux,iptables,重启后生效 ##关闭selinux # sed -i ...

  3. Android中播放本地SD卡中歌曲须要的加入的权限

    使用MediaPlayer播放本地Mp3文件时.须要注意的訪问路径的问题以及訪问权限的问题. 1.訪问路径:/storage/emulated/0 此路径即为手机的根路径,能够通过下载ES文件浏览器软 ...

  4. pig—WordCount analysis

    grunt> cat /opt/dataset/input.txt keyword1 keyword2 keyword2 keyword4 keyword3 keyword1 keyword4 ...

  5. winform打开进程与关闭进程

    #region 判断某进程名是否运行 /// <summary> /// 关闭指定名称的进程 /// </summary> /// <param name="p ...

  6. IDC门外汉-单线、双线、智能多线、BGP的区别

    一.单线在此不多说,不是电信,就是网通机房,是,2005年前的机房情况: 二.双线:上般是从2004年到2008年用此方法较多,此今还有不少在用此法如下: 双线主机 有单IP和单网卡双IP地址,双网卡 ...

  7. 一种基于ES5的JavaScript继承

    关于JavaScript继承,方式非常多,包含compile-to-javascript的语言TypeScript, CoffeeScript以及站点MDN, GitHub, Modernizr各种p ...

  8. windows下androidNDK环境配置

    一:什么是NDK? NDK 提供了一系列的工具,帮助开发者快速开发C(或C++)的动态库,并能自动将so 和java 应用一起打包成apk.这些工具对开发者的帮助是巨大的. NDK 集成了交叉编译器, ...

  9. jQuery元素属性attr设置多个键值或函数 删除属性removeAttr

    $("Element").attr(name) '取得第一个匹配的属性值,比如$("img").attr("src") $("El ...

  10. AES算法工具类

    什么是对称加密算法? AES已经变成目前对称加密中最流行算法之一:AES可以使用128.192.和256位密钥,并且用128位分组加密和解密数据. 对称加密算法安全吗? 看过间谍局的知友们一定知道电台 ...