Poj 2828 buy tickets (line segment tree)

Source: Internet
Author: User

Question:

Someone is waiting in the queue, and every time it is inserted behind the I-th person.

The final output order.


Train of Thought Analysis:

You will find that if reverse processing is performed, you will know the number of this person.

The problem is simplified at once.

It is just the number of null positions in the online segment tree.


#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <vector>#define lson num<<1,s,mid#define rson num<<1|1,mid+1,e#define maxn 200005using namespace std;struct foo{    int pos,val;}Q[maxn];int cnt[maxn<<2];int v[maxn];void update(int num,int s,int e,int k,int val){    if(s==e){        cnt[num]++;        v[s]=val;        return;    }    int mid=(s+e)>>1;    int t=mid-s+1-cnt[num<<1];    if(t>k)update(lson,k,val);    else update(rson,k-t,val);    cnt[num]=cnt[num<<1]+cnt[num<<1|1];}int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        memset(cnt,0,sizeof cnt);        for(int i=1;i<=n;i++){            scanf("%d%d",&Q[i].pos,&Q[i].val);        }        for(int i=n;i>=1;i--){            update(1,1,n,Q[i].pos,Q[i].val);        }        for(int i=1;i<=n;i++){            printf("%d%c",v[i],i==n?'\n':' ');        }    }    return 0;}


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.