Problem Description
  lxhgww got a sequence contains n characters which are all '0's or '1's.
  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]
 
  典型的水题,对一段01序列进行区间覆盖和反转,维护1的个数,连续1的个数,前缀1的个数,后缀1的个数,以及连续0,前缀0,后缀0,用来计算反转时的1.
  不过还是写错了个地方,在程序里面标记出来了。
 
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring> #define lc po*2
#define rc po*2+1
#define lson L,M,lc
#define rson M+1,R,rc using namespace std; const int maxn=1e5+; int sum[maxn*],lsum[][maxn*],rsum[][maxn*],msum[][maxn*];
int COL[maxn*],XOR[maxn*]; inline void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
} void pushUP(int po,int len)
{
sum[po]=sum[lc]+sum[rc]; for(int i=;i<;++i)
{
msum[i][po]=max(msum[i][lc],msum[i][rc]);
msum[i][po]=max(msum[i][po],lsum[i][rc]+rsum[i][lc]); lsum[i][po]=lsum[i][lc];
if(lsum[i][lc]==(len-(len/)))
lsum[i][po]+=lsum[i][rc]; rsum[i][po]=rsum[i][rc];
if(rsum[i][rc]==(len/))
rsum[i][po]+=rsum[i][lc];
}
} void pushDown(int po,int len)
{
if(COL[po]!=-)
{
COL[lc]=COL[rc]=COL[po];
XOR[lc]=XOR[rc]=; sum[lc]=COL[po]*(len-(len/));
sum[rc]=COL[po]*(len/); for(int i=;i<;++i)
{
msum[i][lc]=lsum[i][lc]=rsum[i][lc]=(i ? sum[lc] : len-(len/)-sum[lc]);
msum[i][rc]=rsum[i][rc]=lsum[i][rc]=(i ? sum[rc] : (len/)-sum[rc]);
} COL[po]=-;
} if(XOR[po])
{
XOR[lc]=!XOR[lc];
XOR[rc]=!XOR[rc]; sum[lc]=len-(len/)-sum[lc];
sum[rc]=(len/)-sum[rc]; swap(msum[][lc],msum[][lc]);
swap(msum[][rc],msum[][rc]); swap(lsum[][lc],lsum[][lc]);
swap(rsum[][lc],rsum[][lc]); swap(lsum[][rc],lsum[][rc]);
swap(rsum[][rc],rsum[][rc]); XOR[po]=;
}
} void build_tree(int L,int R,int po)
{
COL[po]=-;
XOR[po]=; if(L==R)
{
int temp;
scanf("%d",&temp); sum[po]=temp;
lsum[][po]=rsum[][po]=msum[][po]=-temp;
lsum[][po]=rsum[][po]=msum[][po]=temp; return;
} int M=(L+R)/; build_tree(lson);
build_tree(rson); pushUP(po,R-L+);
} void update_col(int ul,int ur,int ut,int L,int R,int po)
{
if(ul<=L&&ur>=R)
{
COL[po]=ut;
XOR[po]=; sum[po]=ut*(R-L+);
lsum[][po]=rsum[][po]=msum[][po]=sum[po];
lsum[][po]=rsum[][po]=msum[][po]=R-L+-sum[po]; return;
} pushDown(po,R-L+); int M=(L+R)/; if(ul<=M)
update_col(ul,ur,ut,lson);
if(ur>M)
update_col(ul,ur,ut,rson); pushUP(po,R-L+);
} void update_xor(int ul,int ur,int L,int R,int po)
{
if(ul<=L&&ur>=R)
{
XOR[po]=!XOR[po]; sum[po]=R-L+-sum[po];
swap(msum[][po],msum[][po]);
swap(lsum[][po],lsum[][po]);
swap(rsum[][po],rsum[][po]); return;
} pushDown(po,R-L+); int M=(L+R)/; if(ul<=M)
update_xor(ul,ur,lson);
if(ur>M)
update_xor(ul,ur,rson); pushUP(po,R-L+);
} int query_sum(int ql,int qr,int L,int R,int po)
{
if(ql<=L&&qr>=R)
return sum[po]; pushDown(po,R-L+); int M=(L+R)/; if(qr<=M)
return query_sum(ql,qr,lson);
if(ql>M)
return query_sum(ql,qr,rson); return query_sum(ql,qr,lson)+query_sum(ql,qr,rson);
} int query_max(int ql,int qr,int L,int R,int po)
{
if(ql<=L&&qr>=R)
return msum[][po]; pushDown(po,R-L+); int M=(L+R)/;
int ans=; if(qr<=M)
return query_max(ql,qr,lson);
if(ql>M)
return query_max(ql,qr,rson); ans=max(query_max(ql,qr,lson),query_max(ql,qr,rson));
ans=max(ans,min(rsum[][lc],M-ql+)+min(lsum[][rc],qr-M)); //!!! return ans;
} int main()
{
int T;
int N,M;
int a,b,c;
cin>>T; while(T--)
{
scanf("%d %d",&N,&M); build_tree(,N-,); while(M--)
{
scanf("%d %d %d",&a,&b,&c); switch(a)
{
case :
update_col(b,c,,,N-,);
break;
case :
update_col(b,c,,,N-,);
break;
case :
update_xor(b,c,,N-,);
break;
case :
printf("%d\n",query_sum(b,c,,N-,));
break;
case :
printf("%d\n",query_max(b,c,,N-,));
break;
}
}
} return ;
}

