Medium POJ 2828 Buy Tickets, reverse order + line tree.

Source: Internet
Author: User

Description:

Railway tickets were difficult to buy around the Lunar New year in China, so we must get up early and join a long queue ...

The Lunar New year is approaching, but unluckily the Little Cat still had schedules going here and there. Now, he had-to-train-Mianyang, Sichuan Province for the winter camp selection of the national team of Olympia D in Informatics.

It was one o ' clock a.m. and dark outside. Chill Wind from the northwest does not scare off the people in the queue. The cold night gave the Little Cat a shiver. Why isn't find a problem to think? That is none the less better than freezing to death!

People kept jumping the queue. Since It is too dark around, such moves would not being discovered even by the people adjacent to the queue-jumpers. "If every person in the queue is assigned a integral value and all the information to those who has jumped the queue And where they stand after queue-jumping are given, can I find out the final order of people in the queue? " Thought the Little Cat.

It means that a few people are in line, someone cut in the queue, and then ask what the final queues are like.

It's a classic question (but I didn't think it out.) T).

From the back to the enumeration, achievements. First of all, where is the last person, then the person in front of him if the original position after the last person inserted in the position, then you need to move back one position.

So when it comes to someone (in reverse enumeration), assuming that he is in the 3 position, then the third empty place (already counted) is his position.

The code is as follows:

#include <iostream>#include<cstdio>using namespacestd;intbit[200002*4];intN;intnum[200002];intval[200002];intans[200002];voidBuild_tree (intLintRintPO) {Bit[po]=r-l+1; if(L==R)return; intM= (L+R)/2; Build_tree (L,m,po*2); Build_tree (M+1, r,po*2+1);}intQuery_update (intQnintLintRintPO) {    --Bit[po]; if(L==R)returnL; intM= (L+R)/2; if(bit[po*2]>=qn)returnQuery_update (qn,l,m,po*2); Else        returnQuery_update (qn-bit[po*2],m+1, r,po*2+1);}intMain () {inttemp;  while(~SCANF ("%d",&N)) { for(inti=n;i>=1;--i) scanf ("%d%d",&num[i],&Val[i]); Build_tree (1N1);  for(intI=1; i<=n;++i) {temp=query_update (num[i]+1,1N1); Ans[temp]=Val[i]; }         for(intI=1; i<=n;++i) printf ("%d", Ans[i]); printf ("\ n"); }    return 0;}

Medium POJ 2828 Buy Tickets, reverse order + line 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.