10 5
1 2 3 4 5 6 7 8 9 10
1 1 5
0 1 3
0 4 8
1 7 5
0 4 8
11
30
35
#include<bits/stdc++.h> #include<queue> #include<cstdio> #include<cstring> #include<cmath> #include<map> #include<iostream> #define REP(i, a, b) for(int i = (a); i <= (b); ++ i) #define REP(j, a, b) for(int j = (a); j <= (b); ++ j) #define PER(i, a, b) for(int i = (a); i >= (b); -- i) using namespace std; const int maxn = 1e7 + 5; int n, k,p[maxn],m,x; int lowbit(int a) { return a & (-a); } void add(int pos,int w) { while (pos <= n) { p[pos] += w, pos += lowbit(pos); } } int sum(int pos) { int tot=0; while (pos) { tot += p[pos]; pos -= lowbit(pos); } return tot; } int main(){ cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> x; add(i, x); } while (m--) { int x, u, v; cin >> x >> u >> v; if (x) { add(u, v); } else { cout << sum(v) - sum(u - 1) << endl; } } }
原文:https://www.cnblogs.com/czy-power/p/10357688.html