Data Structures & AlgorithmsHardCodeforces Div2 C · GATE CS

A Fenwick tree t[1..12] (1-indexed, all zero at the start) is updated by add(i, v): set j = i, then while j ≤ n do { t[j] += v; j += j & −j }. The calls, in order, are add(10, -1), add(11, 6), add(5, 3), add(1, -1), add(8, 6), add(12, 5), add(12, -4). What is the final value of t[12]?

  1. A. 6
  2. B. 0
  3. C. 1
  4. D. 14

Correct answer: A. 6

Each update touches exactly the nodes on the chain j, j + lowbit(j), ... up to n. Stopping early or updating only t[i] gives a different value; t[k] is generally not the point value a[k].

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic