Prom Night Problem

I’m approaching this problem with a time complexity of o(n^2).
can you tell me a more optimized way to approach this problem.
The question is there itself in the link to my solution.

You can siimply build segment tree or binary index tree for range queries and answer for the queries accordingly. The time complexity for each query will be log(n) and hence it will not give TLE. alternatively you can use MO’s algortithm but its time complexity will n*sqrt(n).

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

Hi , can you please post the link of the optimized code, i am not able to figure it out
TIA