dsa1 min read
Falling Squares — Coordinate Compress + Segment Tree
Simulate falling squares, track tallest stack height. Coordinate compress x-coordinates, use segment tree with lazy propagation for range max.
Read →
webcoderspeed.com
1276 articles
Simulate falling squares, track tallest stack height. Coordinate compress x-coordinates, use segment tree with lazy propagation for range max.
Count subarray sums within [lower, upper]. Use prefix sums and merge sort: during merge count pairs of prefix sums with difference in range.
Count all LIS of maximum length. DP: track length and count. Segment tree on values enables O(n log n) solution.
Apply range shift operations to letters. Difference array: mark +1 at start and -1 at end+1, prefix sum gives net shift at each position.
Sum of rectangle region. Precompute 2D prefix sums: prefix[i][j] = sum of all cells from (0,0) to (i-1,j-1). Inclusion-exclusion for query.