If you've solved enough competitive programming problems, you've probably written a segment tree more times than you'd like to admit.
Every contest.
Every new repository.
Every interview.
The same build(), query(), update() functions over and over again.
So I decided to stop rewriting it and turn it into a reusable Go package.
Why?
Go has fantastic standard libraries, but when it comes to common data structures used in algorithms, I often find myself copying old code from previous solutions.
I wanted something that I could simply install and use:
go get github.com/satya-sudo/segmenttree
instead of searching through old repositories.
Design Goals
While building the package, I tried to keep a few principles in mind:
- Simple API
- Generic implementation (works beyond just integer sums)
- Fast point updates
- Efficient range queries
- Easy to plug into competitive programming as well as real projects
The goal wasn't to build the most feature-rich segment tree ever—it was to build one that I'd actually enjoy using.
Typical Operations
A segment tree gives you:
- Build in O(n)
- Range Query in O(log n)
- Point Update in O(log n)
Which makes it incredibly useful for problems involving:
- Range Sum
- Range Minimum
- Range Maximum
- GCD queries
- XOR queries
- Any associative operation
Why publish it?
One thing I've been trying to do this year is stop treating useful code as "just another snippet."
If something solves a recurring problem, it deserves to be packaged, documented, versioned, and shared.
Open source isn't only about building huge frameworks.
Sometimes it's about removing a tiny bit of friction for the next developer—including your future self.
Repository
⭐ GitHub: https://github.com/satya-sudo/segmenttree
I'd love feedback, feature requests, or even criticism.
If you find it useful, consider giving it a star. It motivates me to keep building more Go libraries.
I'm planning to open-source more reusable Go utilities over the coming months instead of letting them live inside personal repositories.
If there's a data structure or utility you wish existed as a clean Go package, let me know.
Top comments (0)