70分求调
查看原帖
70分求调
932978
Twinkling楼主2023/12/14 20:59
#include<bits/stdc++.h>
using namespace std;
#define ing long long 
#define lson (root << 1)//(root * 2)
#define rson (root << 1 | 1)//(root * 2 + 1)
#define N 100010
struct node{
	int lft,rgt,sum,lazy;
}tr[N << 2];//N * 2
int n,m,opt,a[N];
void push_up(int root){
	tr[root].sum = tr[lson].sum + tr[rson].sum;
}
void push_down(int root){
	if(!tr[root].lazy)
		return;
	tr[lson].sum += (tr[lson].rgt - tr[lson].lft + 1) * tr[root].lazy;
	tr[rson].sum += (tr[rson].rgt - tr[rson].lft + 1) * tr[root].lazy;
	tr[lson].lazy += tr[root].lazy;
	tr[rson].lazy += tr[root].lazy;
	tr[root].lazy = 0;
}
void build_tree(int root,int lft,int rgt){
	tr[root].lft = lft;
	tr[root].rgt = rgt;
	if(tr[root].lft == tr[root].rgt){
		tr[root].sum = a[lft];
		return;
	}
	int mid = lft + rgt >> 1;//(lft + rgt) / 2 ????? 
	build_tree(lson,lft,mid);
	build_tree(rson,mid+1,rgt);
	push_up(root);
}
int query(int root,int lft,int rgt){
	if(tr[root].lft >= lft && tr[root].rgt <= rgt)
		return tr[root].sum;
	push_down(root);
	int mid = tr[root].lft + tr[root].rgt >> 1;
	int res = 0;
	if(lft <= mid)
		res += query(lson,lft,rgt);
	if(rgt > mid)
		res += query(rson,lft,rgt);
	return res;
}
void update(int root,int lft,int rgt,int k){
	if(tr[root].lft >= lft && tr[root].rgt <= rgt){
		tr[root].sum += (tr[root].rgt - tr[root].lft + 1) * k;
		tr[root].lazy += k;
		return;
	}
	push_down(root);
	int mid = tr[root].lft + tr[root].rgt >> 1;
	if(lft <= mid)
		update(lson,lft,rgt,k);
	if(rgt > mid)
		update(rson,lft,rgt,k);
	push_up(root);
	return;
}
signed main(){
	cin >> n >> m;
	for(int i = 1; i <= n; i++)
		cin >> a[i];
	build_tree(1,1,n);
	while(m--){
		int l,r,k;
		cin >> opt;
		if(opt == 1){
			cin >> l >> r >> k;
			update(1,l,r,k);
		}
		else{
			cin >> l >> r;
			cout << query(1,l,r) << endl;
		}
	}
	return 0;
}
2023/12/14 20:59
加载中...