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. JAVA-面向对象--封装

    1. 如果一个类中没有定义构造函数,会自动加上一个空参的默认构造函数 如果定义了一个构造函数,默认的空参构造函数就没有了. 2. this 当成员变量与局部变量重名的时候,可以使用this来区分.th ...

  2. Trie/最短的名字

    题目链接 /* 简单trie树的应用,注意在初始化的时候要把cnt也初始化,不然,WA! 下面的四分代码各有特点 */ //数组型,名字查询. #include<cstdio> #incl ...

  3. %3f URL --> '?'拼接引发的问题

    转载自:https://www.reddit.com/r/swift/comments/2w19kp/how_do_you_send_a_through_nsmutableurlrequest/ ho ...

  4. DHCPv6

    SLAAC(RFC4862)(StatelessAddressAutoconfiguration),无状态自动配置 IT网,http://www.it.net.cn DHCPv6包含以下两种形式: n ...

  5. Chapter 1 First Sight——11

    I didn't relate well to people my age. 我没有向人们叙述清楚我的年龄 Maybe the truth was that I didn't relate well ...

  6. JSON对象长度和遍历方法(转)

    最 近在修改一个HTML页面的JS的时候遍历JSON对象,却怎么也调试不通过.怪这个HTML网页不知道用了什么方法禁止了js错误提示,刚开始的时候不 知道有这个问题,用chrome的开发人员工具都没发 ...

  7. Nginx 502/504 Gateway time-out错误完美解决方案【转发】

      在安装完Nginx+PHP-fpm+Mysql后,跑PHP的应用会经常出现504 Gateway Time-out 或者502 Bad Gateway的情况. Nginx 504 Gateway ...

  8. Linux添加系统环境变量的两种方法

    方法一 export PATH=/usr/local/bin:$PATH 这种方法的PATH 在终端关闭 后就会消失.所以还是建议通过编辑/etc/profile来改PATH 方法二 # vim /e ...

  9. android点滴之HandlerThread的用法

    转载请注明出处:http://blog.csdn.net/lskshz/article/details/25364909 一.介绍 HandlerThread继承自Thread,当线程开启时,也就是它 ...

  10. SQL Server2008数据库中删除用户,提示数据库主体在该数据库中拥有 架构,无法删除

    一个数据库,运行在SQL Server 2008下,数据库用户无法删除,在删除时提示“数据库主体在该数据库中拥有架构,无法删除”.原因很简单,就是由于此用户在数据库中拥有某些架构的所有权,将相关架构的 ...