'commit'
This commit is contained in:
42
TangDou/AcWing/BIT/P3368_1.cpp
Normal file
42
TangDou/AcWing/BIT/P3368_1.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#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() {
|
||||
scanf("%d %d", &n, &m);
|
||||
|
||||
// 原数组
|
||||
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
|
||||
|
||||
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", a[k] + sum(k)); // 求某点的值
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user