No Need of segment Tree. First Use coordinate compression for X and Y. Now you may observe that after doing this you will have atmost 2*10^5 distinct elements left. Now when you are compressing X and Y , just store this mapping between original number and new mapped value in a map. After you have done this process X and Y values one by one. Suppose current student's mapped range is x=mp[X] and y=mp[Y]. So adding one to a given range can be done by using this simple trick: A[x]+=1 and A[y+1]-=1. Here A is the array initialized to 0. Do this range addition for all students.

Once you have done this , just take the prefix sum from 1 to 2*10^5(in worst case). Now check all indices from 1 to 2*10^5 and find the smallest index having the maximum value. So simply this is the maximum no. of students that can be made happy and to get the minimum no. of candies just output the map value of the smallest index for which maximum value is encountered.