我太菜了,只能用线段树把这个题目给水过了
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define int long long
using namespace std;
const int N = 1e5 + 10;
char op;
int n, m;
int x, y, k;
int a[N];
struct Tree {
int l, r;
int sum, lazytag;
}tree[N * 4];
void pushdown(int cnt) {
if (tree[cnt].lazytag) {
tree[cnt << 1].lazytag += tree[cnt].lazytag;
tree[cnt << 1 | 1].lazytag += tree[cnt].lazytag;
tree[cnt << 1].sum += tree[cnt].lazytag * (tree[cnt << 1].r - tree[cnt << 1].l + 1);
tree[cnt << 1 | 1].sum += tree[cnt].lazytag * (tree[cnt << 1 | 1].r - tree[cnt << 1 | 1].l + 1);
tree[cnt].lazytag = 0;
}
}
void build(int l, int r, int cnt) {
tree[cnt].l = l, tree[cnt].r = r, tree[cnt].lazytag = 0;
if (l == r) {
tree[cnt].sum = a[l];
return;
}
int mid = (l + r) >> 1;
build(l, mid, cnt << 1), build(mid + 1, r, cnt << 1 | 1);
tree[cnt].sum = tree[cnt << 1].sum + tree[cnt << 1 | 1].sum;
return;
}
void modify(int l, int r, int k, int cnt) {
if (tree[cnt].l >= l && tree[cnt].r <= r) {
tree[cnt].sum += k * (tree[cnt].r - tree[cnt].l + 1);
tree[cnt].lazytag += k;
return;
}
pushdown(cnt);
int mid = (tree[cnt].l + tree[cnt].r) >> 1;
if (l <= mid) modify(l, r, k, cnt << 1);
if (r > mid) modify(l, r, k, cnt << 1 | 1);
tree[cnt].sum = tree[cnt << 1].sum + tree[cnt << 1 | 1].sum;
return;
}
int query(int l, int r, int cnt) {
if (tree[cnt].l >= l && tree[cnt].r <= r) return tree[cnt].sum;
pushdown(cnt);
int mid = (tree[cnt].l + tree[cnt].r) >> 1;
int ans = 0;
if (l <= mid) ans += query(l, r, cnt << 1);
if (r > mid) ans += query(l, r, cnt << 1 | 1);
return ans;
}
signed main(void) {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
build(1, n, 1);
while (m -- ) {
cin >> op;
if (op == 'Q') {
cin >> x >> y;
cout << query(x, y, 1) << '\n';
}
else {
cin >> x >> y >> k;
modify(x, y, k, 1);
}
}
return 0;
}