Update: I got AC, i only changed the way of getting the elements to insert in the Treap, this code help me. So the problem is not the treap, is the priority queue, but i'm still wanna kown why Memory Limit .-.
Sadly, i have a new problem that i can't solve by myself. ( I'm still trying to solve it )
I made a solution that i think it should pass both D1 and D2 problems but i'm getting Memory limit exceeded on test 10.
Here is my submission from D1 code. The difference from D2 are the lines 89-91 because of the limits ( I only did this because i didn't know what else to do :( ).
I think that my idea it's quite complicated to explain ( at least for me ), but i am working on it, maybe knowing what my code does someone would be able to help me..
But, as i can see i only do $$$N$$$ insertions to the Treap and $$$N$$$ insertions to an auxiliary priority queue, i don´t have idea why i´m getting memory limit.
I will appreciate any comment. Thank you :D