I have implemented segment tree in python for this
[1].
It is running in some cases and for others abort is called in build function ( I checked that by debugging in Pycharm but I couldn't understand why code is misbehaving)
Any help would be appreciated :)
[1]: https://www.hackerrank.com/contests/adobe-hackathon/challenges/modular-queries/submissions/code/1304249692
The only possible reason is the input size is large, so the recursion depth exceeds the stack size resulting in runtime error. There are three solutions:-
1)Use sys.setrecursionlimit(x) to increase the stack size manually. This is a temporary fix, because x can’t be increased above certain range, and the input may be bigger.
2)Rewrite the recursive procedure in an iterative manner.
@anushi I saw your code. I think the problem is in the initialization of Segment tree. Why are you only making st of only N elements ?. In segment tree the number of nodes are 4*n. So I think it will cause a problem when n = 10^5. SO I think you should initialize it with 4N