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.如果原来是 ...
随机推荐
- mysql,多对多的hibernate操作对应的jdbc操作
在hibernate中oo思想操作数据库,很方便,但是需要了解一下底层的jdbcsql是怎么写的,复习 多对多的表关系,取出,id为1的学生 订阅了哪些课程? mysql> select c_n ...
- [转载] ubuntu Authentication failure
ubuntu的root用户默认是禁止的,需要手动打开才行.事实上ubuntu下的所有操作都用不到root用户,由于sudo的合理使用,避免了root用户下误操作而产生的毁灭性问题.root账号启用方法 ...
- Clusterware后台进程
Clusterware由若干进程组成,其中最重要的是CRSD,CSSD,EVMD 在Clusterware安装的最后阶段,会要求在每个节点执行root.sh脚本,这个脚本实际的作用就是在/etc/ ...
- 【转】使用ngrok快速地将本地Web服务映射到外网
为什么要使用ngrok? 作为一个Web开发者,我们有时候会需要临时地将一个本地的Web网站部署到外网,以供它人体验评价或协助调试等等,通常我们会这么做: 找到一台运行于外网的Web服务器 服务器上有 ...
- html元素中class属性值多个空格分格
问题: 比如 <div class="alert alert-info"> 回答: 同时指定了多个CSS样式,这里面的alert-info还可以换成alert-warn ...
- MySQL与Oracle 差异比较之七其它
其它 编号 类别 ORACLE MYSQL 注释 1 内连接的更改 1.select a.*, b.*, c.*, d.* from a, b, c, d where a.id = b.id a ...
- OpenCV 连接 Android IP摄像头
0.下载IP摄像头(android软件)并安装 比如这个(图标是一个灰色的摄像头的那个软件) 1.新建cpp文件,编译 #include "opencv2/opencv.hpp" ...
- JavaScript/jQuery 表单美化插件小结
Niceforms Niceforms是一款独立的表单美化工具,当前版本为2.0 官方主页:http://www.emblematiq.com/lab/niceforms/ 官方演示:http://w ...
- webstorm无法格式化
快捷键失效,一般都是由于快键键冲突引起的.但是像CTRL + ALT + L的组合件冲突,还是没见过. 后来在网上查知,网易云音乐,会引发此冲突,果然高手在网络啊. 打开设置,禁用网易云音乐快捷键,妥 ...
- DOM笔记(五):JavaScript的常见事件和Ajax小结
一.常见事件类型 1.鼠标事件 事件名称 说明 onclick 鼠标单击时触发 ondbclick 鼠标双击时触发 onmousedown 鼠标左键按下时触发 onmouseup 鼠标释放时触发 on ...