How to optimise SQRT Decomposition code?

I am learning SQRT Decomoposition and solving related problem, but after a certain test cases my code is exceeding time limit, How can I optimize this code?

Question

My submission

I haven’t read your code, but I feel that this might be useful.

DIFFERENCES IN SORT