Files
python/TangDou/AcWing/BIT/243_SZSZ_1.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

49 lines
1.0 KiB
C++

#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
// 树状数组
typedef long long LL;
#define lowbit(x) (x & -x)
LL c1[N], c2[N];
void add(LL x, LL d) {
for (LL i = x; i < N; i += lowbit(i)) c1[i] += d, c2[i] += x * d;
}
LL sum(LL x) {
LL res = 0;
for (LL i = x; i; i -= lowbit(i)) res += (x + 1) * c1[i] - c2[i];
return res;
}
int n, m;
LL a[N];
int main() {
// 加快读入
ios::sync_with_stdio(false), cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("243.in", "r", stdin);
#endif
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
add(i, a[i] - a[i - 1]); // 保存基底是差分数组的树状数组
}
while (m--) {
string op;
int x, y, d;
cin >> op;
if (op[0] == 'Q') {
cin >> x >> y;
printf("%lld\n", sum(y) - sum(x - 1));
} else {
cin >> x >> y >> d;
add(x, d), add(y + 1, -d); // 维护差分
}
}
return 0;
}