Test instructions is to give you n person, everyone has their own POS to insert and Val, ask you for the final sort
Idea: Reverse insert, the last person's position must be fixed, because must be inserted continuously, so I must be inserted in the first person to ensure that there is pos[i] empty space,
Then use the line tree to record the number of positions, if the location is not enough, move backwards.
#include <iostream>#include<cstdio>using namespacestd;#defineN 200005intspare[n<<2];intSeq[n];intPos[n],val[n];voidPushdown (intRT) {Spare[rt]=spare[rt*2]+spare[rt*2+1];}voidBuildintRtintLintR) { if(l==R) {Spare[rt]=1; return; } intMid= (L+R)/2; Build (Rt*2, L,mid); Build (Rt*2+1, mid+1, R); Pushdown (RT);}voidInsertintPosintValintRtintLintR) { if(l==R) {Seq[l]=Val; SPARE[RT]=0; return; } intMid= (L+R)/2; if(pos<spare[rt*2]) {Insert (Pos,val,rt*2, L,mid); } Else{Insert (pos-spare[rt*2],val,rt*2+1, mid+1, R); } pushdown (RT);}intMain () {intN; while(~SCANF ("%d",&N) {build (1,1, N); for(intI=0; I<n; i++) {scanf ("%d%d",&pos[i],&Val[i]); } for(inti=n-1; i>=0; i--) {Insert (Pos[i],val[i],1,1, N); } for(intI=1; i<=n; i++. ) { if(i==N) {printf ("%d\n", Seq[i]); } Elseprintf ("%d", Seq[i]); } } return 0;}
Buy Tickets POJ-2828 line segment tree for queue jumping