题目:下列操作

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的更多相关文章

  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(很有意思的线段树题)

    Sequence operation Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  3. hdu 3397 Sequence operation(线段树:区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3397 题意:给你一个长度为n的0,1序列,支持下列五种操作, 操作0(0 a b):将a到b这个区间的 ...

  4. hdu 3397 Sequence operation (线段树 区间合并 多重标记)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=3397 题意: 给你一串01串,有5种操作 0. 区间全部变为0 1.区间全部变为1 2.区间异或 3.询问 ...

  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,线段树+区间合并。

    Problem Description lxhgww got a sequence contains n characters which are all '0's or '1's. We have ...

  7. HDU 3397 Sequence operation (区间合并,操作比较多)

    费了我一天半的时间,到处debug,后来才发现,主要是建树的时候只在叶子节点对lazy1和lazy2进行初始化了,父节点都没初始化...晕. 具体见代码吧. #include <iostream ...

  8. hdu 3397 Sequence operation 线段树

    题目链接 给出n个数, 每个数是0或1, 给5种操作, 区间变为1, 区间变为0, 区间0,1翻转, 询问区间内1的个数, 询问区间内最长连续1的个数. 需要将数组开成二维的, 然后区间0, 1翻转只 ...

  9. 【线段树】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 ...

随机推荐

  1. HttpClient_002_中文乱码、HttpClient中文乱码透析、总结

    中文乱码原理代码: String s = "中文"; byte[] bs2 = s.getBytes("utf-8");//将s拆成:utf-8个体,注:utf ...

  2. 手机触摸touch事件

    1.Touch事件简介 pc上的web页面鼠 标会产生onmousedown.onmouseup.onmouseout.onmouseover.onmousemove的事件,但是在移动终端如 ipho ...

  3. pb数据窗口设置操作

    1 使DataWindow列只能追加不能修改如何使DataWindow中的数据只能追加新记录而不能修改,利用 Column 的 Protect 属性可以很方便的做到这一点,方法如下:将每一列的 Pro ...

  4. 一个注解方式webSocket demo

    前段时间在研究websocket.其中遇到了一些bug.这里跟大家分享这过程. 首先介绍一下websocket WebSocket是HTML5的一种新协议,实现了浏览器和服务器的双全工通信,能更好的节 ...

  5. Java虚拟机学习(5):类加载器(ClassLoader

    类加载器 类加载器(ClassLoader)用来加载 class字节码到 Java 虚拟机中.一般来说,Java 虚拟机使用 Java 类的方式如下:Java 源文件在经过 Javac之后就被转换成 ...

  6. Duilib实现QQ聊天窗口晃动

    转载:http://blog.csdn.net/arbboter/article/details/26282717 转载:http://blog.csdn.net/zerolusta/article/ ...

  7. Bootstrap_导航条

    一.基础导航条 在制作一个基础导航条时,主要分以下几步: 第一步:首先在制作导航的列表(<ul class=”nav”>)基础上添加类名“navbar-nav” 第二步:在列表外部添加一个 ...

  8. c++ 指针常量,常量指针

    当const遇到指针 一般来说,const修饰指针可以分为下面的集中情况. 描述 例子 含义 备注 const在*的左边 const int *b=&a; int const *b=& ...

  9. XML与JSON的转换

    -(void)test {          //XML文本范例     NSString *testXMLString = @"Cake0.55RegularChocolateBluebe ...

  10. jsp表格数据导出到Execl

    1.关于“下载” 需要设置页面header的一个属性为:Content-Disposition: attachment; filename=下载的文件.txt 如: <a href=" ...