Codeforces Round 710 Div3 Restoring the Permutation

I need to find if I can optimize my solution further using a suitable data structure or a different approach…
problem link: Problem - E - Codeforces
solution link:Submission #111220272 - Codeforces
Thank you in advance

I did in O(n) with two pointers and stack: Submission #111225459 - Codeforces

can you explain your idea to me?