Data Structures19 sections · 729 units
Open in Course

Problem - Forest Queries

2D static queries

Given an n×nn \times n grid with trees (*) and empty cells (.), answer queries: how many trees in rectangle (r1,c1,r2,c2)(r_1, c_1, r_2, c_2)? This is 2D range sum on a static grid. Options:

1.1. 2D prefix sums: O(nm)O(nm) build, O(1)O(1) query

2.2. 2D Sparse Table for sum: doesn't work (sum not idempotent) For counting, 2D prefix sums is the right choice. But if the query was "what's the maximum value in this rectangle?", 2D sparse table would apply. Constraints: nn up to 10001000, qq up to 21052 \cdot 10^5.