Code Room2D Fenwick tree
HardPrep Room Coding #1104

2D Fenwick tree

CodingAlgorithms & data structuresSenior–Staff~40 min

Implement a 2D Fenwick (binary indexed) tree over a rows x cols grid initialized to zero. Process operations: ["set", r, c, v] adds v to cell (r, c); ["query", r1, c1, r2, c2] returns the sum of the inclusive sub-rectangle with corners (r1, c1) and (r2, c2) where r1<=r2 and c1<=c2. Return the list of query answers in order. Both operations must run in O(log(rows) * log(cols)).

Implement
matrix_sum_2d(rows: int, cols: int, ops: list) → list[int]
Examples
in[3,3,[["set",0,0,5],["set",1,1,3],["set",2,2,2],["query",0,0,2,2],["query",1,1,2,2]]]out[10,5]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 40 min
InputExpectedGot
[3,3,[["set",0,0,5],["set",1,1,3],["set",2,2,2],["query",0,0,2,2],["query",1,1,2,2]]][10,5]not run yetsample