from : acwing
#include <bits/stdc++.h> using namespace std; const int n = 100001; int n, m; int tree[n], a[n]; int lowbit(int x){ return x & -x; } void update(int x, int v){//这是下标x数加上v值 for(int i = x; i <= n; i += lowbit(i)) tree[i] += v; } int query(int x){//求前缀和 int res = 0; for(int i = x; i; i -= lowbit(i)) res += tree[i]; return res; } int main(){ cin >> n >> m; for(int i = 1; i <= n; i ++ ) cin >> a[i]; for(int i = 1; i <= n; i ++ ) update(i, a[i]);//这是初始化前缀和,不是以前那样 int k, x, y; while(m -- ){ cin >> k >> x >> y; if(k == 0){ cout << query(y) - query(x - 1) << "n"; }else update(x, y); } return 0; }
0 条评论