Luogu P3374 【模板】树状数组 1

题目描述

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

单点加,区间求和

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, a[maxn];

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; scanf("%d%d", &x, &y);
add(x, y);
}

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

int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
int x; scanf("%d", &x);
add(i, 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;
}