线段树求条
查看原帖
线段树求条
1110209
Eater_Tree楼主2024/12/6 18:29
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int INF=1e11;
int n,m;
int a[1001000];
struct node{
	int l,r,dat,add,rev;
}tree[4001000];
int op,l,r,k;
void build(int p,int l,int r){
	tree[p].l=l,tree[p].r=r,tree[p].rev=INF;
	if(l==r){
		tree[p].dat=a[l];
		return ;
	}
	int mid=(tree[p].l+tree[p].r)/2;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
	tree[p].dat=max(tree[p*2].dat,tree[p*2+1].dat);
	return ;
}
void down(int p){
	if(tree[p].rev!=INF){
		int d=tree[p].rev;
		tree[p*2].dat=d;
		tree[p*2+1].dat=d;
		tree[p*2].rev=d;
		tree[p*2+1].rev=d;
		tree[p*2].add=0;
		tree[p*2+1].add=0;
		tree[p].rev=INF;
	}
	if(tree[p].add!=0){
		int d=tree[p].add;
		tree[p*2].dat+=d;
		tree[p*2+1].dat+=d;
		tree[p*2].add+=d;
		tree[p*2+1].add+=d;
		tree[p].add=0;
	}
	return ;
}
void revupdate(int p,int l,int r,int k){
	if(tree[p].l>=l&&tree[p].r<=r){
		tree[p].dat=k;
		tree[p].rev=k;
		tree[p].add=0;
		return ;
	}
	down(p);
	int mid=(tree[p].l+tree[p].r)/2;
	if(l<=mid)
		revupdate(p*2,l,r,k);
	if(r>mid)
		revupdate(p*2+1,l,r,k);
	tree[p].dat=max(tree[p*2].dat,tree[p*2+1].dat);
	return ;
}
void addupdate(int p,int l,int r,int k){
	if(tree[p].l>=l&&tree[p].r<=r){
		tree[p].dat+=k;
		tree[p].add+=k;
		return ;
	}
	down(p);
	int mid=(tree[p].l+tree[p].r)/2;
	if(l<=mid)
		addupdate(p*2,l,r,k);
	if(r>mid)
		addupdate(p*2+1,l,r,k);
	tree[p].dat=max(tree[p*2].dat,tree[p*2+1].dat);
	return ;
}
int ask(int p,int l,int r){
	if(tree[p].l>=l&&tree[p].r<=r)
		return tree[p].dat;
	down(p);
	int ans=0;
	int mid=(tree[p].l+tree[p].r)/2;
	if(l<=mid)
		ans+=ask(p*2,l,r);
	if(r>mid)
		ans+=ask(p*2+1,l,r);
	return ans;
}
signed main(){
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i]);
	build(1,1,n);
	while(m--){
		scanf("%lld",&op);
		if(op==1){
			scanf("%lld%lld%lld",&l,&r,&k);
			revupdate(1,l,r,k);
		}
		if(op==2){
			scanf("%lld%lld%lld",&l,&r,&k);
			addupdate(1,l,r,k);
		}
		if(op==3){
			scanf("%lld%lld",&l,&r);
			printf("%lld\n",ask(1,l,r));
		}
	}
	return 0;
}
2024/12/6 18:29
加载中...