poj 3667 线段树区间合并

题意:

n个房间,有如下两种操作:

1 d:有d个人要入住,并且入住的要是连续的房间;打印输出连续房间的第一个房间,若住不进去,输出0;

2 x d:从x开始有d个人退房。


解析:

区间合并的模板往上套了。

具体细节还是想不明白,先放着吧。

来自:

http://www.cnblogs.com/yewei/archive/2012/05/05/2484471.html


代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1

using namespace std;
const int maxn = 50000 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);

///sum保存的是还有多少房间可住
int sum[maxn << 2];
int lsum[maxn << 2], rsum[maxn << 2];
///-1 无延迟 1 住进  0 搬出
int cover[maxn << 2];

void pushup(int rt, int d)
{
    lsum[rt] = lsum[rt << 1];
    rsum[rt] = rsum[rt << 1 | 1];

    if (lsum[rt] == d - (d >> 1))
        lsum[rt] += lsum[rt << 1 | 1];
    if (rsum[rt] == d >> 1)
        rsum[rt] += rsum[rt << 1];

    sum[rt] = max(rsum[rt << 1] + lsum[rt << 1 | 1], max(sum[rt << 1], sum[rt << 1 | 1]));
}

void pushdown(int rt, int d)
{
    if (cover[rt] != -1)
    {
        cover[rt << 1] = cover[rt << 1 | 1] = cover[rt];
        lsum[rt << 1] = rsum[rt << 1] = sum[rt << 1] = cover[rt] ? 0 : (d - (d >> 1));
        lsum[rt << 1 | 1] = rsum[rt << 1 | 1] = sum[rt << 1 | 1] = cover[rt] ? 0 : (d >> 1);
        cover[rt] = -1;
    }
}

void build(int lo, int hi, int rt)
{
    sum[rt] = lsum[rt] = rsum[rt] = hi - lo + 1;
    cover[rt] = -1;
    if (lo == hi)
        return;
    int mi = (lo + hi) >> 1;
    build(lson);
    build(rson);
    pushup(rt, hi - lo + 1);
}

void update(int L, int H, int num, int lo, int hi, int rt)
{
    if (L <= lo && hi <= H)
    {
        lsum[rt] = rsum[rt] = sum[rt] = num ? 0 : hi - lo + 1;
        cover[rt] = num;
        return;
    }
    pushdown(rt, hi - lo + 1);
    int mi = (lo + hi) >> 1;
    if (L <= mi)
        update(L, H, num, lson);
    if (mi < H)
        update(L, H, num, rson);
    pushup(rt, hi - lo + 1);
}

int query(int w, int lo, int hi, int rt)
{
    if (lo == hi)
        return 1;
    pushdown(rt, hi - lo + 1);
    int mi = (lo + hi) >> 1;
    if (w <= sum[rt << 1])
        return query(w, lson);
    else if (w <= lsum[rt << 1 | 1] + rsum[rt << 1])
        return mi - rsum[rt << 1] + 1;
    return query(w, rson);
}

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAL
    int n, m;
    while (~scanf("%d%d", &n, &m))
    {
        build(1, n, 1);
        while (m--)
        {
            int op;
            scanf("%d", &op);
            if (op == 1)
            {
                int d;
                scanf("%d", &d);
                if (sum[1] < d)
                    printf("0\n");
                else
                {
                    int x = query(d, 1, n, 1);
                    printf("%d\n", x);
                    update(x, x + d - 1, 1, 1, n, 1);
                }
            }
            if (op == 2)
            {
                int x, d;
                scanf("%d%d", &x, &d);
                update(x, x + d - 1, 0, 1, n, 1);
            }
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值