Comments

i got it!thanks.

How about n=3?

in the editorial of B,when the multiset at d is empty,just let end[d]=0 and update ans[d](it bother me for a while)