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. split和join函数的比较

    关于split和join方法 处理对象字符串.split拆分字符串,join连接字符串 string.join(sep): 以string作为分隔符,将seq中的所有元素(字符串表示)合并成一个新的字 ...

  2. 取消svn版本控制

    删除项目中的版本控制,即删除项目文件夹下的所有.svn文件夹(在做项目的时候,有时候想把项目拷回去看看,但是文件太大了,而且压缩起来很慢.这主要是因为在项目中有大量的.svn文件夹.所以才想把.svn ...

  3. 通过intent启动Activity

    android应用程序内部通过Intent来实现Activity间的跳转.也知道通过Intent调用系统程序.但若想在应用程序A内开启应用程序B(前提是A.B均已安装),该如何去实现? 记录下实现过程 ...

  4. libMobileGestalt与UDID

    libMobileGestalt与UDID 没有评论 在iOS中,libMobileGestalt动态库, 用来取得各种系统变量,比如UDID, 磁盘使用量, 设备版本 在iOS7中,对于开发者来说, ...

  5. Bom2016/4/21

    Browser Object Model 浏览器对象模型 open(地址,打开方式) open('http://www.baidu.com','_self')本窗口打开百度 open('http:// ...

  6. JSch - Java实现的SFTP

    JSch - Java实现的SFTP(文件上传详解篇)     JSch是Java Secure Channel的缩写.JSch是一个SSH2的纯Java实现.它允许你连接到一个SSH服务器,并且可以 ...

  7. 第11章 类的高级特性--final

    1.final变量 (1)final关键字可用于变量声明,一旦该变量被设定,就不可以再改变该变量的值.通常,由final定义的变量为常量.例如:final double PI=3.14; final关 ...

  8. zabbix企业应用之bind dns监控(转)

    继续介绍zabbix监控企业应用的实例,本次介绍zabbix监控dns,我监控的dns为bind 9.8.2,本dns为公网dns,是为了解决公司内网服务器自动化所需求的dns解析,比如目前的pupp ...

  9. 详细讲解MOSFET管驱动电路(转)

    作者:   来源:电源网 关键字:MOSFET 结构 开关 驱动电路 在使用MOS管设计开关电源或者马达驱动电路的时候,大部分人都会考虑MOS的导通电阻,最大电压等,最大电流等,也有很多人仅仅考虑这些 ...

  10. CentOS查询 杀死进程

    ps aux | grep XXX 查询进程 ps a 显示现行终端机下的所有程序,包括其他用户的程序. ps -A 显示所有程序. ps c 列出程序时,显示每个程序真正的指令名称,而不包含路径,参 ...