现在回来看的时候都不知道自己写了个啥……不RE才怪(求助)
查看原帖
现在回来看的时候都不知道自己写了个啥……不RE才怪(求助)
922691
Hisy楼主2023/10/14 22:02
#include<cstdio>
#include<vector>
#define null NULL
#define int long long
using namespace std; 
struct node{
	int sum=0,first=0,last=0;
	node *prev,*next;
};
void setrange(int first,int last,node *tree){
	tree->first=first;
	tree->last=last;
};
void maketree(vector<int> data,node *tree,int first,int last){
	setrange(first,last,tree);
	if(first==last){
		tree->sum=data[first];
		tree->prev=null;
		tree->next=null;
		return;
	};
	maketree(data,tree->prev,first,first+last<<1);
	maketree(data,tree->next,first+last<<1|1,last);
	tree->sum=tree->prev->sum+tree->next->sum;
};
void addrange(int add,int first,int last,node *tree){
	if(first==last){
		tree->sum+=add;
		return;
	};
	int lower=tree->first,upper=tree->last;
	tree->sum+=add*(last-first+1);
	addrange(add,first,lower+upper<<1,tree->prev);
	addrange(add,lower+upper<<1,last,tree->next);
};
int queried(int first,int last,node *tree){
	if(first==last){
		return tree->sum;
	};
	int lower=tree->first,upper=tree->last;
	return queried(first,lower+upper<<1,tree->prev)+queried(lower+upper<<1|1,last,tree->next);
};
signed main(signed argc,char **argv){
	node *tree;
	int maxn,maxm;
	scanf("%lld %lld",&maxn,&maxm);
	vector<int> data(1,0);
	for(int iter=1;iter<=maxn;iter++){
		int temp;
		scanf("%lld",&temp);
		data.push_back(temp);
	};
	maketree(data,tree,1,maxn);
	while(maxm--){
		int symbol,first,last;
		scanf("%lld %lld %lld",&symbol,&first,&last);
		if(symbol==1){
			int add;
			scanf("%lld",&add);
			addrange(add,first,last,tree);
		}else{
			printf("%lld\n",queried(first,last,tree));
		};
	};
	return 0;
};
2023/10/14 22:02
加载中...