线段树20pts4种颜色求调
查看原帖
线段树20pts4种颜色求调
1036776
zjy4楼主2025/7/25 11:15

1,3AC

2,4WA

5-9TLE

10RE

#include<bits/stdc++.h>
using namespace std;
#define int long long
struct node{
	int v,l,r;
}a[1000001];
int n,b[100001],maxn=0,m;
void build(int l,int r,int pos){
	a[pos].l=l,a[pos].r=r;
	if(l==r){
		a[pos].v=b[l];
		return;
	}
	int mid=(l+r)/2;
	build(l,mid,pos*2);
	build(mid+1,r,pos*2+1);
	a[pos].v=max(a[pos*2].v,a[pos*2+1].v);
}
void cx(int l,int r,int pos){
	if(a[pos].l==l&&a[pos].r==r) {
		maxn=max(a[pos].v,maxn);
		return;
	}
	if(l==r){
		maxn=max(b[l],maxn);
		return;
	}
	int mid=(a[pos].l+a[pos].r)/2;
	if(l>mid) cx(l,r,pos*2+1);
	else if(r<=mid) cx(l,r,pos*2);
	else{
		cx(l,mid,pos*2);
		cx(mid+1,r,pos*2+1);
	} 
}
void up(int l,int r,int x,int pos){
	if(l==r&&a[pos].l==a[pos].r){
		a[pos].v=x;
		b[l]=x;
		while(1){
			pos/=2;
			if(pos==0) break;
			a[pos].v=max(a[pos*2].v,a[pos*2+1].v);
		}
		return;
	}
	int mid=(a[pos].l+a[pos].r)/2;
	if(l>mid) up(l,r,x,pos*2+1);
	else if(r<=mid) up(l,r,x,pos*2);
	else{
		up(l,mid,x,pos*2);
		up(mid+1,r,x,pos*2+1);
	} 
}
void up2(int l,int r,int x,int pos){
	if(l==r&&a[pos].l==a[pos].r){
		a[pos].v+=x;
		b[l]+=x;
		while(1){
			pos/=2;
			if(pos==0) break;
			a[pos].v=max(a[pos*2].v,a[pos*2+1].v);
		}
		return;
	}
	int mid=(a[pos].l+a[pos].r)/2;
	if(l>mid) up(l,r,x,pos*2+1);
	else if(r<=mid) up(l,r,x,pos*2);
	else{
		up(l,mid,x,pos*2);
		up(mid+1,r,x,pos*2+1);
	} 
}
signed main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>b[i];
	build(1,n,1);
	for(int i=1;i<=m;i++){
		int op,l,r,x;
		maxn=-1e9;
		cin>>op;
		if(op==1){
			cin>>l>>r>>x;
			up(l,r,x,1);
		}
		if(op==2){
			cin>>l>>r>>x;
			up2(l,r,x,1);
		}
		if(op==3){
			cin>>l>>r;
			cx(l,r,1);
			cout<<maxn<<endl;
		}
	}
} 
2025/7/25 11:15
加载中...