Poj buy tickets

Source: Internet
Author: User

Question Analysis:

There are n queues for you. Each person has a location where they want to stand. You need to sort them from the past to the next and output the final result. Note that the people behind will overwrite the previous ones. That is, the person who was originally at this position moves a position.

Algorithm analysis:

We can regard the total number of people as the size of the range, and then the result is to put every position in the range on the top, that is, the answer.

From the questions, we can know that the people behind the questions are not influenced by the people above. So we can simulate the process.

How to simulate it? We can think of line tree (who thinks of it? I didn't think of it anyway). Based on the characteristics of the Line Segment tree, we can assign values to the values of each interval to the available positions of the interval. In this way, quick search can be achieved.

In fact, the key to this question is whether to assign the value of the range to the available number of positions in the range when the line segment tree is used. If you think of it, the line segment tree is very basic.


#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define L(x) (x << 1)#define R(x) (x << 1|1)#define MOD(a,b) (a+((b-a) >> 1))#define lson lft,md,rt << 1#define rson md+1,rht,rt << 1|1const int MAXN = 200000 + 10;struct Node{    int lft,rht,val;    int mid(){return MOD(lft,rht);}};Node tree[4*MAXN];int n,ran[MAXN],num[MAXN],res[MAXN];class SegTree{public:    void Build(int lft,int rht,int rt){         tree[rt].lft = lft; tree[rt].rht = rht;         tree[rt].val = rht - lft + 1;         if(lft != rht){            int md = tree[rt].mid();            Build(lson);            Build(rson);         }    }    int Query(int val,int rt){         int lft = tree[rt].lft,rht = tree[rt].rht;         if(lft == rht){            tree[rt].val = 0;            return lft;         }         else{            int pos;            if(val <= tree[L(rt)].val)pos = Query(val,L(rt));            else pos = Query(val-tree[L(rt)].val,R(rt));            tree[rt].val = tree[L(rt)].val + tree[R(rt)].val;            return pos;         }    }};int main(){    while(~scanf("%d",&n)){        SegTree seg;        seg.Build(0,n-1,1);        for(int i = 0;i < n;++i){            scanf("%d%d",&ran[i],&num[i]);        }        int pos;        for(int i = n-1;i >= 0;--i){            pos = seg.Query(ran[i]+1,1);                   res[pos] = num[i];        }        for(int i = 0;i < n-1;++i)            printf("%d ",res[i]);        printf("%d\n",res[n-1]);    }    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.