There is a sequence aa of length nn. We use aiai to denote the ii-th element in this sequence. You should do the following three types of operations to this sequence.

0 x y t0 x y t: For every x≤i≤yx≤i≤y, we use min(ai,t)min(ai,t) to replace the original aiai's value. 
1 x y1 x y: Print the maximum value of aiai that x≤i≤yx≤i≤y. 
2 x y2 x y: Print the sum of aiai that x≤i≤yx≤i≤y. 

InputThe first line of the input is a single integer TT, indicating the number of testcases.

The first line contains two integers nn and mm denoting the length of the sequence and the number of operations.

The second line contains nn separated integers a1,…,ana1,…,an (∀1≤i≤n,0≤ai<231∀1≤i≤n,0≤ai<231).

Each of the following mm lines represents one operation (1≤x≤y≤n,0≤t<2311≤x≤y≤n,0≤t<231).

It is guaranteed that T=100T=100, ∑n≤1000000, ∑m≤1000000∑n≤1000000, ∑m≤1000000.OutputFor every operation of type 11 or 22, print one line containing the answer to the corresponding query. 
Sample Input

1
5 5
1 2 3 4 5
1 1 5
2 1 5
0 3 5 3
1 1 5
2 1 5

Sample Output

5
15
3
12

题意:三种操作,0区间min操作;1区间求max;2区间求和。

思路:segments tres beats模板题。 因为都是区间操作,所以有很多相同的值,我们记录每个区间的最大值,最大值的数量,以及第二大值。然后就可以搞了,不用lazy,下推的时候如果mx[Now]比儿子还小,那么久自然的下推即可。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=;
int mx[maxn<<],mc[maxn<<],se[maxn<<];
ll sum[maxn<<];
void pushdown(int Now)
{
if(mx[Now]<mx[Now<<]){
sum[Now<<]-=(ll)(mx[Now<<]-mx[Now])*mc[Now<<];
mx[Now<<]=mx[Now];
}
if(mx[Now]<mx[Now<<|]){
sum[Now<<|]-=(ll)(mx[Now<<|]-mx[Now])*mc[Now<<|];
mx[Now<<|]=mx[Now];
}
}
void pushup(int Now)
{
sum[Now]=sum[Now<<]+sum[Now<<|];
mx[Now]=max(mx[Now<<],mx[Now<<|]);
if(mx[Now<<]==mx[Now<<|]){
mc[Now]=mc[Now<<]+mc[Now<<|];
se[Now]=max(se[Now<<],se[Now<<|]);
}
else {
if(mx[Now<<]>mx[Now<<|]){
mc[Now]=mc[Now<<];
se[Now]=max(mx[Now<<|],se[Now<<]);
}
else {
mc[Now]=mc[Now<<|];
se[Now]=max(mx[Now<<],se[Now<<|]);
}
}
}
void build(int Now,int L,int R)
{
if(L==R){
scanf("%d",&mx[Now]);
sum[Now]=mx[Now]; se[Now]=;
mc[Now]=; return ;
}
int Mid=(L+R)>>;
build(Now<<,L,Mid); build(Now<<|,Mid+,R);
pushup(Now);
}
ll querysum(int Now,int L,int R,int l,int r){
if(l<=L&&r>=R) return sum[Now];
int Mid=(L+R)>>; ll res=;
pushdown(Now);
if(l<=Mid) res+=querysum(Now<<,L,Mid,l,r);
if(r>Mid) res+=querysum(Now<<|,Mid+,R,l,r);
return res;
}
int querymax(int Now,int L,int R,int l,int r){
if(l<=L&&r>=R) return mx[Now];
int Mid=(L+R)>>,res=-;
pushdown(Now);
if(l<=Mid) res=max(res,querymax(Now<<,L,Mid,l,r));
if(r>Mid) res=max(res,querymax(Now<<|,Mid+,R,l,r));
return res;
}
void update(int Now,int L,int R,int l,int r,int x)
{
if(mx[Now]<=x) return ;
if(l<=L&&r>=R){
if(se[Now]<x){
sum[Now]-=(ll)mc[Now]*(mx[Now]-x);
mx[Now]=x;
return ;
}
}
int Mid=(L+R)>>;
pushdown(Now);
if(l<=Mid) update(Now<<,L,Mid,l,r,x);
if(r>Mid) update(Now<<|,Mid+,R,l,r,x);
pushup(Now);
}
int main()
{
int T,N,M,L,R,opt,x;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&M);
build(,,N);
while(M--){
scanf("%d%d%d",&opt,&L,&R);
if(opt==) printf("%d\n",querymax(,,N,L,R));
else if(opt==) printf("%lld\n",querysum(,,N,L,R));
else {
scanf("%d",&x);
update(,,N,L,R,x);
}
}
}
return ;
}

