Code Room2D Fenwick tree
HardPrep Room Coding #880

2D Fenwick tree

CodingAlgorithms & data structuresSenior–Staff~40 min

Maintain a rows x cols matrix initialized to all zeros using a 2D Fenwick (binary indexed) tree. Process operations: ["update", r, c, v] adds v to cell (r, c), and ["query", r1, c1, r2, c2] returns the sum of the inclusive submatrix with top-left (r1, c1) and bottom-right (r2, c2). All indices are 0-based and in range. Return the list of query answers in order.

Implement
fenwick_2d(rows: int, cols: int, ops: list[list]) → list[int]
Examples
in[3,3,[["update",0,0,5],["update",2,2,3],["query",0,0,2,2],["query",1,1,2,2]]]out[8,3]
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,[["update",0,0,5],["update",2,2,3],["query",0,0,2,2],["query",1,1,2,2]]][8,3]not run yetsample