Fenwick Tree

Given an array of length $N$, it processes the following queries in $O(\log N)$ time.

Constructor

fenwick_tree<T> fw(int n)

Constraints

Complexity

add

void fw.add(int p, T x)

It processes a[p] += x.

Constraints

Complexity

sum

T fw.sum(int l, int r)

It returns a[l] + a[l + 1] + ... + a[r - 1]. If T is integer type(int, uint, ll, or ull), it returns the answer in $\bmod 2^{\mathrm{bit}}$, if overflowed.

Constraints

Complexity

Examples

AC code of https://atcoder.jp/contests/practice2/tasks/practice2_b

import atcoder/header import atcoder/fenwicktree let n, q = nextInt() var fw = init_fenwick_tree[int](n) for i in 0..<n: let a = nextInt() fw.add(i, a) for i in 0..<q: let t = nextInt() if t == 0: let p, x = nextInt() fw.add(p, x) else: let l, r = nextInt() echo fw.sum(l..<r)