Pojbuy tickets2828 line segment tree (queued in reverse order)

Source: Internet
Author: User
 
The idea at the beginning of this question is to simulate: Just like inserting points in the array, everyone who wants to buy a ticket wants to jump into the queue! But in this case, it must have been recognized by TLE! Positive Solution: insert the beginning in the forward direction into the inverted direction. In this case, think about the position where I personally want to insert P [I, therefore, you must ensure that there must be a P [I]-1 vacant space before the inserted position! Because p [J] <p [I] (0 <= P [J] <= J, everybody wants to jump forward) the J-person in front of P [I! If I <j; & P [I] = P [J], insert in reverse order, then, the I-th person must insert P [I]-1 vacant space before the inserted position to the back of the J-th person!
 

 



1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 #define M 200005 6 using namespace std; 7 8 pair<int, int>per[M]; 9 int ret[M]; 10 int tree[M*4];11 int n;12 13 void buildT(int p, int ld, int rd){14 if(ld==rd){15 tree[p]=1;16 return ;17 }18 int mid=(ld+rd)>>1;19 buildT(p<<1, ld, mid);20 buildT(p<<1|1, mid+1, rd);21 tree[p]=tree[p<<1]+tree[p<<1|1]; 22 }23 24 void updateT(int p, int ld, int rd, int pos, int val){25 if(ld==rd){26 tree[p]=0;27 ret[ld]=val;28 return ;29 }30 int mid=(ld+rd)>>1;31 if(tree[p<<1]>pos)32 updateT(p<<1, ld, mid, pos, val);33 else 34 updateT(p<<1|1, mid+1, rd, pos-tree[p<<1], val);35 36 tree[p]=tree[p<<1]+tree[p<<1|1]; 37 }38 39 int main(){40 int i;41 while(scanf("%d", &n)!=EOF){42 buildT(1, 1, n); 43 for(i=1; i<=n; ++i)44 scanf("%d%d", &per[i].first, &per[i].second);45 for(i=n; i>=1; --i)46 updateT(1, 1, n, per[i].first, per[i].second);47 48 printf("%d", ret[1]);49 for(i=2; i<=n; ++i)50 printf(" %d", ret[i]);51 printf("\n"); 52 }53 return 0;54 }

 

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.