操作

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. http协议之 COOKIE

    cookie我们都很了解,这里描述下cookie的几个参数意义 key = "qq", value = "Bobser" .. os.time(), path ...

  2. Xcode 小技巧

    1.手动添加 #warning ,在不确定的 bug.错误.待定代码处,手动添加 #warning 行,在编译时间提醒自己需要处理的地方. 2.由于 arrayWithObjects: 和 initW ...

  3. Mysql show indexes 查看索引状态

     查看表中有哪些已建立的索引 SHOW INDEX FROM tbl_name [FROM db_name] SHOW INDEX会返回以下字段: | Table | Non_unique | Key ...

  4. Java_JSP自定义标签的开发与应用

    在JSTL提供了四个标签库(核心标签库.国际化标签库.数据库标签库和XML标签库),涉及到了几十个标签.虽然这些标签可以完成比较复杂的工作,但它们仍然无法满足程序中的特殊需求.因此,就需要用户根据自己 ...

  5. 【ButterKnife】 安卓程序猿的一大利器

    注:近期才看到的这个类库,来自于jakewharton大神的力作,安卓里面的视图注入库 另小弟水平有限,翻译的不好,还请多多指正 首先是地址(托管在github上):http://jakewharto ...

  6. Html基本操作实例代码

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...

  7. make mrproper and make clean

    make mrproper命令会删除所有的编译生成文件.内核配置文件(.config文件)和各种备份文件,所以几乎只在第一次执行内核编译前才用这条命令. make clean命令则是用于删除大多数的编 ...

  8. MYSQL 慢日志

    http://blog.chinaunix.net/uid-9950859-id-122259.html

  9. mysql安装三 linux源码安装mysql5.6.22

    http://blog.csdn.net/beiigang/article/details/43053803

  10. MsDepSvc 启动失败

    MsDepSvc 使用80端口,用于 Microsoft Web Deploy 3.6 的远程代理服务. 如果80端口被占用,则启动失败.我的是被phpstudy软件占用,所以启动失败.