0分求调
查看原帖
0分求调
1104824
lyt20101001楼主2024/11/24 16:42
#include<bits/stdc++.h>
#define lc x<<1
#define rc x<<1|1
#define ll long long
using namespace std;
struct node {
	ll val, l, r, lazy;
} tr[100005 << 2];
ll n, m, k, a[100005], op, x, y;
void pushUp(int x) {
	tr[x].val = tr[lc].val + tr[rc].val;
}
void pushDown(int x) {
	if (tr[x].lazy == 0) return;
	tr[lc].val += tr[x].lazy*(tr[x].r - tr[x].l + 1);
	tr[rc].val += tr[x].lazy*(tr[x].r - tr[x].l + 1);
	tr[lc].lazy += tr[x].lazy;
	tr[rc].lazy += tr[x].lazy;
	tr[x].lazy = 0;
}
void build(int x, int l, int r) {
	tr[x].l = l, tr[x].r = r;
	if (l == r) {
		tr[x].val = a[l];
		return;
	}
	int m = (l + r) >> 1;
	build(lc, l, m);
	build(rc, m + 1, r);
	pushUp(x);
}
ll quary(int x, int l, int r) {
	if (tr[x].l >= l && tr[x].r <= r)
		return tr[x].val;
	pushDown(x);
	ll sum = 0;
	int m = (tr[x].l + tr[x].r) >> 1;
	if (m >= l) sum += quary(lc, l, r);
	if (m < r) sum += quary(rc, l, r);
	return sum;
}
void update(int x, int l, int r, int k) {
	if (l <= tr[x].l && tr[x].r <= r) {
		tr[x].val += k*(tr[x].r - tr[x].l + 1);
		tr[x].lazy += k;
		return;
	}
	pushDown(x);
	int m = (tr[x].l + tr[x].r) >> 1;
	if (l <= m) update(lc, l, r, k);
	if (m < r) update(rc, l, r, k);
	pushUp(x);
}
int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	build(1, 1, n);
	for (int i = 1; i <= m; i++) {
		cin >> op;
		if (op == 1) {
			cin >> x >> y >> k;
			update(1, x, y, k);
		} else {
			cin >> x >> y;
			cout << quary(1, x, y) << endl;
		}
	}
	return 0;
}
2024/11/24 16:42
加载中...