HELP! ! !
查看原帖
HELP! ! !
275748
Comintern楼主2021/7/28 17:43

神奇

怎么改都是输出15/110/110(就是主页上的数据)

#include<bits/stdc++.h>
#define mid (l+r)/2
using namespace std;
struct hop{
	int r,l;long long num;
};hop tree[400001];
int tag[400000];
int n,m;
void pushup(int root){
	tree[root].num=tree[root*2].num+tree[root*2+1].num; 
}
void build(int l,int r,int root){
	tag[root]=0;
	if(l==r){
	    cin>>tree[root].num;
	    tree[root].l=l;
	    tree[root].r=r;return;}
	build(l,mid,root*2);
	build(mid+1,r,root*2+1);
	pushup(root);
}
void pushdown(int m,int root){
	tree[root*2].num+=(m-m/2)*tag[root];
	tree[root*2+1].num+=(m/2)*tag[root];
	tag[root*2]+=tag[root];
	tag[root*2+1]+=tag[root];
}
void update(int a,int b,int root,long long p,int l,int r){
	if(a>=l&&b<=r){
		tree[root].num+=(r-l+1)*p;
		tag[root]+=p;return ;
	}
	int m=r-l+1;
	pushdown(m,root);
	if(a<=(r+l)/2){
		update(a,b,root*2,p,l,(r+l)/2);//[Error] reference to 'update' is ambiguous
	}
	if(b>(r+l)/2){
		update(a,b,root*2+1,p,(r+l)/2+1,r);
	}
	pushup(root);
}
long long query(int l,int r,int root,int a,int b){
	if(a>=l&&b<=r){
		return tree[root].num;
	}
	m=r-l+1;
	pushdown(m,root);
	long long ans=0;
	if(a<=(l+r)/2)ans+=query(l,mid,root*2,a,b);if(b>(l+r)/2)ans+=query(mid+1,r,root*2+1,a,b);
	return ans;
}
int main(){
	int n,m;
	cin>>m>>n;
	build(1,m,1);
	for(int i=1;i<=n;i++){
		int k;
		cin>>k;
		if(k==1){
			int a,b;long long c;
			update(a,b,1,c,1,m);
		}
		else {
			int a,b;
			cin>>a>>b;
			cout<<query(1,m,1,a,b)<<endl;
		}
	}
	return 0;
}

求巨佬指教

2021/7/28 17:43
加载中...