求改
  • 板块学术版
  • 楼主Staggered
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/11/19 21:52
  • 上次更新2024/11/20 10:20:22
查看原帖
求改
816200
Staggered楼主2024/11/19 21:52

P1253扶苏的问题
一直60分

# include<bits/stdc++.h>
# define int long long
using namespace std;
int n,m;
int read(){
	int ans=0,res=1;
	char c=getchar();
	while(c>'9'||c<'0'){if(c=='-') res=-1;c=getchar();};
	while(c>='0'&&c<='9') ans=ans*10+c-'0',c=getchar();
	return ans*res;
}
const int N=1e6+7;
int a[N],lson[N*4],rson[N*4],tot,tree[N*4],add[N*4],add2[N*4];
void build(int &rt,int l,int r){
	if(!rt) rt=++tot;
	if(l==r) {
		tree[rt]=a[l];
		return ;
	}
	int mid=(l+r)/2;
	build(lson[rt],l,mid);
	build(rson[rt],mid+1,r);
	tree[rt]=max(tree[lson[rt]],tree[rson[rt]]);
}
void pushdown(int rt){
	if(add[rt]==1145141919810) return ;
	tree[lson[rt]]=tree[rson[rt]]=add[rt];
	add[lson[rt]]=add[rson[rt]]=add[rt];
	add[rt]=1145141919810;
}
void pushdown2(int rt){
	tree[lson[rt]]+=add2[rt];
	tree[rson[rt]]+=add2[rt];
	add2[lson[rt]]+=add2[rt];
	add2[rson[rt]]+=add2[rt];
	add2[rt]=0;
}
void modify1(int rt,int l,int r,int x,int y,int v){
	if(x<=l&&r<=y){
		tree[rt]=v;
		add[rt]=v;
		return ;
	}
	int mid=(l+r)/2;
	pushdown(rt);
	pushdown2(rt);
	if(x<=mid) modify1(lson[rt],l,mid,x,y,v);
	if(mid<y) modify1(rson[rt],mid+1,r,x,y,v);
	tree[rt]=max(tree[lson[rt]],tree[rson[rt]]);
}
void modify2(int rt,int l,int r,int x,int y,int v){
	if(x<=l&&r<=y){
		tree[rt]+=v;
		add2[rt]+=v;
		return ;
	}
	int mid=(l+r)/2;
	pushdown(rt);
	pushdown2(rt);
	if(x<=mid) modify2(lson[rt],l,mid,x,y,v);
	if(mid<y) modify2(rson[rt],mid+1,r,x,y,v);
	tree[rt]=max(tree[lson[rt]],tree[rson[rt]]);
}
int query(int rt,int l,int r,int x,int y){
	if(x<=l&&r<=y) return tree[rt];
	int mid=(l+r)/2,res=LLONG_MIN;
	pushdown(rt);
	pushdown2(rt);
	if(x<=mid) res=max(res,query(lson[rt],l,mid,x,y));
	if(mid<y) res=max(res,query(rson[rt],mid+1,r,x,y));
	tree[rt]=max(tree[lson[rt]],tree[rson[rt]]);
	return res;
}
void write(int x){
    if(x<0)
        putchar('-'),x=-x;
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
    return;
}
signed main(){
	// freopen("P1253_7.in","r",stdin);
	// freopen("out.out","w",stdout);
	for(int i=0;i<N*4;i++) add[i]=1145141919810;
	int rt=0;
	n=read(),m=read();
	for(int i=1;i<=n;i++) a[i]=read();
	build(rt,1,n);
	while(m--){
		int op=read(),l,r,x;
		if(op==1){
			l=read(),r=read(),x=read();
			modify1(1,1,n,l,r,x);
		}else if(op==2){
			l=read(),r=read(),x=read();
			modify2(1,1,n,l,r,x);
		}else{
			l=read(),r=read();
			int ans=query(1,1,n,l,r);
			write(ans);
			printf("\n");
		}
	}
	return 0;
}
2024/11/19 21:52
加载中...