Why am I getting SIGSEGV in this problem ?

My solution

The amount of memory needed for tree is in your data segment and it is not enough for the tree array, you must store it in the heap.

// instead of initializing the array as this:
#define MAX 100002
int tree[MAX][26];

// try doing something like this
#include <stdlib.h>
#define MAX 100002
int **tree;
int main() {
    // allocate "array" of pointers to "array of int" length MAX into heap
    tree =  (int**)malloc(MAX * sizeof(int*));
    for(int i=0;i<MAX;i++)
        // allocate "array" of 26 zero integers into each tree's element
        tree[i] = (int*)calloc(26, sizeof(int));


Isn’t global variable stored in heap by default?

My understanding is that it is not, it’s a place called Data Segment, and while it will not complain of a stack overflow, it does complain when you try to access data out of bounds. ie. suppose you have space for an array size 500, you will not get an error when declaring int example[501], but you will get it if you try to access the 501st element cout << example[500]

Thank you for your help. I will try it when submission link works.

See my segment tree solution for the same: I have allocated more memory and that too the very same way as done by you @dragonemperor