poj 2828 Buy Tickets(线段树)

思路:从后往前插,pos表示他前面有多少的空位,叶子结点存他的val 这样就转化到线段树上 然后就简单了

//10736K      1641MS

#include <stdio.h>
#define L(x) (x<<1)
#define R(x) ((x<<1)+1)
#define M 200050
int loc[M],k;
struct tree
{
      int l,r;
      int val,pos;//pos表示当点有多少空位
}node[3*M];
struct data
{
      int pos,num;   
}pep[M];

void BuildTree(int left,int right,int u)
{
      node[u].l = left;
      node[u].r = right;
      if (left == right)
      {
              node[u].pos = 1;
              return ;
      }
      int mid = (left + right)>>1;
      BuildTree(left,mid,L(u));
      BuildTree(mid+1,right,R(u));
      node[u].pos = node[L(u)].pos + node[R(u)].pos;
}

void updata (int val,int pos,int u)
{
      if (node[u].l == node[u].r)
      {
              node[u].val = val;
              node[u].pos = 0;
              return ;
      }
      if (node[L(u)].pos > pos)
              updata (val,pos,L(u));
      else
              updata (val,pos - node[L(u)].pos,R(u));
      node[u].pos = node[L(u)].pos + node[R(u)].pos;
}

void query (int u)
{
      if (node[u].l == node[u].r)
      {
              loc[k++] = node[u].val;
              return ;
      }
      query (L(u));
      query (R(u));
}
int main ()
{
      int n,i;
      while (~scanf ("%d",&n))
      {
              k = 0;
              for (i = 0;i < n;i ++)
                      scanf ("%d %d",&pep[i].pos,&pep[i].num);
              BuildTree(1,n,1);
              for (i = n-1;i >= 0;i --)
                      updata (pep[i].num,pep[i].pos,1);
              query (1);
              for (i = 0;i < n - 1;i ++)
                      printf ("%d ",loc[i]);
              printf ("%d\n",loc[n-1]);
      }
      return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值