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

48 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int a[N]; // a是原数组。c是差分数组,用树状数组维护
int n, m;
// 树状数组模板
typedef long long LL;
#define lowbit(x) (x & -x)
int c[N];
void add(int x, int d) {
for (int i = x; i < N; i += lowbit(i)) c[i] += d;
}
LL sum(int x) {
LL res = 0;
for (int i = x; i; i -= lowbit(i)) res += c[i];
return res;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("P3368.in", "r", stdin);
#endif
scanf("%d %d", &n, &m);
// 原数组
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
// 保存差分数组
for (int i = 1; i <= n; i++) add(i, a[i] - a[i - 1]);
int op, l, r, v, k;
while (m--) {
scanf("%d", &op);
if (op == 1) {
scanf("%d %d %d", &l, &r, &v);
add(l, v), add(r + 1, -v);
} else {
scanf("%d", &k);
// 树状数组维护的是变化量,还需要加上原来的值
printf("%d\n", sum(k)); // 求某点的值
}
}
return 0;
}