Data Structures & AlgorithmsHardCodeforces Div2 C · GATE CS
A Fenwick tree t[1..6] (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(2, 2), add(1, 3), add(6, 1), add(3, -3), add(4, 5), add(5, 5), add(2, -2). What is the final value of t[6]?
- A. 11
- B. 0
- C. 1
- D. 6
Correct answer: D. 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].
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- 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 &…
- A Fenwick tree t[1..6] (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…
- A Fenwick tree on 1..9 starts all zero and receives add(i, v) implemented as j = i; while j ≤ n: t[j] += v; j += j & −j. The calls, in…
- A Fenwick tree on 1..10 starts all zero and receives add(i, v) implemented as j = i; while j ≤ n: t[j] += v; j += j & −j. The calls, in…
- A Fenwick tree on 1..11 starts all zero and receives add(i, v) implemented as j = i; while j ≤ n: t[j] += v; j += j & −j. The calls, in…
- A Fenwick tree on 1..9 starts all zero and receives add(i, v) implemented as j = i; while j ≤ n: t[j] += v; j += j & −j. The calls, in…