Gorgeous Sequence
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2150 Accepted Submission(s): 594
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.
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.
5 5
1 2 3 4 5
1 1 5
2 1 5
0 3 5 3
1 1 5
2 1 5
15
3
12
Please use efficient IO method
![HDU 5306 Gorgeous Sequence[线段树区间最值操作] HDU 5306 Gorgeous Sequence[线段树区间最值操作]](https://image.miaokee.com:8440/aHR0cHM6Ly9pbWFnZXMyMDE1LmNuYmxvZ3MuY29tL2Jsb2cvODkyNzU4LzIwMTcwMy84OTI3NTgtMjAxNzAzMjkyMTQ3NDcxNzAtMTc3Mjk4Ni5wbmc%3D.png?w=700&webp=1)
一份代码交了13遍。从TLE->WA->TLE->……QAQ
#include<cstdio>
#include<iostream>
#define lc k<<1
#define rc k<<1|1
#define EF if(ch==EOF) return x;
using namespace std;
typedef long long ll;
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;EF;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int N=1e6+;
const int M=N<<;
int n,m,a[N];
ll sum[M];int mx[M],se[M],mc[M];
inline void updata(int k){
sum[k]=sum[lc]+sum[rc];
mx[k]=max(mx[lc],mx[rc]);
se[k]=max(se[lc],se[rc]);mc[k]=;
if(mx[lc]!=mx[rc]) se[k]=max(se[k],min(mx[lc],mx[rc]));
if(mx[k]==mx[lc]) mc[k]+=mc[lc];
if(mx[k]==mx[rc]) mc[k]+=mc[rc];
}
inline void dec_tag(int k,int v){
if(v>=mx[k]) return ;
sum[k]+=1LL*(v-mx[k])*mc[k];mx[k]=v;
}
inline void pushdown(int k){
dec_tag(lc,mx[k]);
dec_tag(rc,mx[k]);
}
void build(int k,int l,int r){
if(l==r){
sum[k]=mx[k]=a[l];mc[k]=;se[k]=-;
return ;
}
int mid=l+r>>;
build(lc,l,mid);
build(rc,mid+,r);
updata(k);
}
void change(int k,int l,int r,int x,int y,int v){
if(v>=mx[k]) return ;
if(l==x&&r==y&&v>se[k]){
dec_tag(k,v);return ;
}
pushdown(k);
int mid=l+r>>;
if(y<=mid) change(lc,l,mid,x,y,v);
else if(x>mid) change(rc,mid+,r,x,y,v);
else change(lc,l,mid,x,mid,v),change(rc,mid+,r,mid+,y,v);
updata(k);
}
int query_max(int k,int l,int r,int x,int y){
if(l==x&&r==y) return mx[k];
pushdown(k);
int mid=l+r>>;
if(y<=mid) return query_max(lc,l,mid,x,y);
else if(x>mid) return query_max(rc,mid+,r,x,y);
else return max(query_max(lc,l,mid,x,mid),query_max(rc,mid+,r,mid+,y));
}
ll query_sum(int k,int l,int r,int x,int y){
if(l==x&&r==y) return sum[k];
pushdown(k);
int mid=l+r>>;
if(y<=mid) return query_sum(lc,l,mid,x,y);
else if(x>mid) return query_sum(rc,mid+,r,x,y);
else return query_sum(lc,l,mid,x,mid)+query_sum(rc,mid+,r,mid+,y);
}
inline void work(){
n=read();m=read();
for(int i=;i<=n;i++) a[i]=read();
build(,,n);
for(int i=,opt,x,y,z;i<=m;i++){
opt=read();x=read();y=read();
if(opt==) z=read(),change(,,n,x,y,z);
if(opt==) printf("%d\n",query_max(,,n,x,y));
if(opt==) printf("%lld\n",query_sum(,,n,x,y));
}
}
int main(){
for(int T=read();T--;) work();
return ;
}