HDU - 5306: Gorgeous Sequence (势能线段树)的更多相关文章

  1. HDU 5306 Gorgeous Sequence[线段树区间最值操作]

    Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  2. HDU - 5306 Gorgeous Sequence (吉司机线段树)

    题目链接 吉司机线段树裸题... #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f3 ...

  3. 2015 Multi-University Training Contest 2 hdu 5306 Gorgeous Sequence

    Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  4. HDU 6047 Maximum Sequence(贪心+线段树)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...

  5. 2017ACM暑期多校联合训练 - Team 2 1003 HDU 6047 Maximum Sequence (线段树)

    题目链接 Problem Description Steph is extremely obsessed with "sequence problems" that are usu ...

  6. [HDU] 5306 Gorgeous Sequence [区间取min&求和&求max]

    题解: 线段树维护区间取min求和求max 维护最小值以及个数,次小值 标记清除时,分情况讨论 当lazy>max1 退出 当max1>lazy>max2(注意不要有等号) 更新 否 ...

  7. Gorgeous Sequence(HDU5360+线段树)

    题目链接 传送门 题面 思路 对于线段树的每个结点我们存这个区间的最大值\(mx\).最大值个数\(cnt\).严格第二大数\(se\),操作\(0\): 如果\(mx\leq val\)则不需要更新 ...

  8. Gorgeous Sequence(线段树)

    Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  9. 【HDU5306】【DTOJ2481】Gorgeous Sequence【线段树】

    题目大意:给你一个序列a,你有三个操作,0: x y t将a[x,y]和t取min:1:x y求a[x,y]的最大值:2:x y求a[x,y]的sum 题解:首先很明显就是线段树裸题,那么考虑如何维护 ...

  10. HDU - 5306 Gorgeous Sequence 线段树 + 均摊分析

    Code: #include<algorithm> #include<cstdio> #include<cstring> #define ll long long ...

随机推荐

  1. mysql 批量更新多条记录(且不同值)的实现方法

    mysql更新语句很简单,更新多条数据的某个字段为相同值,一般这样写: UPDATE table_name SET field = 'value' WHERE condition; 更新多条数据为不同 ...

  2. 011_用eclipse打开hadoop1.2.1源码出现The method getSplits(JobConf, int) is undefined for the type Object错误

    出现的错误截图: 此时Eclipse使用的jdk1.8,将编译环境改成jdk1.7就行了,解决. 没问题了. 下面观点是参考如下网址,未经验证.参考:http://blog.csdn.net/onei ...

  3. Raspberry Pi开发之旅-土壤湿度检测

    一.土壤传感器 传感器四个针脚:  针脚 含义 AO 模拟信号输出 DO 数字信号输出 GND 电源负极 VCC 电源正极 二.接线 YL-38和YL69 之间直接用2根母对母线连接. YL-38和树 ...

  4. UCOS2_STM32F1移植详细过程

    源:UCOS2_STM32F1移植详细过程(三) UCOS2_STM32移植过程.系统内核.事件描述(汇总)

  5. 离乡与理想 Demo

    离乡与理想---理想不分黑白 踏上了火车,离开家乡背着幻梦,站累了有人叫我幸福,叫我快乐而我是没有名字的流浪的日子,已数不清有多少带着理想,一路飞翔路上遇见困难,一定要坚强因为理想已离乡 我为理想而离 ...

  6. 一句white-space:nowrap解决IE6,IE7下浮动元素不自动换行

    一句white-space:nowrap解决IE6,IE7下浮动元素不自动换行

  7. 3.6《深入理解计算机系统》笔记(四)虚拟存储器,malloc,垃圾回收【插图】

    概述 ●我们电脑上运行的程序都是使用虚拟存储,跟物理内存根本不搭边. ●既然虚拟内存是在磁盘上的,为什么它又运行这么好,并没有感觉卡顿?这要感谢程序的局部性! ●虚拟存储器的调度是一个操作系统必须做好 ...

  8. MongoDB快速入门(四)- 插入文档

    插入文档 将数据插入到MongoDB集合,需要使用MongoDB 的 insert() 方法. 语法 insert()命令的基本语法如下: >db.COLLECTION_NAME.insert( ...

  9. 关于发邮件报错535 Error:authentication failed&553 authentication is required

    553 authentication is required:这个错误的意思是你必须需要认证. 也就是说,你连接smtp服务器的时候必须使用密码来连接:下面代码红色那句 代码: @Override p ...

  10. Linux自定义别名alias重启失效问题

    Linux上的别名功能非常方便,例如ll可以显示文件列表的长信息,但是却不是以human能读懂的方式显示,所以我尝试直接在命令行中自定义一个别名: alisa lk='ls -lh' 然后lk就能正常 ...