Please tell the mistake in the code sir it is giving segmentation fault

1)sir on printing the treeseg.size() it is printing 21 for the test case below (in line 93 of the code);
2) sir i think there is some problem with the query function.
https://ide.codingblocks.com/s/199476 is my code id
5

1 2 3 4 5

5

1 4 1

1 5 2

1 5 3

1 5 4

1 5 5
is the required test case

maybe your merging part is wrong.
Try to use STL inbuilt merge function to merge two vectors
v1 -> left vector
v2 -> right vector
v -> into which the merginf occurs.
std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(v) );