求助,样例没过
查看原帖
求助,样例没过
1126325
Indestructible楼主2024/6/21 21:07

太久没写线段树的 lazy_tag 了,直接样例 RE

#include<bits/stdc++.h>
#define int long long
using namespace std;
int ls(int p){ return p<<1; }
int rs(int p){ return p<<1|1; }
const int maxn=1000006,minx=-(1ll<<60);
int n,m,num[maxn],op,l,r,x;
struct SegmentTree{ int x,tagadd,tagx; }tree[maxn<<2];
void build(int pl,int pr,int p){
	if (pl==pr){ tree[p].x=num[pl]; return; }
	int mid=(pl+pr)>>1;
	build(pl,mid,ls(p)),build(mid+1,pr,rs(p));
	tree[p].x=max(tree[ls(p)].x,tree[rs(p)].x);
}
void addtag(int l,int r,int p,int x,int isadd){
	if (isadd) tree[p].x+=(r-l+1)*x,tree[p].tagx?tree[p].tagx+=x:tree[p].tagadd+=x;
	else tree[p].x=x,tree[p].tagadd=0,tree[p].tagx=x;
}
void pushdown(int l,int r,int p){
	int mid=(l+r)>>1;
	if (tree[p].tagx){
		tree[ls(p)].tagx+=tree[p].tagx,tree[rs(p)].tagx+=tree[p].tagx;
		tree[ls(p)].x=tree[rs(p)].x=tree[p].x;
	}else{
		tree[ls(p)].tagadd+=tree[p].tagadd,tree[rs(p)].tagadd+=tree[p].tagadd;
		tree[ls(p)].x+=(mid-l+1)*tree[p].tagadd,tree[rs(p)].x+=(r-mid)*tree[p].tagadd;
	}
	tree[p].tagadd=tree[p].tagx=0;
}
void change(int pl,int pr,int p,int l,int r,int x,bool isadd){
	if (l<=pl && pr<=r) addtag(pl,pr,p,x,isadd);
	pushdown(pl,pr,p); int mid=(pl+pr)>>1;
	if (l<=mid) change(pl,mid,ls(p),l,r,x,isadd);
	if (r>mid) change(mid+1,pr,rs(p),l,r,x,isadd);
}
int query(int pl,int pr,int p,int l,int r){
	if (l<=pl && pr<=r) return tree[p].x;
	pushdown(pl,pr,p); int mid=(pl+pr)>>1,s=minx;
	if (l<=mid) s=max(s,query(pl,mid,ls(p),l,r));
	if (r>mid) s=max(s,query(mid+1,pr,rs(p),l,r));
	return s;
}
signed main(){
	scanf("%lld%lld",&n,&m);
	for (int i=1;i<=n;i++) scanf("%lld",&num[i]);
	build(1,n,1);
	for (int i=1;i<=m;i++){
		scanf("%lld%lld%lld",&op,&l,&r);
		if (op==1) scanf("%lld",&x),change(1,n,1,l,r,x,0);
		if (op==2) scanf("%lld",&x),change(1,n,1,l,r,x,1);
		if (op==3) printf("%lld\n",query(1,n,1,l,r));
	}
	return 0;
}
2024/6/21 21:07
加载中...