POJ 2828 Buy Tickets (线段树 单点更新 查询右界)

题目大意:

就是告诉N个人依次插队的顺序要求输出最终的序列


大致思路:

对于整个插入倒着执行, 那么前面的插入不会影响后面插入的人

记初始数组为[0, 1, 1, 1, ...., 1]一共n + 1个数下标从0到n


那么每次就相当于找出当前前缀和大于p的插入(p, value)然后更新找到的位置的值为0即可

查询操作用线段树维护, 单点修改也是


代码如下:

话说C++1600ms+, G++3600ms+差别好大


Result  :  Accepted     Memory  :  4544 KB     Time  :  1672 ms

/*
 * Author: Gatevin
 * Created Time:  2015/8/15 23:09:30
 * File Name: Sakura_Chiyo.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

#define maxn 200020

struct Segment_Tree
{
    #define lson l, mid, rt << 1
    #define rson mid + 1, r, rt << 1 | 1
    int val[maxn << 2];
    void pushUp(int rt)
    {
        val[rt] = val[rt << 1] + val[rt << 1 | 1];
        return;
    }
    void build(int l, int r, int rt)
    {
        if(l == r)
        {
            if(l == 0) val[rt] = 0;
            else val[rt] = 1;
            return;
        }
        int mid = (l + r) >> 1;
        build(lson);
        build(rson);
        pushUp(rt);
    }
    void update(int l, int r, int rt, int pos, int value)
    {
        if(l == r)
        {
            val[rt] = value;
            return;
        }
        int mid = (l + r) >> 1;
        if(mid >= pos) update(lson, pos, value);
        else update(rson, pos, value);
        pushUp(rt);
    }
    int query(int l, int r, int rt, int value)
    {
        if(l == r)
        {
            return l;
        }
        int mid = (l + r) >> 1;
        if(val[rt << 1] > value) return query(lson, value);
        else return query(rson, value - val[rt << 1]);
    }
};

Segment_Tree ST;
pair<int, int> ins[maxn];
int seq[maxn];
int n;

int main()
{
    while(~scanf("%d", &n))
    {
        ST.build(0, n, 1);
        for(int i = 1; i <= n; i++)
            scanf("%d %d", &ins[i].first, &ins[i].second);
        for(int i = n; i >= 1; i--)
        {
            int pos = ST.query(0, n, 1, ins[i].first);
            seq[pos] = ins[i].second;
            ST.update(0, n, 1, pos, 0);
        }
        for(int i = 1; i <= n; i++)
            printf("%d%c", seq[i], i == n ? '\n' : ' ');
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值