操作

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. hdoj 5113 Black And White DFS+剪枝

    Black And White Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) T ...

  2. java_线程的几种状态

    java thread的运行周期中, 有几种状态, 在 java.lang.Thread.State 中有详细定义和说明: NEW 状态是指线程刚创建, 尚未启动 RUNNABLE 状态是线程正在正常 ...

  3. 字符串转码【String.getBytes()和new String()】

    在Java中,String.getBytes(String decode)方法会根据指定的decode编码返回某字符串在该编码下的byte数组表示,如 byte[] b_gbk = "中&q ...

  4. A* search算法解迷宫

    这是一个使用A* search算法解迷宫的问题,细节请看:http://www.laurentluce.com/posts/solving-mazes-using-python-simple-recu ...

  5. Vue2.1.7源码学习

    原本文章的名字叫做<源码解析>,不过后来想想,还是用“源码学习”来的合适一点,在没有彻底掌握源码中的每一个字母之前,“解析”就有点标题党了.建议在看这篇文章之前,最好打开2.1.7的源码对 ...

  6. go test

    testing 是go中自动测试的包, 直接import就可以使用, 使用时需要注意以下规范 执行测试函数的文件必须以 _test.go 结尾, 注意下划线 单元测试函数名必须以 Test 开头, 并 ...

  7. [转载] 为Visual Studio添加默认INCLUDE包含路径的方法

    原文地址 你是否曾经也有过这样的问题: 用VS的时候,有时会用到一些非自带的库,例如WTL.Boost.DX等,每次需要用到时都要在项目属性里添加相应的include目录,久而久之觉得有点麻烦.是否有 ...

  8. Unity中一键创建常用文件夹

    Unity中一键创建常用文件夹 说明 项目测试版本Unity5.3. 这个一个小工具:功能非常简单,就是一键给新建工程添加所有文件夹.到此结束. 但是具体咋操作呢? 与把大象装进冰箱一样,三步,下载代 ...

  9. 解决kylin sync table报错:MetaException(message:java.lang.ClassNotFoundException Class org.apache.hive.hcatalog.data.JsonSerDe not found

    在kylin-gui中sync表default.customer_visit时报错: -- ::, ERROR [http-bio--exec-] controller.BasicController ...

  10. Ubuntu安装Oracle时出现乱码,及其他安装错误

    只要在运行./runInstaller之前先运行下以下命令就ok了: export LANG=en_US #设置运行语言 编译错误 ln -s /usr/lib/i386-linux-gnu/libp ...