poj 2828 Buy Tickets

http://poj.org/problem?id=2828


思路:将输入数据逆序传入,pos[i]表示此时这个人前面应有多少个空位。所以就用线段树来维护某个区间内的空位个数。在查找操作时如果t[rt<<1]<=target,就将target减去t[rt<<1],在右子树中找,最终定位在target==0且l==r的位置。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#define fi first
#define se second
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
//head
const int MAX = 2e5+5;
P q[MAX];
int t[MAX<<2];
int ans[MAX];

void pushUp(int rt)
{
    t[rt] = t[rt<<1] + t[rt<<1|1];
}

void build(int l, int r, int rt)
{
    if(l==r)
    {
        t[rt] = 1;
        return;
    }
    int m = (l+r)>>1;
    build(lson);
    build(rson);
    pushUp(rt);
}

void update(int pos, int l, int r, int rt)
{
    if(l==r)
    {
        t[rt]--;
        return;
    }
    int m = (l+r)>>1;
    if(pos<=m)
        update(pos, lson);
    else
        update(pos, rson);
    pushUp(rt);
}

int query(int target, int l, int r, int rt)//return the position to add the entry
{
    if(l==r && target==0)
        return r;
    int m = (l+r)>>1;
    if(t[rt<<1]<=target)
        return query(target-t[rt<<1], rson);
    else
        return query(target, lson);
}

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        build(1,n,1);
        for(int i=1; i<=n; i++)
            scanf("%d%d",&q[i].fi,&q[i].se);
        for(int i=n; i>=1; i--)
        {
            int pos = query(q[i].fi, 1, n, 1);
            ans[pos] = q[i].se;
            update(pos, 1, n, 1);
        }
        for(int i=1; i<n; i++)
            printf("%d ",ans[i]);
        printf("%d\n",ans[n]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值