Data Structures & AlgorithmsHardCodeforces Div2 C · ICPC regional
A Fenwick tree on 1..12 starts all zero and receives add(i, v) implemented as j = i; while j ≤ n: t[j] += v; j += j & −j. The calls, in order, are add(7, 4), add(2, 3), add(8, -2), add(6, 1), add(6, 4), add(12, -3). Then query(7) is run as s = 0; j = 7; while j > 0: s += t[j]; j −= j & −j; and returns s. What does query(7) return?
- A. 7
- B. 12
- C. 8
- D. 15
Correct answer: B. 12
The query visits the nodes k, k minus its lowest set bit, and so on, so it sums exactly the prefix a[1..k]. Adding up the tree entries directly, or querying k − 1, gives a different total.
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 on 1..8 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..6 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..6 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..8 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..12 starts all zero and receives add(i, v) implemented as j = i; while j ≤ n: t[j] += v; j += j & −j. The calls, in…