树状数组
Single point modification, interval query
the code is follow 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
using namespace std;
const int N = 5e5+10;
int n, m;
int a[N], tree[N<<2];
inline void chenge (int x, int val) {
for (int i = x; i <= n; i += lowbit(i))
tree[i] += val;
}
inline int ask (int x) {
int res = 0;
while (x) {
res += tree[x];
x -= lowbit(x);
}
return res;
}
int main () {
scanf ("%d%d", &n, &m);
for (int i = 1; i <= n; ++ i) {
scanf ("%d", &a[i]);
chenge (i, a[i]);
}
for (int i = 1; i <= m; ++ i) {
int opt, x, k;
scanf ("%d%d%d", &opt, &x, &k);
if (opt == 1) chenge (x, k);
else cout << ask(k)-ask(x-1) << '\n';
}
return 0;
}
and the Interval modification, single point query
the code is follow:
1 |
|
PS
1.s must from zero
2.the tree array is so easy!