๐ WAYPOINT LESSON
Prefix Sums + Binary Search
โญโญโญ advancedโณ 30 min read๐ Lesson 181 of 281
When the prefix array is monotone, every two-condition subarray count becomes two binary searches โ one idea, dozens of problems.
Monotone prefixes unlock counting
Positive values make prefix sums increasing; the subarray (i, j] has sum in [L, R] exactly when P[j]-R <= P[i] <= P[j]-L. Count positions i with two binary searches. If values can be negative, prefixes stop being monotone โ the same counting then needs BIT/merge-sort (next lesson).