Wangzehao2009 @ 2023-03-20 17:00:11
最后一个样例RE了!没看到和我一样错误的同学(苦笑)
#include <iostream>
using namespace std;
struct node{long long L,R,max,lazy,cover;};
long long const N=1e6+1,none=1e9+1;
node tree[N<<2];
long long a[N];
void Merge(long long p)
{
tree[p].max=max(tree[p<<1].max,tree[p<<1|1].max); return ;
}
void Cover(long long p)
{
if(tree[p].cover==none) return ;
tree[p<<1].lazy=tree[p<<1|1].lazy=0;
tree[p<<1].max=tree[p<<1|1].max=tree[p].cover;
tree[p<<1].cover=tree[p<<1|1].cover=tree[p].cover;
tree[p].cover=none;
return ;
}
void Sum(long long p)
{
if(tree[p].lazy==0) return ;
Cover(p);
tree[p<<1].lazy+=tree[p].lazy;
tree[p<<1|1].lazy+=tree[p].lazy;
tree[p<<1].max+=tree[p].lazy;
tree[p<<1|1].max+=tree[p].lazy;
tree[p].lazy=0;
return ;
}
void Push_down(long long p)
{
Cover(p);
Sum(p);
return ;
}
void Build(long long p,long long l,long long r)
{
tree[p].L=l,tree[p].R=r;
if(tree[p].L==tree[p].R)
{
tree[p].max=a[l];
tree[p].cover=none;
return ;
}
long long mid=(l+r)>>1;
Build(p<<1,l,mid);
Build(p<<1|1,mid+1,r);
Merge(p);
return ;
}
void Update(long long p,long long l,long long r,long long k,bool q)
{
if(l<=tree[p].L && tree[p].R<=r)
{
if(q==0)
{
Cover(p);
tree[p].max+=k;
tree[p].lazy+=k;
}
else
{
tree[p].lazy=0;
tree[p].max=k;
tree[p].cover=k;
}
return ;
}
Push_down(p);
long long mid=(tree[p].L+tree[p].R)>>1;
if(r<=mid) Update(p<<1,l,r,k,q);
else if(l>mid) Update(p<<1|1,l,r,k,q);
else
{
Update(p<<1,l,r,k,q);
Update(p<<1|1,l,r,k,q);
}
Merge(p);
return ;
}
long long Query(long long p,long long l,long long r)
{
if(l<=tree[p].L && tree[p].R<=r) return tree[p].max;
Push_down(p);
long long mid=(tree[p].L+tree[p].R)>>1;
if(r<=mid) return Query(p<<1,l,r);
else if(l>mid) return Query(p<<1|1,l,r);
else return max(Query(p<<1,l,r),Query(p<<1|1,l,r));
}
int main()
{
ios::sync_with_stdio(false);
long long n,m,q,l,r,x;
cin>>n>>m;
for(long long i=1;i<=n;i++) cin>>a[i];
for(long long i=1;i<=n<<2;i++) tree[i].cover=none;
Build(1,1,n);
for(long long i=1;i<=m;i++)
{
cin>>q>>l>>r;
if(q==1) cin>>x,Update(1,l,r,x,1);
if(q==2) cin>>x,Update(1,l,r,x,0);
if(q==3) cout<<Query(1,l,r)<<endl;
}
return 0;
}
by Larry76 @ 2023-03-20 17:35:56
将 2e6
就行了
by Larry76 @ 2023-03-20 17:54:00
或者在 Update
中特判一下当前
by Wangzehao2009 @ 2023-03-21 08:43:27
谢谢巨佬!