操作

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. Eclipse 工具下Maven 项目的快速搭建

    Eclipse 工具下Maven 项目的搭建 参考博文:https://www.cnblogs.com/iflytek/p/7096481.html 什么是Maven项目 简单来说,传统的Web项目: ...

  2. 【10.9校内练习赛】【搜索】【2-sat】【树链剖分】【A_star k短路】【差分约束+判负环】

    在洛谷上复制的题目! P3154 [CQOI2009]循环赛 题目描述 n队伍比赛,每两支队伍比赛一次,平1胜3负0. 给出队伍的最终得分,求多少种可能的分数表. 输入输出格式 输入格式: 第一行包含 ...

  3. Codeforces Round #228 (Div. 1) C. Fox and Card Game 博弈

    C. Fox and Card Game 题目连接: http://codeforces.com/contest/388/problem/C Description Fox Ciel is playi ...

  4. hdoj 1753 大明A+B 高精度/java

    大明A+B Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  5. bootstrap字体图标不正常显示的原因

    本地引入bootstrap.css文件,使用https://v3.bootcss.com/components/站点 字体图标 时不能正常显示,换成 bootstrap 官网的 cdn 链接却能正常显 ...

  6. mysql学习心得转

    http://www.cnblogs.com/lyhabc/p/3691555.html

  7. ie不支持max-height的解决之法

    .div{ max-height: 100px; _height:expression(this.scrollHeight > 100 ? "100px" : "a ...

  8. RobotFramework自动化1-环境搭建

    前言 Robot Framework是一款python编写的功能自动化测试框架.具备良好的可扩展性,支持关键字驱动,可以同时测试多种类型的客户端或者接口,可以进行分布式测试执行. Robot Fram ...

  9. Android之多媒体扫描过程

    转自:http://blog.csdn.net/yan8024/article/details/6620359下面是系统图      MediaScannerReceiver会在任何的ACTION_B ...

  10. 编译打包工具sbt的镜像设置

    sbt可以和maven共用一个镜像,公司内部有的自然最后不过 创建文件:~/.sbt/repositories [repositories] local aliyun: http://maven.al ...