Buy Tickets POJ-2828 line segment tree for queue jumping

Source: Internet
Author: User

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

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.