I tried to debug your code but sadly I was unable to figure out the exact bug . I recently solved this question so I can share my approach with you.

Let us store 2 values at every node of the segment tree, (i) no. of **unpaired opening brackets** (ii) no. of **unpaired closing brackets** in that particular interval. For Merging nodes, the first value will be the unpaired opening brackets from the right node(as they will not be paired with left node) plus the remaining brackets which are not paired with the unpaired closing brackets from the right node while merging. The second value can also be calculated similarly. For checking whether an expression is correct or not just check whether the two values are zero for the root node. Have a look at my code for better understanding, it is very similar to your code the only difference is I am storing 2 values instead of 4. AC Code