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
| { int[] tree; int lowbit(int x) { return x & -x; } void add(int x, int u) { for (int i = x; i <= n; i += lowbit(i)) tree[i] += u; } int query(int x) { int ans = 0; for (int i = x; i > 0; i -= lowbit(i)) ans += tree[i]; return ans; }
}
{ for (int i = 0; i < n; i++) add(i + 1, nums[i]); }
{ void update(int i, int val) { add(i + 1, val - nums[i]); nums[i] = val; } int sumRange(int l, int r) { return query(r + 1) - query(l); } }
|