Test instructions
There are n individuals, each with a certain value, to n arrangement, each arrangement has two number p,v P is the number of people in front of this person (directly inserted in the P-person behind other people to move back), V is its value, N arranges to seek the final value sequence.
Analysis:
The more in the back of the arrangement the more his final position, all we have to do from the back, every time we find the position of P in front, find the vacancy minus one, with the line segment tree to maintain the interval number of slots.
#include <map>#include<Set>#include<list>#include<cmath>#include<queue>#include<stack>#include<cstdio>#include<vector>#include<string>#include<cctype>#include<complex>#include<cassert>#include<utility>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespaceStd;typedef pair<int,int>Pii;typedefLong Longll;#defineLson l,m,rt<<1#definePi ACOs (-1.0)#defineRson m+1,r,rt<<1|1#defineAll 1,n,1#defineRead Freopen ("In.txt", "R", stdin)#defineN 200010Constll infll =0x3f3f3f3f3f3f3f3fll;Const intinf=0x7ffffff;Const intMoD =1000000007;intnum[n<<2],a[n],v[n],n,pos;voidBuildintLintRintRT) {Num[rt]= (r-l) +1;//Initial full Vacancy if(L==R)return; intM= (l+r) >>1; Build (Lson); Build (Rson);}voidUpdateintSintLintRintRT) {Num[rt]--;//corresponding interval vacancy minus one if(l==R) {POS=l; return; } intM= (l+r) >>1; if(s<=num[rt<<1]) update (S,lson);//Find left dial hand tree first Else{s-=num[rt<<1]; Update (S,rson);//find the right subtree-left dial hand tree vacancy }}intMain () {intTmp[n]; while(~SCANF ("%d",&N)) {Build (1N1); for(intI=0; i<n;++i) {scanf ("%d%d",&a[i],&V[i]); } for(inti=n-1; i>=0; i--) {update (A[i]+1,1N1);//I need a vacancy .tmp[pos]=V[i]; } for(intI=1; i<=n;++i) printf ("%d", Tmp[i]); printf ("\ n"); }return 0;}
POJ 2828-buy Tickets (segment tree)