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 ...
随机推荐
- render httprequest
render def my_view(request):# View code here... t = loader.get_template('myapp/index.html') c = Requ ...
- loadrunner脚本篇——Run-time Settings之ContentCheck
运用场景(很少用到): ContentCheck的设置可用来让VuGen检测存在错误的站点页面.如果被测的Web应用没有使用自定义的错误页面,那么这里不用添加规则,因为LR在回放时候,可以默认的捕捉到 ...
- 【HackerRank】 The Full Counting Sort
In this challenge you need to print the data that accompanies each integer in a list. In addition, i ...
- python配置文件操作
步骤: 1.导入模块 import configparser 2.创建实例 cf = configparser.ConfigParser() 3.读取配置文件,若配置文件中有中文,则需设置编码格式 ...
- GIT截图
GIT截图 今天首次成功用了GIT上传了JAVA代码,感觉一下次就能上传这么多代码,确实比在网页上方便.自己一开始根本摸不着头脑,不知道怎样使用GIT软件,但在学姐博客的指导下,在同学热情且耐心地指导 ...
- Windows批量添加和删除IP
随着天气变冷了,好多小伙伴都开始变懒了,都想用最快的方式完成任务 下面给大家介绍一下Windows批量添加和删除IP的办法 (1)批量添加IP 直接在CMD下边运行下边命令. for /l %i in ...
- CCNA 课程 二
传输层:两个重要的协议 TCP 和 UDP TCP: 面向连接的协议:在传输用户数据前,先要建立连接 (TCP的三次握手) 错误检查 数据包序列化 可靠性传输:发送的数据需要接受者提供确认,通过报头中 ...
- Fatal error: cannot create 'R_TempDir'
[user@mgmt dir]$ R Fatal error: cannot create 'R_TempDir' [user@mgmt dir]$ ll -ad /tmp drwxrwxrwt. 2 ...
- 找回J2EE 之再学习打卡记录
由于之前准备专心搞前端,就把一些java知识闲置了很久.导致...现在有点艰难. 所以!我决定,要找回他. 这是篇打卡记录.(每天一小时.监督自己!) Day1(2017-10-8.)
- hdoj1005--Number Sequence
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...