HDU 3397 Sequence operation
题目:下列操作
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]
思路: 基础 区间 合并 不过操作比较多, 题目要求查询最长的 1 因为有异或 也要记下最长的 0
#include <iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<vector>
#define N 100050
using namespace std;
int flag[N * ], Xor[N * ], sum[N * ];
int a[N];
int lsum[][N * ], rsum[][N * ], msum[][N * ];
int max(int x, int y) {
return x > y ? x : y;
}
void pushup(int i, int l, int r) {
int mid = (l + r) >> ;
sum[i] = sum[i << ] + sum[i << | ];
for (int j = ; j < ; ++j) {
lsum[j][i] = lsum[j][i << ];
rsum[j][i] = rsum[j][i << | ];
if (lsum[j][i] == mid - l + )
lsum[j][i] += lsum[j][i << | ];
if (rsum[j][i] == r - mid)
rsum[j][i] += rsum[j][i << ];
msum[j][i] = max(msum[j][i << ], msum[j][i << | ]);
msum[j][i] = max(msum[j][i], rsum[j][i << ] + lsum[j][i << | ]);
}
}
void pushdown(int i, int l, int r) {
if (flag[i] != -) {
int mid = (l + r) >> ;
flag[i << ] = flag[i << | ] = flag[i];
Xor[i << ] = Xor[i << | ] = ;
sum[i << ] = (mid - l + ) * flag[i];
sum[i << | ] = (r - mid) * flag[i];
lsum[flag[i]][i << ] = rsum[flag[i]][i << ] = msum[flag[i]][i << ]
= mid - l + ;
lsum[flag[i]][i << | ] = rsum[flag[i]][i << | ] = msum[flag[i]][i
<< | ] = r - mid;
lsum[ - flag[i]][i << ] = rsum[ - flag[i]][i << ] = msum[
- flag[i]][i << ] = ;
lsum[ - flag[i]][i << | ] = rsum[ - flag[i]][i << | ] = msum[
- flag[i]][i << | ] = ;
flag[i] = -; }
if (Xor[i] != ) {
int mid = (l + r) >> ;
Xor[i << ] ^= ;
Xor[i << | ] ^= ;
sum[i << ] = (mid - l + ) - sum[i << ];
sum[i << | ] = (r - mid) - sum[i << | ];
swap(lsum[][i << ], lsum[][i << ]);
swap(rsum[][i << ], rsum[][i << ]);
swap(msum[][i << ], msum[][i << ]);
swap(lsum[][i << | ], lsum[][i << | ]);
swap(rsum[][i << | ], rsum[][i << | ]);
swap(msum[][i << | ], msum[][i << | ]);
Xor[i] = ;
}
} void build(int l, int r, int i) {
flag[i] = -;
Xor[i] = ;
if (l == r) {
sum[i] = flag[i] = a[l];
msum[a[l]][i] = rsum[a[l]][i] = lsum[a[l]][i] = ;
msum[ - a[l]][i] = rsum[ - a[l]][i] = lsum[ - a[l]][i] = ;
return;
}
int mid = (l + r) >> ;
build(l, mid, i << );
build(mid + , r, i << | );
pushup(i, l, r);
}
void update(int l, int r, int pl, int pr, int type, int i) {
if (l >= pl && r <= pr) {
if (type == ) {
flag[i] = ;
sum[i] = ;
Xor[i] = ;
lsum[][i] = rsum[][i] = msum[][i] = r - l + ;
lsum[][i] = rsum[][i] = msum[][i] = ;
return;
}
if (type == ) { flag[i] = ;
sum[i] = (r-l+);
Xor[i] = ;
lsum[][i] = rsum[][i] = msum[][i] = ;
lsum[][i] = rsum[][i] = msum[][i] = r-l+;
return;
}
if(type==)
{
Xor[i]^=;
sum[i]=(r-l+)-sum[i];
swap(lsum[][i],lsum[][i]);
swap(rsum[][i],rsum[][i]);
swap(msum[][i],msum[][i]);
return;
}
return;
}
pushdown(i,l,r);
int mid=(l+r)>>;
if(pl<=mid)update(l,mid,pl,pr,type,i<<);
if(pr>mid)update(mid+,r,pl,pr,type,i<<|);
pushup(i,l,r);
}
int query1(int l,int r,int pl,int pr,int i)
{
if(l>=pl&&r<=pr)
return sum[i];
pushdown(i,l,r);
int mid=(l+r)>>;
int tmp=;
if(pl<=mid)tmp+=query1(l,mid,pl,pr,i<<);
if(pr>mid)tmp+=query1(mid+,r,pl,pr,i<<|);
pushup(i,l,r);
return tmp;
} int query2(int l,int r,int pl,int pr,int i)
{
if(l>=pl&&r<=pr)
{
return msum[][i];
}
pushdown(i,l,r);
int mid=(l+r)>>;
if(pr<=mid)return query2(l,mid,pl,pr,i<<);
else if(pl>mid)return query2(mid+,r,pl,pr,i<<|);
else
{
int tmp=;
if(mid-rsum[][i<<]+>=pl)
tmp+=rsum[][i<<];
else
tmp+=mid-pl+;
if(mid+lsum[][i<<|]<=pr)
tmp+=lsum[][i<<|];
else
tmp+=pr-mid; int maxn1=query2(l,mid,pl,mid,i<<);
int maxn2=query2(mid+,r,mid+,pr,i<<|);
tmp=max(tmp,maxn1);
tmp=max(tmp,maxn2);
return tmp;
}
}
int main() {
int n,m,tt;
scanf("%d",&tt);
while(tt--)
{
scanf("%d%d",&n,&m);
for(int i=;i<n;++i)
scanf("%d",&a[i]);
n--;
build(,n,); while(m--)
{
int x,y,z;
scanf("%d%d%d",&z,&x,&y);
if(z<=)update(,n,x,y,z,);
else if(z==)
{ printf("%d\n",query1(,n,x,y,));
}
else
printf("%d\n",query2(,n,x,y,));
}
}
return ;
}
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(很有意思的线段树题)
Sequence operation Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 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(区间合并 + 区间更新)
题目链接:pid=3397">http://acm.hdu.edu.cn/showproblem.php?pid=3397 题意:给定n个数,由0,1构成.共同拥有5种操作. 每一个操 ...
- (简单) 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 (区间合并,操作比较多)
费了我一天半的时间,到处debug,后来才发现,主要是建树的时候只在叶子节点对lazy1和lazy2进行初始化了,父节点都没初始化...晕. 具体见代码吧. #include <iostream ...
- hdu 3397 Sequence operation 线段树
题目链接 给出n个数, 每个数是0或1, 给5种操作, 区间变为1, 区间变为0, 区间0,1翻转, 询问区间内1的个数, 询问区间内最长连续1的个数. 需要将数组开成二维的, 然后区间0, 1翻转只 ...
- 【线段树】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 ...
随机推荐
- Java集合---ArrayList的实现原理
目录: 一. ArrayList概述 二. ArrayList的实现 1) 私有属性 2) 构造方法 3) 元素存储 4) 元素读取 5) 元素删除 6) 调整数组容量 ...
- AJAX-----16HTML5实现大文件切割上传
2点多接了个电话导致失眠,没办法,跑起来接着板砖了...... 废话不多说,直接走码... <!DOCTYPE html> <html lang="en"> ...
- Session与cookie的区别?
1.cookie 是一种发送到客户浏览器的文本串句柄,并保存在客户机硬盘上,可以用来在某个WEB站点会话间持久的保持数据. 2.session其实指的就是访问者从到达某个特定主页到离开为止的那段时间. ...
- tomcat部署https
在server.xml配置文件中增加证书位置跟密码: <Connector port="443" protocol="org.apache.coyote.http1 ...
- PowerDesigner反向工程,根据Oracle数据库结构生成ER图(2014-3-25记)
01.添加数据服务端 02. PowerDesigner 1. 新建PDM:选择菜单File->New Model,[Model type]选择Physical Data Model,[DBMS ...
- 【request_firmware】 linux内核下载模块固件接口【转】
转自:http://blog.csdn.net/magod/article/details/6049558 [-] 8 处理固件 1481 内核固件接口 1482 它如何工作 14.8. 处理固件 作 ...
- php Windows系统 wamp集成环境下redis的使用
先说一下我的本地环境,使用的是wamp集成环境,(Apache 2.4.9.PHP 5.5.12.MySQL 5.6.17) windows下安装PHP扩展: 第一步:找到扩展文件(.dll),htt ...
- 如何阅读《ECMAScript 2015 Language Specification》
你不需要把<ECMAScript 2015 Language Specification>通读一遍,因为没那个必要. 阮一峰建议: 对于一般用户来说,除了第4章,其他章节都涉及某一方面 ...
- iOS 使用Touch ID 校验[新增 iOS9 三种错误]
iOS8后苹果开放了Touch ID的API给开发者,这也给我们的app带来了新的体验.开发者们可使用向第三方应用开放了Touch ID权限的API,以便他们在应用中使用指纹认证来完成用户认证或支付购 ...
- java 代理模式二:动态代理
java动态代理: java动态代理类位于java.lang.reflect包下,一般主要涉及两个类: 1.Interface InvocationHandler 该接口中仅定义了一个方法:Objec ...