Luogu P3368 【模板】树状数组 2

题目描述

https://www.luogu.com.cn/problem/P3368

区间加,单点求和

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <cstdio>
#define maxn 500010
#define lowbit(i) ((i) & (-i))
using namespace std;

int n, m;

int Bit[maxn];
inline void add(int i, int v) { while (i <= n) Bit[i] += v, i += lowbit(i); }

inline int get_sum(int i) {
int s = 0;
while (i) s += Bit[i], i -= lowbit(i);
return s;
}

inline void solve_1() {
int x, y, z; scanf("%d%d%d", &x, &y, &z);
add(x, z); add(y + 1, -z);
}

inline void solve_2() {
int x; scanf("%d", &x);
printf("%d\n", get_sum(x));
}

int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
int x; scanf("%d", &x);
add(i, x); add(i + 1, -x);
}
for (int i = 1; i <= m; ++i) {
int opt; scanf("%d", &opt);
switch (opt) {
case 1 : solve_1(); break;
case 2 : solve_2(); break;
}
}
return 0;
}