Debugging is a great skill. You won't achieve that if you just tell others to debug your code.
Consider a polynomial
P(x) = x^a0 + x^a1 + x^a2 + x^a3 + ..... + x^an
Now count of (i, j, k, l) such that a[i] + a[j] + a[k] + a[l] = T is just coefficient of x^T in P(x)^4
But you need to adjust the answer because this method counts all permutations of (i, j, k, l) as different!
I think the post is not well-phased. If you keep some array in each node of Segment Tree, that doesn't make a segment tree a 2D segment tree. 2D segment trees are different.
However, I've seen so many people who think quad-tree is 2D segment tree. Where each node has 4 child.
But most of them don't know quad-tree is O(n) per query in worst case or even worse, while 2D segment tree can query in sub-grid in O(log^2 n). That because maybe googling with 2D segment tree, a stackoverflow post comes up that describes quad-tree as 2D segment tree :3
I think for ConvexHull "Monotonic Chain Method" is more simple than "Graham Scan". It is also very easy to understand why and how it works.
In this method, we first find the left most point, start from that and go to right as long as we can, in CCW order. They will form a lower hull
Then from the rightmost point, we again go in CCW order as long as we can, they will form the upper hull.
Here is a full explanation - Convex hull-Monotone chain