萌新分块求调30pts
查看原帖
萌新分块求调30pts
975724
Sine_Func楼主2024/3/9 00:18

rt

#include <bits/stdc++.h>
#define get(x) (x/lenx)
#define int long long
using namespace std;
const int lenx = 1000;
int n, m, a[lenx], f[lenx], tag[lenx], l, r, d;
char c;
void update(int l, int r, int v) {
	if (get(r) - get(l) <= 1) {
		for (int i = l; i <= r; i++) {
			a[i] += v;
		}
	} else {
		int i = l;
		while (get(i) == get(l)) {
			a[i] += v;
			i++;
		}
		i = r;
		while (get(i) == get(r)) {
			a[i] += v;
			i--;
		}
		for (int i = get(l) + 1; i < get(r); i++) {
			tag[i] += v;
		}
	}
}
int ask(int l, int r) {
	int ans = 0;
	if (get(r) - get(l) <= 1)for (int i = l; i <= r; i++) ans += a[i];
	else {
		int i = l;
		while (get(i) == get(l)) {
			ans += a[i];
			ans += tag[get(i)];
			i++;
		}
		i = r;
		while (get(i) == get(r)) {
			ans += a[i];
			ans += tag[get(i)];
			i--;
		}
		for (int i = get(l) + 1; i < get(r); i++) {
			ans += f[i];
			ans += (lenx * tag[i]);
		}
	}
	return ans;
}
signed main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		f[get(i)] += a[i];
	}
	while (m--) {
		cin >> c >> l >> r;
		if (c == '1') {
			cin >> d;
			update(l, r, d);
		} else cout << ask(l, r) << "\n";
	}
	return 0;
}
2024/3/9 00:18
加载中...