POJ 2828 Buy Tickets

题意:

有很多人在排队,给你的信息是相当于是,这个人排在第几个人的后面(0 就代表他是第一个)和他的名字。

如果有两个人都排在了第k个人的后面,先来的那个排在后面,后来的在前面。

问这些人最终的顺序是什么。

思路:

同样是一个简单的线段树的应用。

由于是先来的在后面,后来的在前面的,如果按照正常的顺序来的话, 就会造成数组的移动,

所以我们可以从后面到前面进行处理,这样做的话,就可以不用移动数组了。

线段树可以做到查询我到底这个人插到后面还是前面,每一个节点代表的都是这个范围内还有几个空位。

叶子节点如果没有人是1,有人的话是0,然后再向上更新父亲节点的数目。

Code:

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<map>
#include<cctype>
#include<vector>

//#define TEST

#define LL long long
#define Mt(f, x) memset(f, x, sizeof(f));
#define xep(i, n) for(int i = 0; i < (n); ++i)
#define rep(i, s, e) for(int i = (s); i <= (e); ++i)
#define dep(i, s, e) for(int i = (s); i >= (e); --i)
#ifdef TEST
    #define See(a) cout << #a << " = " << a << endl;
    #define See2(a, b) cout << #a << " = " << a << ' ' << #b << " = " << b << endl;
    #define debug(a, s, e){ rep(_i, s, e) {cout << a[_i] << ' '; }cout << endl;}
    #define debug2(a, s, e, ss, ee) rep(i_, s, e) {debug(a[i_], ss, ee);}
#else
    #define See(a) {}
    #define See2(a, b) {}
    #define debug(a, s, e) {}
    #define debug2(a, s, e, ss, ee) {}
#endif

const int MAX = 2e9;
const int MIN = -2e9;
const int PI = acos(-1.0);
const double eps = 1e-9;

using namespace std;

#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

const int N = 200000 + 5;

int r[N], v[N];
int ans[N];
int p[N << 2];

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

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

void update(int I, int v, int l, int r, int rt)
{
    if(l == r)
    {
        ans[l] = v;
        p[rt] = 0;
        return ;
    }
    int m = (l + r) >> 1;
    if(I <= p[rt << 1])
    {
        update(I, v, lson);
    }
    else
    {
        update(I - p[rt << 1], v, rson);//减去左边还可以插人个数
    }
    pushUp(rt);
}

int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        build(1, n, 1);
        xep(i, n)
        {
            scanf("%d%d", &r[i], &v[i]);
        }
        dep(i, n - 1, 0)
        {
            update(r[i] + 1, v[i], 1, n, 1);
        }
        printf("%d", ans[1]);
        rep(i, 2, n)
        {
            printf(" %d", ans[i]);
        }
        printf("\n");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值