We code a Sparse Segment Tree live and take a look at optimizations, generalizations, and tricks you can use in the contests. All of the code is on GitHub and I hope we can improve up on it. Even if you don't how a segment tree works, if you need a sparse segment tree, you can use this implementation by just changing the functions inside the "Data" struct to fit your problem. What we coded: https://github.com/mitkonikov/competi... Optimized: https://github.com/mitkonikov/competi... Segment Tree solutions: https://github.com/mitkonikov/competi...