Segment Tree giving SIGSEGV error in Chef and Strings (Code: STRQ)

I had used segment tree to solve the problem STRQ in Feb 15 Long Challenge. My solution passed the subtasks 1 and 2 but gives RE(SIGSEGV) in subtask 3. I’m not able to understand what’s wrong in my solution that is giving runtime error. Can someone please point out the mistake?
Here’s the link to my solution.

The size of segment tree is the problem. I prefer making it 4*N. Its giving TLE now.