(简单) HDU 3397 Sequence operation,线段树+区间合并。
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]
#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,线段树+区间合并。的更多相关文章
- 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 线段树 区间更新 区间合并
题意: 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 ...
- (简单) HDU 3308 LCIS,线段树+区间合并。
Problem Description Given n integers. You have two operations: U A B: replace the Ath number by B. ( ...
- hdu 3397 Sequence operation 线段树
题目链接 给出n个数, 每个数是0或1, 给5种操作, 区间变为1, 区间变为0, 区间0,1翻转, 询问区间内1的个数, 询问区间内最长连续1的个数. 需要将数组开成二维的, 然后区间0, 1翻转只 ...
- HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举
HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...
- Sequence operation(线段树区间多种操作)
Sequence operation Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 5316——Magician——————【线段树区间合并区间最值】
Magician Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- HDU 1540 Tunnel Warfare 线段树区间合并
Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...
- hdu 4453 约会安排(线段树区间合并)
约会安排 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
随机推荐
- HDU2586 How far away ? 邻接表+DFS
题目大意:n个房子,m次询问.接下来给出n-1行数据,每行数据有u,v,w三个数,代表u到v的距离为w(双向),值得注意的是所修建的道路不会经过一座房子超过一次.m次询问,每次询问给出u,v求u,v之 ...
- NSArray或NSDictionary中汉字输出
1.问题: NSArray *array = [NSArray arrayWithObjects:@"我",@"在",@"鼓楼", nil] ...
- php 高并发下数据同步的问题
1.加锁缺点:降低性能优点:减少代码逻辑复杂度(题主现在这样超过1w条就删数据的逻辑,感觉看起来就点糟糕啊,如果整个系统一复杂,这样的来回写数据,你确定你的逻辑还维护得下去?建议题主梳理一下代码的逻辑 ...
- js入门实例
<!DOCTYPE html> <html> <body> <h1>My First Web Page</h1> <p>My F ...
- nagios总结二
log_file=/usr/local/nagios/var/nagios.log # 定义nagios日志文件的路径cfg_file=/usr/local/nagi ...
- acm课程练习2--1002
题目描述 Now, here is a fuction: F(x) = 6 * x^7+8x^6+7x^3+5x^2-yx (0 <= x <=100)Can you find the ...
- Python基础学习1---函数
Python函数篇 函数是重用的程序选, 他们允许给一块语句一个名称,然后可以在你的程序的任何地方是使用这个名称任意多次地运行这个语句块.这个就被称为 调用 函数.比如内建了很多高效的函数 如len ...
- 一个设置 material design icon的插件工具
一个设置 material design icon的插件工具 github地址:https://github.com/konifar/android-material-design-icon-gene ...
- L9,a cold welcome
expression: a large crowd of 一大群 in twenty minutes’time 20分钟之后 一些时间使用的介词 in two year‘s time on Satur ...
- jQuery笔记(1)
jQuery 是一个类库 拥有众多js函数的类库 jQuery 大大简化了js的书写代码,看的舒服,用的爽. jQuery 是一个数组,它能够隐性的遍历. 比如 ${"button" ...