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. __init__和__new__

    一.__init__方法是什么 __init__方法通常用在初始化一个类实例的时候, class Person(object): """Silly Person" ...

  2. PHP实现生成唯一编号(36进制的不重复编号)

    当我们要将一个庞大的数据进行编号时,而编号有位数限制,比如5位的车牌号.10位的某证件号码.订单流水号.短网址等等,我们可以使用36进制计算出符合位数的不重复的编号. 我们将0-Z(012345678 ...

  3. MATLAB画图设置长宽。并高清复制

  4. python 运行报错 Process finished with exit code -1073741819 (0xC0000005)

    发现是由于openpyxl模块导致的,去掉这个模块的内容就能运行,import openpyxl就运行不起来, 将openpyxl卸载了重装, 以及更换了不同的openpyxl版本,都不行,还是运行不 ...

  5. JavaWeb 文件上传下载

    1. 文件上传下载概述 1.1. 什么是文件上传下载 所谓文件上传下载就是将本地文件上传到服务器端,从服务器端下载文件到本地的过程.例如目前网站需要上传头像.上传下载图片或网盘等功能都是利用文件上传下 ...

  6. CentOS/Linux 卸载MATLAB

    rm -rf /usr/local/MATLAB/R2012arm /usr/local/bin/matlab /usr/local/bin/mcc /usr/local/bin/mex /usr/l ...

  7. java深入探究14-lucene

    项目代码:链接:http://pan.baidu.com/s/1qXVcfCw 密码:apw1 01 回顾索引 定义:索引是对数据库表中一列或多列的值进行排序的一种结构 目的:加快对数据库表中记录的查 ...

  8. HDFS-文件写入API

    package com.zhen.hdfs; import java.io.BufferedInputStream; import java.io.FileInputStream; import ja ...

  9. 【转】一次完整的HTTP请求所经历的7个步骤

    HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: 1. 建立TCP连接 在HTTP工作开始之前,Web浏览器首先要通过网络与Web服务器建立连接,该 ...

  10. repo 小结

    repo只是google用Python脚本写的调用git的一个脚本,主要是用来下载.管理Android项目的软件仓库. 1. 下载 repo 的地址: http://android.git.kerne ...