(简单) HDU 3397 Sequence operation,线段树+区间合并。的更多相关文章

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

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

  2. 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 ...

  3. (简单) HDU 3308 LCIS,线段树+区间合并。

    Problem Description Given n integers. You have two operations: U A B: replace the Ath number by B. ( ...

  4. hdu 3397 Sequence operation 线段树

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

  5. HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举

    HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...

  6. Sequence operation(线段树区间多种操作)

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

  7. HDU 5316——Magician——————【线段树区间合并区间最值】

    Magician Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  8. HDU 1540 Tunnel Warfare 线段树区间合并

    Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...

  9. hdu 4453 约会安排(线段树区间合并)

    约会安排 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submis ...

随机推荐

  1. opencv---cvor

    void cvXor计算两个数组中的每个元素的按位异或. void cvXor (const CvArr* src1, const CvArr* src2, CvArr* dst, const CvA ...

  2. java代码用dom4j解析xml文件的简单操作

    时间: 2016/02/17 目标:为telenor的ALU Femto接口写一个采集xml文件并解析出locationName标签里的值,然后更新到数据库中. 从网上搜了下,有四种常用的解析xml的 ...

  3. php 四种基础的算法 ---- 冒泡排序法

    1. 冒泡排序法  *     思路分析:法如其名,就是像冒泡一样,每次从数组当中 冒一个最大的数出来.  *     比如:2,4,1    // 第一次 冒出的泡是4  *             ...

  4. MonkeyRunner 实现自动点击截屏后与本地图库进行对比输出

    先说下本人是菜鸟,通过网上资料学习,终于调通了MonkeyRunner 实现自动点击截屏后与本地图库进行对比输出,以后做静态UI测试就不需要眼睛盯着看图了,这一切交给MonkeyRunner了. 首先 ...

  5. cocos2d3.8.1 使用prebuild提升发布android速度

    1.生成cocos prebuild库 cocos gen-libs -m debug或 cocos gen-libs -m release 2.使用命令创建test项目 cocos new test ...

  6. Windows编译安装mod_wsgi,配合使用Django+Apahce

    编译环境: 均是32位版本 Microsoft Visual Studio 10.0 Microsoft SDKs v7.1 Apache2.4 Python3.4 mod_wsgi-4.5.0 要求 ...

  7. .Net 中的反射(查看基本类型信息)

    反射概述 和Type类 1.反射的作用 简单来说,反射提供这样几个能力:1.查看和遍历类型(及其成员)的基本信息和程序集元数据(metadata):2.迟绑定(Late-Binding)方法和属性.3 ...

  8. Struts2 语法--验证方式:

    第一种方式: 重写validation方法, ====验证action中所有的方法: 1. 在UserAction1里重写validation: @Override public void valid ...

  9. PHP字节格式化

    /** * 容量转换 * @param string $value 字节数值 * @return string */function GBKB($value) { $size = ($value &g ...

  10. C# 经典入门12章-使用泛型类型-2