50分,后面5个WA
查看原帖
50分,后面5个WA
508177
GXYZY楼主2023/11/15 22:34

rt,代码如下:

#include<bits/stdc++.h>
// #pragma GCC optimize(2)
// #define int long long
using namespace std;
const int inf=0x7fffffff;
int read(){
	int x=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0'){
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=x*10+c-'0';
		c=getchar();
	}
	return x*f;
}
void write(int x){
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10+'0');
	return ;
}
int n,q,a[1000006],op,x,y,z;
struct segtree{
	int lc,rc,mmax,addlazy,changelazy;
	bool ifchange;
}node[5000006];
void pushup(int st){
	node[st].mmax=max(node[st*2].mmax,node[st*2+1].mmax);
	return ;
}
void build(int st,int l,int r){
	node[st].lc=l;
	node[st].rc=r;
	node[st].ifchange=0;
	if(l==r){
		node[st].mmax=a[l];
		return ;
	}
	int md=(l+r)/2;
	build(st*2,l,md);
	build(st*2+1,md+1,r);
	pushup(st);
	return ;
}
void pushdown(int st){
	if(node[st].ifchange==1){
		node[st*2].changelazy=node[st].changelazy;
		node[st*2].addlazy=0;
		node[st*2].mmax=node[st].changelazy;
		node[st*2+1].changelazy=node[st].changelazy;
		node[st*2+1].addlazy=0;
		node[st*2+1].mmax=node[st].changelazy;
		node[st].changelazy=0;
		node[st].ifchange=0;
	}
	else if(node[st].addlazy!=0){
		if(node[st*2].ifchange==1){
			node[st*2].changelazy+=node[st].addlazy;
		}
		else{
			node[st*2].addlazy+=node[st].addlazy;
		}
		node[st*2].mmax+=node[st].addlazy;
		if(node[st*2+1].ifchange==1){
			node[st*2+1].changelazy+=node[st].addlazy;
		}
		else{
			node[st*2+1].addlazy+=node[st].addlazy;
		}
		node[st*2+1].mmax+=node[st].addlazy;
		node[st].addlazy=0;
	}
	return ;
}
void changeinterval(int st,int l,int r,int data){
	if(node[st].lc>r||node[st].rc<l) return ;
	if(node[st].lc>=l&&node[st].rc<=r){
		node[st].mmax=data;
		node[st].changelazy=data;
		node[st].addlazy=0;
		node[st].ifchange=1;
		return ;
	}
	pushdown(st);
	changeinterval(st*2,l,r,data);
	changeinterval(st*2+1,l,r,data);
	pushup(st);
	return ;
}
void addinterval(int st,int l,int r,int data){
	if(node[st].lc>r||node[st].rc<l) return ;
	if(node[st].lc>=l&&node[st].rc<=r){
		node[st].mmax+=data;
		if(node[st].ifchange==1){
			node[st].changelazy+=data;
		}
		else{
			node[st].addlazy+=data;
		}
		return ;
	}
	pushdown(st);
	addinterval(st*2,l,r,data);
	addinterval(st*2+1,l,r,data);
	pushup(st);
	return ;
}
int getmax(int st,int l,int r){
	if(node[st].lc>r||node[st].rc<l) return -inf;
	if(node[st].lc>=l&&node[st].rc<=r){
		return node[st].mmax;
	}
	pushdown(st);
	return max(getmax(st*2,l,r),getmax(st*2+1,l,r));
}
int main(){
	// freopen(".in","r",stdin);
	// freopen(".out","w",stdout);
	n=read();
	q=read();
	for(int i=1;i<=n;i++){
		a[i]=read();
	}
	build(1,1,n);
	for(int i=1;i<=q;i++){
		op=read();
		if(op==1){
			x=read();
			y=read();
			z=read();
			changeinterval(1,x,y,z);
		}
		if(op==2){
			x=read();
			y=read();
			z=read();
			addinterval(1,x,y,z);
		}
		if(op==3){
			x=read();
			y=read();
			write(getmax(1,x,y));
			putchar('\n');
		}
	}
	return 0;
}
/*
4 4
10 4 -3 -7
1 1 3 0
2 3 4 -4
1 2 4 -9
3 1 4
*/
2023/11/15 22:34
加载中...