HDU - 5306: Gorgeous 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 (势能线段树)的更多相关文章
- HDU 5306 Gorgeous Sequence[线段树区间最值操作]
Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU - 5306 Gorgeous Sequence (吉司机线段树)
题目链接 吉司机线段树裸题... #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f3 ...
- 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 ...
- HDU 6047 Maximum Sequence(贪心+线段树)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...
- 2017ACM暑期多校联合训练 - Team 2 1003 HDU 6047 Maximum Sequence (线段树)
题目链接 Problem Description Steph is extremely obsessed with "sequence problems" that are usu ...
- [HDU] 5306 Gorgeous Sequence [区间取min&求和&求max]
题解: 线段树维护区间取min求和求max 维护最小值以及个数,次小值 标记清除时,分情况讨论 当lazy>max1 退出 当max1>lazy>max2(注意不要有等号) 更新 否 ...
- Gorgeous Sequence(HDU5360+线段树)
题目链接 传送门 题面 思路 对于线段树的每个结点我们存这个区间的最大值\(mx\).最大值个数\(cnt\).严格第二大数\(se\),操作\(0\): 如果\(mx\leq val\)则不需要更新 ...
- Gorgeous Sequence(线段树)
Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- 【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 题解:首先很明显就是线段树裸题,那么考虑如何维护 ...
- HDU - 5306 Gorgeous Sequence 线段树 + 均摊分析
Code: #include<algorithm> #include<cstdio> #include<cstring> #define ll long long ...
随机推荐
- 【整理学习Hadoop】H D F S 一个分布式文件系统
Hadoop分布式文件系统(HDFS)被设计成适合运行在通用硬件(commodity hardware)上的分布式文件系统.它和现有的分布式文件系统有很多共同点.但同时,它和其他的分布式文件系统的区别 ...
- margin无法居中原因
1.要给居中的元素一个宽度,否者无效. 2.该元素一定不能浮动,否者无效. 3 在HTML中使用标签,需考虑好整体构架,否者全部元素都会居中的.
- 每天一个Linux命令(50)netstat命令
netstat命令用来打印Linux中网络系统的状态信息,可让你得知整个Linux系统的网络情况. (1)用法: 用法: netstat [选项参数] (2)功能: ...
- MACHINE_START-内核板级初始化实现机制(linux3.1.0)
转:https://blog.csdn.net/charliewangg12/article/details/41518549 在驱动开发时,我们都是以一块开发板为基础移植驱动程序.每一块开发板对应一 ...
- 【Head First Servlets and JSP】笔记7:如何创建一个全局的dog?
重定向与请求分派 “局部”参数——ServletConfig——servlet初始化参数 “全局”参数——ServletContext——上下文初始化参数 Web app的“构造器”——Servlet ...
- com.android.dex.DexIndexOverflowException: Cannot merge new index 66299 into a non-jumbo instruction
打包时控制台输出: Error:Execution failed for task ':app:transformClassesWithDexForAll32Release'. > com.an ...
- JVM原理(Java代码编译和执行的整个过程+JVM内存管理及垃圾回收机制)
转载注明出处: http://blog.csdn.net/cutesource/article/details/5904501 JVM工作原理和特点主要是指操作系统装入JVM是通过jdk中Java.e ...
- linux中获取堆栈空间大小的方法
#include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include < ...
- MySQL数据库基本操作(三)
MySQL补充: mysql是关系型数据库,关系数据库,是建立在关系模型基础上的数据库,现实世界中的各种实体,以及实体之间的各种联系,均用关系模型(table)来表示.#关系模型就是指二维表格模型,因 ...
- Maven webapp index.jsp报错
javax.servlet javax.servlet-api 3.1.0