90分 10 TLE 非输入输出的问题(试过了)求助!
查看原帖
90分 10 TLE 非输入输出的问题(试过了)求助!
1112954
lsc72楼主2024/5/30 17:06
#include<bits/stdc++.h>
using namespace std;
inline long long maxx(long long a,long long b){
	if (a>=b) return a;
	else return b;
}
struct node{long long l,r,n,add=0,ccc=-1e17;};
node tree[5000010];
long long a[1000010];
long long build(int p,int l,int r){
	node temp;temp.l=l;temp.r=r;
	if (l==r) {temp.n=a[l];tree[p]=temp;return tree[p].n;}
	int mid=(l+r)/2;
	temp.n=maxx(build(p*2,l,mid),build(p*2+1,mid+1,r));
	tree[p]=temp;
	return tree[p].n;
}
void update(int s,int p){
	if (s==p) return;
	int mid=(tree[s].l+tree[s].r)/2;
	if (tree[p].l<=mid) update(s*2,p);
	if (tree[p].l>mid) update(s*2+1,p);
	tree[s].n=maxx(tree[s*2].n,tree[s*2+1].n);
}
void change(int s){
	if (tree[s*2].l!=0){
		if (tree[s].ccc!=-1e17) {
			tree[s*2].n=tree[s].ccc;
			tree[s*2].add=0;
			tree[s*2].ccc=tree[s].ccc;
		}
		tree[s*2].n+=tree[s].add;
		tree[s*2].add+=tree[s].add;
	}
	if (tree[s*2+1].l!=0){
		if (tree[s].ccc!=-1e17) {
			tree[s*2+1].n=tree[s].ccc;
			tree[s*2+1].add=0;
			tree[s*2+1].ccc=tree[s].ccc;
		}
		tree[s*2+1].n+=tree[s].add;
		tree[s*2+1].add+=tree[s].add;
	}
	tree[s].add=0; tree[s].ccc=-1e17;
	return;
}
void search(long long m,int l,int r,int s){
	if (tree[s].l==l&&r==tree[s].r){
		tree[s].n+=m;
		tree[s].add+=m;
		update(1,s);
		return;
	}
	change(s);
	int mid=(tree[s].l+tree[s].r)/2;
	if (l<=mid) search(m,l,min(mid,r),2*s);
	if (r>mid) search(m,max(mid+1,l),r,2*s+1);
	return;
}
void search2(long long m,int l,int r,int s){
	if (tree[s].l==l&&r==tree[s].r){
		tree[s].n=m;
		tree[s].add=0;
		tree[s].ccc=m;
		update(1,s);
		return;
	}
	change(s);
	int mid=(tree[s].l+tree[s].r)/2;
	if (l<=mid) search2(m,l,min(mid,r),2*s);
	if (r>mid) search2(m,max(mid+1,l),r,2*s+1);
	return;
}
long long find(int l,int r,int s){
	if (tree[s].l==l&&r==tree[s].r){
		return tree[s].n;
	}
	change(s);
	long long ans=-1e17;
	int mid=(tree[s].l+tree[s].r)/2;
	if (l<=mid) ans=maxx(ans,find(l,min(mid,r),2*s));
	if (r>mid) ans=maxx(ans,find(max(mid+1,l),r,2*s+1));
	return ans;
}
int main()
{
	int n,m;
	cin>>n>>m;
	for (int i=1;i<=n;i++) scanf("%ld",&a[i]);
	build(1,1,n);
	while (m--)
	{
		int a,x,y;
		long long k;
		cin>>a;
		if (a==1)
		{
			cin>>x>>y>>k;
			search2(k,x,y,1);
		}
		else if (a==2)
		{
			cin>>x>>y>>k;
			search(k,x,y,1);
		}
		else
		{
			cin>>x>>y;
			printf("%ld\n",find(x,y,1));
		}
	}
	return 0; 
}
2024/5/30 17:06
加载中...