求助,4和8过不去,80分
查看原帖
求助,4和8过不去,80分
1112954
lsc72楼主2024/5/23 12:27
#include<bits/stdc++.h>
using namespace std;
struct node{long long l,r,n,add=0;
};
node tree[500010];
long long a[100010];
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=build(p*2,l,mid)+build(p*2+1,mid+1,r);
	tree[p]=temp;
	return tree[p].n;
}
long long update(int s,int p){
	if (s==p) return tree[s].n;
	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);
	return tree[s].n=tree[s*2].n+tree[s*2+1].n;
}
void change(int s){
	if (tree[s].add==0) return;
	if (tree[s*2].l!=0){
		tree[s*2].n+=tree[s].add*(tree[s*2].r-tree[s*2].l+1);
		tree[s*2].add+=tree[s].add;
	}
	if (tree[s*2+1].l!=0){
		tree[s*2+1].n+=tree[s].add*(tree[s*2+1].r-tree[s*2+1].l+1);
		tree[s*2+1].add+=tree[s].add;
	}
	tree[s].add=0;return;
}
void search(long long m,int l,int r,int s){
	if (tree[s].l==l&&r==tree[s].r){
		if(m<tree[s].n){
			tree[s].n+=m*(r-l+1);
			tree[s].add+=m;
			update(1,s);
		} 
		return;
	}
	change(s);
	int mid=(tree[s].l+tree[s].r)/2;
	if (l<=mid&&m<tree[2*s].n) search(m,l,min(mid,r),2*s);
	if (r>mid&&m<tree[2*s+1].n) search(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=0;
	int mid=(tree[s].l+tree[s].r)/2;
	if (l<=mid) ans+=find(l,min(mid,r),2*s);
	if (r>mid) ans+=find(max(mid+1,l),r,2*s+1);
	return ans;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); 
	cout.tie(NULL);
	int n,m;
	cin>>n>>m;
	for (int i=1;i<=n;i++) cin>>a[i];
	build(1,1,n);
	while (m--)
	{
		int a,x,y;long long k;
		cin>>a;
		if (a==2)
		{
			cin>>x>>y;
			cout<<find(x,y,1)<<'\n';
		}
		else
		{
			cin>>x>>y>>k;
			search(k,x,y,1);
		}
	}
	return 0; 
}
2024/5/23 12:27
加载中...