Segment Tree
Range queries and point updates in O(log n) — sums, minimums, maximums, and more.
This topic is part of Pro.
Free covers the C and Python courses, the first three DSA topics, the opening system design lessons and the beginner AI lessons, all with the full coach. Pro opens the rest of the library in the FollowtheDamnCode app.
What is in it
- 1How a segment tree is shapedEach node owns a range and stores its answer; any query range is covered by O(log n) nodes. Pro
- 2Build, query, and updateThree short recursive functions — build bottom-up, query by covering, update along one path. Pro
- 3Range updates with lazy propagationPostpone updates on whole ranges, record them as pending tags, and push them down only when needed. Pro