[题解]hdu5306 Gorgeous Sequence
来源:互联网 发布:mysql的事务隔离级别 编辑:程序博客网 时间:2024/06/10 03:45
Description
There is a sequence a of length n. We use ai to denote the i-th element in this sequence. You should do the following three types of operations to this sequence.
0 x y t: For every x≤i≤y, we use min(ai,t) to replace the original ai’s value.
1 x y: Print the maximum value of ai that x≤i≤y.
2 x y: Print the sum of ai that x≤i≤y
Input
The first line of the input is a single integer T, indicating the number of testcases.
The first line contains two integers n and m denoting the length of the sequence and the number of operations.
The second line contains n separated integers a1,…,an (∀1≤i≤n,0≤ai<231).
Each of the following m lines represents one operation (1≤x≤y≤n,0≤t<231).
It is guaranteed that T=100, ∑n≤1000000, ∑m≤1000000
Output
For every operation of type 1 or 2, print one line containing the answer to the corresponding query.
Sample Input
15 51 2 3 4 51 1 52 1 50 3 5 31 1 52 1 5
Sample Output
515312
Solution
吉司机线段树模板,区间取
每个区间记录最大值
假设当前要对
代码:
#include<cstdio>#include<algorithm>using namespace std;template<typename T>inline void read(T &x){ T f=1;char ch=getchar(); for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1; for(x=0;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0'; x*=f;}typedef long long LL;const int maxn=1000010,inf=0x7fffffff;struct Segment_Tree{ #define lc x<<1 #define rc x<<1|1 int L[maxn<<2],R[maxn<<2],t[maxn<<2]; LL mx[maxn<<2],se[maxn<<2],sum[maxn<<2],tag[maxn<<2]; void update(int x){ if(mx[lc]>mx[rc]){ mx[x]=mx[lc]; t[x]=t[lc]; se[x]=max(se[lc],mx[rc]); } else if(mx[lc]<mx[rc]){ mx[x]=mx[rc]; t[x]=t[rc]; se[x]=max(mx[lc],se[rc]); } else{ mx[x]=mx[lc]; t[x]=t[lc]+t[rc]; se[x]=max(se[lc],se[rc]); } sum[x]=sum[lc]+sum[rc]; } void Build(int x,int *a,int l,int r){ tag[x]=inf; if((L[x]=l)==(R[x]=r)){ mx[x]=sum[x]=a[l]; t[x]=1;se[x]=-inf; return; } int mid=(l+r)>>1; Build(lc,a,l,mid);Build(rc,a,mid+1,r); update(x); } void pushmin(int x,LL val){ sum[x]-=(mx[x]-val)*t[x]; mx[x]=tag[x]=val; } void pushdown(int x){ if(tag[x]==inf)return; if(mx[lc]>tag[x])pushmin(lc,tag[x]); if(mx[rc]>tag[x])pushmin(rc,tag[x]); tag[x]=inf; } void Modify(int x,int l,int r,LL val){ if(val>=mx[x])return; if(L[x]>=l&&R[x]<=r){ if(val<mx[x]&&val>se[x])return pushmin(x,val),void(); pushdown(x); Modify(lc,l,r,val);Modify(rc,l,r,val); return update(x),void(); } pushdown(x); int mid=(L[x]+R[x])>>1; if(l<=mid)Modify(lc,l,r,val); if(r>mid)Modify(rc,l,r,val); update(x); } LL Qsum(int x,int l,int r){ if(L[x]>=l&&R[x]<=r)return sum[x]; pushdown(x); int mid=(L[x]+R[x])>>1; LL ans=0; if(l<=mid)ans+=Qsum(lc,l,r); if(r>mid)ans+=Qsum(rc,l,r); return ans; } LL Qmax(int x,int l,int r){ if(L[x]>=l&&R[x]<=r)return mx[x]; pushdown(x); int mid=(L[x]+R[x])>>1; LL ans=0; if(l<=mid)ans=max(ans,Qmax(lc,l,r)); if(r>mid)ans=max(ans,Qmax(rc,l,r)); return ans; }}tree;int T,n,m,a[maxn];int main(){ read(T); while(T--){ read(n);read(m); for(int i=1;i<=n;i++)read(a[i]); tree.Build(1,a,1,n); while(m--){ int opt,l,r,c; read(opt);read(l);read(r); if(!opt)read(c),tree.Modify(1,l,r,c); else if(opt==1)printf("%lld\n",tree.Qmax(1,l,r)); else if(opt==2)printf("%lld\n",tree.Qsum(1,l,r)); } } return 0;}
- [题解]hdu5306 Gorgeous Sequence
- hdu5306 Gorgeous Sequence
- hdu5306 Gorgeous Sequence(线段树:复杂区间更新)
- Gorgeous Sequence
- Gorgeous Sequence
- hdu 5306 Gorgeous Sequence
- hdu 5306 Gorgeous Sequence
- HDU 5306 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
- [HDU] 5306 Gorgeous Sequence [区间取min&求和&求max][线段树]
- poj1019--Number Sequence题解
- tmux命令大全
- C# 调用 C++ 写的Dll 遇到的问题(调用DLL 自动退出) win32已停止工作
- 不同请求获取到了相同的ThreadLocal值引起的问题
- 父节点layout动态添加子节点,container模式动态更新父节点size
- CentOs7改造mysql服务
- [题解]hdu5306 Gorgeous Sequence
- 全方面角度剖析一句话木马
- QT CSV操作
- shell脚本截取列cut的使用
- 传数据不能为中文,而是乱码
- java概述
- 二维数组元素的地址
- Java基础——Request对象的主要方法
- 快速排序(Quick Sort)