POJ 2828-buy Tickets (segment tree)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.