Poj2828 -- buy tickets

Source: Internet
Author: User

A total of N people come to the queue one by one. Each person has a requirement to be placed behind the first few people (of course, it must be the requirements of the last person ), each person has a corresponding Val, which outputs the Val of n persons in order.

We use the line segment tree to maintain the number of remaining positions in the interval. Of course, we must update the line segment tree from the last person, after each update, set the remaining quantity of the location to 0 (because the requirements of people later must be met first)

The Code is as follows:

#include <set>#include <map>#include <cmath>#include <ctime>#include <queue>#include <stack>#include <cctype>#include <cstdio>#include <string>#include <vector>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef unsigned long long ull;typedef long long ll;const int inf = 0x3f3f3f3f;const double eps = 1e-8;const int maxn = 2e5+10;int seg[maxn<<2],ans[maxn],Pos[maxn],val[maxn],n;void build(int l,int r,int pos){    if (l == r)    {        seg[pos] = 1;        return;    }    int mid = (l + r) >> 1;    build(l,mid,pos<<1);    build(mid+1,r,pos<<1|1);    seg[pos] = seg[pos<<1] + seg[pos<<1|1];}int update(int l,int r,int pos,int x){    if (l == r)    {        seg[pos] = 0;        return l;    }    int mid = (l + r) >> 1;    int ans;    if (seg[pos<<1] >= x)        ans = update(l,mid,pos<<1,x);    else        ans = update(mid+1,r,pos<<1|1,x-seg[pos<<1]);    seg[pos] = seg[pos<<1|1] + seg[pos<<1];    return ans;}int main(void){    #ifndef ONLINE_JUDGE        freopen("in.txt","r",stdin);    #endif    while (~scanf ("%d",&n)){  for (int i = 0; i < n; i++)      {          scanf ("%d%d",Pos+i,val+i);      }      build(1,n,1);      for (int i = n - 1; i >= 0; i--)      {          int tmp = update(1,n,1,Pos[i]+1);          ans[tmp-1] = val[i];      }      printf("%d",ans[0]);      for (int i = 1; i < n; i++)      {          printf(" %d",ans[i]);      }      printf("\n");}return 0;}

  

Poj2828 -- buy tickets

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.