Gorgeous Sequence
来源:互联网 发布:墨菲斯 知乎 编辑:程序博客网 时间:2024/06/10 17:19
There is a sequence a a of length n n. We use ai ai to denote the i i-th element in this sequence. You should do the following three types of operations to this sequence.
0 x y t 0 x y t: For every x≤i≤y x≤i≤y, we use min(ai,t) min(ai,t) to replace the original ai ai's value.
1 x y 1 x y: Print the maximum value of ai ai that x≤i≤y x≤i≤y.
2 x y 2 x y: Print the sum of ai ai that x≤i≤y x≤i≤y.
The first line contains two integers
The second line contains
Each of the following
It is guaranteed that
15 51 2 3 4 51 1 52 1 50 3 5 31 1 52 1 5
515312
题解:建立一个sec[]记录第二大的元素,当Max>val>sec时,可以进行延迟修改。
#include<iostream>#include<stdio.h>#include<algorithm>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define root 1,n,1typedef long long ll;using namespace std;const int Maxn=1e6+10;int Max[Maxn<<2],sec[Maxn<<2],num[Maxn<<2];ll sum[Maxn<<2];void dec_tag(int rt,int val){ if(Max[rt]<=val) return; sum[rt]-=1ll*num[rt]*(Max[rt]-val),Max[rt]=val;}void PushUp(int rt){ int l=rt<<1,r=rt<<1|1; sum[rt]=sum[l]+sum[r]; sec[rt]=max(max(sec[l],sec[r]),Max[l]==Max[r]?0:min(Max[l],Max[r])); Max[rt]=max(Max[l],Max[r]); num[rt]=0; if(Max[rt]==Max[l]) num[rt]+=num[l]; if(Max[rt]==Max[r]) num[rt]+=num[r];}void PushDown(int rt){ dec_tag(rt<<1,Max[rt]); dec_tag(rt<<1|1,Max[rt]);}void Build(int l,int r,int rt){ sec[rt]=-1; if(l==r){ scanf("%d",&Max[rt]); sum[rt]=(long long)Max[rt]; num[rt]=1; return; } int m=l+r>>1; Build(lson); Build(rson); PushUp(rt);}void Updata(int L,int R,int val,int l,int r,int rt){ if(val>=Max[rt]) return; if(L<=l&&r<=R&&val>sec[rt]){ dec_tag(rt,val); return; } PushDown(rt); int m=l+r>>1; if(L<=m) Updata(L,R,val,lson); if(R>m) Updata(L,R,val,rson); PushUp(rt);}int QueryMax(int L,int R,int l,int r,int rt){ if(L<=l&&r<=R) return Max[rt]; PushDown(rt); int m=l+r>>1; int ans=0; if(L<=m) ans=max(ans,QueryMax(L,R,lson)); if(R>m) ans=max(ans,QueryMax(L,R,rson)); return ans;}ll QuerySum(int L,int R,int l,int r,int rt){ if(L<=l&&r<=R) return sum[rt]; PushDown(rt); int m=l+r>>1; ll ans=0; if(L<=m) ans+=QuerySum(L,R,lson); if(R>m) ans+=QuerySum(L,R,rson); return ans;}int main(){ int t,m,n;scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); Build(root); while(m--){ int a,b,c,d; scanf("%d",&a); if(a==0){ scanf("%d%d%d",&b,&c,&d); Updata(b,c,d,root); } else if(a==1){ scanf("%d%d",&b,&c); printf("%d\n",QueryMax(b,c,root)); } else{ scanf("%d%d",&b,&c); printf("%lld\n",QuerySum(b,c,root)); } } }}
阅读全文
0 0
- Gorgeous Sequence
- Gorgeous Sequence
- hdu 5306 Gorgeous Sequence
- hdu 5306 Gorgeous Sequence
- hdu5306 Gorgeous Sequence
- HDU 5306 Gorgeous Sequence
- [题解]hdu5306 Gorgeous Sequence
- hdu 5306 Gorgeous Sequence
- HDOJ 5306 Gorgeous Sequence 线段树
- hdu 5306 Gorgeous Sequence(线段树)
- HDU 5306 Gorgeous Sequence(线段树)
- HDU 5306 Gorgeous Sequence 线段树
- HDU 5306 Gorgeous Sequence(线段树)
- 【Gorgeous】
- [线段树 区间最值操作] HDU 5306 Gorgeous Sequence
- hdu5306 Gorgeous Sequence(线段树:复杂区间更新)
- [HDU] 5306 Gorgeous Sequence [区间取min&求和&求max][线段树]
- It's a gorgeous day!
- 离散题目18
- Qualcomm 平台触摸屏驱动移植 笔记
- Git 工作区、暂存区和版本库
- splice和slice的区别
- VUE2.0 全套 demo 讲解 基础4(条件渲染)
- Gorgeous Sequence
- jquery.countdown.js一个时间倒计时的插件
- MongoDB常用命令总结
- Java基础
- 【Swift】文档分享到其他应用
- 整理一下vim命令的基本用法
- Vue2.0进阶组件篇1 教你秒撸(短信倒计时组件)
- 数据库范式-表结构设计规范
- 个人工作笔记(20170522-20170527)