hdu 3397 Sequence operation(很有意思的线段树题)
Sequence operation
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4952 Accepted Submission(s): 1452
We have five operations here:
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]
Each case has two integers in the first line: n and m (1 <= n , m <= 100000).
The next line contains n characters, '0' or '1' separated by spaces.
Then m lines are the operations:
op a b: 0 <= op <= 4 , 0 <= a <= b < n.
10 10
0 0 0 1 1 0 1 0 1 1
1 0 2
3 0 5
2 2 2
4 0 4
0 3 6
2 3 7
4 2 8
1 0 5
0 5 6
3 3 9
2
6
5
题意:
#include<algorithm>
#include<iostream>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100100;
int sta[maxn<<2],ml0[maxn<<2],mr0[maxn<<2],ml1[maxn<<2];//sta为标记。ml1为1左端连续。ml0为0左端连续
int mr1[maxn<<2],ma1[maxn<<2],ma0[maxn<<2],one[maxn<<2];//ma1为最大1连续数.one记录1的个数
void opp(int L,int R,int k)//对于2操作。0和1互换
{
swap(ma0[k],ma1[k]);
swap(ml0[k],ml1[k]);
swap(mr0[k],mr1[k]);
one[k]=R-L-one[k]+1;
}
void pushup(int L,int R,int k)//下传标记
{
int ls,rs,mid;
ls=k<<1;
rs=ls|1;
mid=(L+R)>>1;
ml0[k]=ml0[ls];
mr0[k]=mr0[rs];
ml1[k]=ml1[ls];
mr1[k]=mr1[rs];
if(ml0[ls]==mid-L+1)
ml0[k]+=ml0[rs];
if(mr0[rs]==R-mid)
mr0[k]+=mr0[ls];
if(ml1[ls]==mid-L+1)
ml1[k]+=ml1[rs];
if(mr1[rs]==R-mid)
mr1[k]+=mr1[ls];
ma1[k]=max(ma1[ls],ma1[rs]);
ma1[k]=max(ma1[k],mr1[ls]+ml1[rs]);
ma0[k]=max(ma0[ls],ma0[rs]);
ma0[k]=max(ma0[k],mr0[ls]+ml0[rs]);
one[k]=one[ls]+one[rs];
}
void pushdown(int L,int R,int k)//上传
{
int ls,rs,mid;
ls=k<<1;
rs=ls|1;
mid=(L+R)>>1;
//printf("mark %d->%d and %d->%d %d\n",L,mid,mid+1,R,sta[k]);
if(sta[k]==0)
{
sta[ls]=sta[rs]=0;
ma0[ls]=ml0[ls]=mr0[ls]=mid-L+1;
ma1[ls]=ml1[ls]=mr1[ls]=one[ls]=ma1[rs]=ml1[rs]=mr1[rs]=one[rs]=0;
ma0[rs]=ml0[rs]=mr0[rs]=R-mid; }
else if(sta[k]==1)
{
sta[ls]=sta[rs]=1;
ma0[ls]=ml0[ls]=mr0[ls]=ma0[rs]=ml0[rs]=mr0[rs]=0;
ma1[ls]=ml1[ls]=mr1[ls]=one[ls]=mid-L+1;
ma1[rs]=ml1[rs]=mr1[rs]=one[rs]=R-mid;
}
else
{
if(sta[ls]!=-1)//2操作对于0,1标记直接0,1标记互换
{
if(sta[ls]==2)//原先有2直接变-1
sta[ls]=-1;
else
sta[ls]^=1;
}
else
sta[ls]=2;
if(sta[rs]!=-1)
{
if(sta[rs]==2)
sta[rs]=-1;
else
sta[rs]^=1;
}
else
sta[rs]=2;
opp(L,mid,ls);
opp(mid+1,R,rs);
}
sta[k]=-1;
}
void btree(int L,int R,int k)
{
int ls,rs,mid;
sta[k]=-1;
if(L==R)
{
scanf("%d",&one[k]);
if(one[k])
{
ma1[k]=ml1[k]=mr1[k]=1;
ma0[k]=ml0[k]=mr0[k]=0;
}
else
{
ma1[k]=ml1[k]=mr1[k]=0;
ma0[k]=ml0[k]=mr0[k]=1;
}
return ;
}
ls=k<<1;
rs=ls|1;
mid=(L+R)>>1;
btree(L,mid,ls);
btree(mid+1,R,rs);
pushup(L,R,k);
//printf("%d->%d\n",L,R);
//printf("%d---%d----%d\n",ml1[k],mr1[k],one[k]);
}
void update(int L,int R,int l,int r,int k,int op)
{
int ls,rs,mid;
if(l==L&&r==R)
{
if(op==0)
{
sta[k]=0;
ma0[k]=ml0[k]=mr0[k]=R-L+1;
ma1[k]=ml1[k]=mr1[k]=one[k]=0;
}
else if(op==1)
{
sta[k]=1;
ma0[k]=ml0[k]=mr0[k]=0;
ma1[k]=ml1[k]=mr1[k]=one[k]=R-L+1;
}
else
{
if(sta[k]==-1)
sta[k]=2;
else if(sta[k]==2)
sta[k]=-1;
else
sta[k]^=1;
opp(L,R,k);
}
//printf("mark %d->%d %d\n",L,R,op);
//printf("%d->%d\n",L,R);
//printf("%d---%d----%d\n",ml1[k],mr1[k],one[k]);
return ;
}
if(sta[k]!=-1)
pushdown(L,R,k);
ls=k<<1;
rs=ls|1;
mid=(L+R)>>1;
if(l>mid)
update(mid+1,R,l,r,rs,op);
else if(r<=mid)
update(L,mid,l,r,ls,op);
else
{
update(L,mid,l,mid,ls,op);
update(mid+1,R,mid+1,r,rs,op);
}
pushup(L,R,k);
//printf("%d->%d\n",L,R);
//printf("%d---%d----%d\n",ml1[k],mr1[k],one[k]);
}
int qu(int L,int R,int l,int r,int k,int op)
{
int ls,rs,mid,tmp,ll,rr;
if(sta[k]==0)
return 0;
if(sta[k]==1)
return r-l+1;
if(l==L&&r==R)
{
if(op==3)
return one[k];
else
return ma1[k];
}
if(sta[k]!=-1)
pushdown(L,R,k);
ls=k<<1;
rs=ls|1;
mid=(L+R)>>1;
if(l>mid)
return qu(mid+1,R,l,r,rs,op);
else if(r<=mid)
return qu(L,mid,l,r,ls,op);
else
{
if(op==3)
return qu(L,mid,l,mid,ls,op)+qu(mid+1,R,mid+1,r,rs,op);
else//4对于分离操作尤其注意!!最大值只能是以下几种情况。
{
tmp=max(qu(L,mid,l,mid,ls,op),qu(mid+1,R,mid+1,r,rs,op));//最大值在左儿子或右儿子中
ll=max(mid-mr1[ls]+1,l);//最大值在中间区域
rr=min(mid+ml1[rs],r);//注意范围
tmp=max(tmp,rr-ll+1);
return tmp;
}
} }
int main()
{
int t,n,m,op,a,b; scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
btree(1,n,1);
while(m--)
{
scanf("%d%d%d",&op,&a,&b);
a++,b++;
if(op<3)
update(1,n,a,b,1,op);
else
printf("%d\n",qu(1,n,a,b,1,op));
}
}
return 0;
}
hdu 3397 Sequence operation(很有意思的线段树题)的更多相关文章
- 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变 ...
- hdu 3397 Sequence operation(线段树:区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3397 题意:给你一个长度为n的0,1序列,支持下列五种操作, 操作0(0 a b):将a到b这个区间的 ...
- hdu 3397 Sequence operation (线段树 区间合并 多重标记)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=3397 题意: 给你一串01串,有5种操作 0. 区间全部变为0 1.区间全部变为1 2.区间异或 3.询问 ...
- 【线段树】HDU 3397 Sequence operation 区间合并
操作 Change operations: 0 a b change all characters into '0's in [a , b] 1 a b change all characters i ...
- HDU 5919 Sequence II(可持久化线段树)
[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5919 [题目大意] 给出一个数列,每次查询数列中,区间非重元素的下标的中位数.查询操作强制在线. [ ...
- (简单) HDU 3397 Sequence operation,线段树+区间合并。
Problem Description lxhgww got a sequence contains n characters which are all '0's or '1's. We have ...
- 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 ...
- hdu 3397 Sequence operation 线段树
题目链接 给出n个数, 每个数是0或1, 给5种操作, 区间变为1, 区间变为0, 区间0,1翻转, 询问区间内1的个数, 询问区间内最长连续1的个数. 需要将数组开成二维的, 然后区间0, 1翻转只 ...
- HDU 3397 Sequence operation 多标记线段树
/* 一开始维护了两个标记 开了两个数组 想的是 可能当前两种操作都要做 但是太复杂了 不好处理 其实 当前要做的标记可以只有一个 我们在Insert的时候 要打的标记是2即翻转区间: 1.如果原来是 ...
随机推荐
- pageX, clientX ,screenX, offsetX, layerX, ,x的区别
事件对象event的位置属性,这些参数比较容易混淆 1. pageX,pageY :IE不识别的,鼠标在页面上的位置,从页面左上角开始,即是以页面为参考点,不随滑动条移动而变化,其实就是clientY ...
- python练习程序(批量重命名)
# -*- coding: cp936 -*- import sys,os,string d=0; path="F://test" srcfile=os.listdir(path) ...
- Maven加依赖包
对于初学maven的人来说刚开始会有个困惑,那就是怎么知道依赖的jar的groupId和atrifactId是什么, 比如要依赖mybatis,会在pom.xml中配置如下: <dependen ...
- Heritrix源码分析(四) 各个类说明(转)
Heritrix的类的确很繁琐,往往继承了一层又一层,最多的继承好像有7层.下面就一个包一个包的说明每个类的作用,由于里面Heritrix组件分明,很多组件没用到的同时该组件的类我也没怎么接触,所以这 ...
- 增加eclipse启动的Tomcat内存的
JAVA程序启动时JVM都会分配一个初始内存和最大内存给这个应用程序.这个初始内存和最大内存在一定程度都会影响程序的性能. 如何设置Tomcat的JVM内存大小 Tomcat本身不能直接在计算机上运行 ...
- 表单Ext.form.FormPanel(转)
1.表单 对于传统的b/s应用来说,数据录入元素是放在表单<form>标签里面的.而对于ExtJS应用来说,则可以直接使用FormPanel控件来存放表单中的元素.FormPanel继承自 ...
- NetBeansRCP-添加/修改NetBeans的JVM启动参数
NetBeans运行的速度实在是不敢恭维.还好机器配置还可以,修改其JVM启动参数命令行,以期运行的更加顺畅. 那么如何修改NetBeans IDE的JVM参数呢? 1.到NetBeans IDE的安 ...
- linux -- 串口调试总结
linux 串口输出调试 在某些情况下,需要同时对两台或多台Linux主机进行管理和操作.如果手头缺少足够多的键盘和显示器,那么通过一台机器的串口对其余主机进行控制不失为一种快捷.有效的方法. 下面就 ...
- 更新的packages.config所有的软件包?
1. 你NuGet.exe的恢复你的包.运行的每个项目. nuget install packages.config 或用NuGet的2.7可以恢复所有包中的行. nuget restore Your ...
- MSDN 中 对vector::erase()的解释.xml
pre{ line-height:1; color:#f0caa6; background-color:#2d161d; font-size:16px;}.sysFunc{color:#e54ae9; ...