Best Segment tree template for python

Could anyone please provide their segment tree template in python and why do you think its best?

could any one explain this template of meooow.

1 Like

The segment tree I used is described in this blog post. It is an iterative implementation of segment tree which is simpler and usually faster than recursion. The last point is true especially for Python, and it is a good idea to avoid recursion in Python as much as possible (I don’t know about PyPy though). There is also a much more elaborate explanation and guide for iterative segment trees on Codeforces here.

5 Likes

hey @meooow I love Cp…but the fact is I only code in Python because I m very comfortable with it…but I m finding from last 1-2 weeks about perfect segment tree implementation in python but I was unable to do it…please recommend can we use fenwick tree instead of segment tree or the implementation u provide is equivalent to segment tree…???..Please help me…I m in trouble from many days…

1 Like

Hii… Do u have a similar variant for c++ as well? If yes, please link it.
Thanks :slight_smile:

@dishant_18 both the links I provided have implementations in C++, take a look.