POJ 3667 Hotel

线段树,区间合并模板题。仔细研究notonlysuccess的模板,学习区间合并,其实就是在左边,在右边,还是在中间。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
///LOOP
#define REP(i, n) for(int i = 0; i < n; i++)
#define FF(i, a, b) for(int i = a; i < b; i++)
#define FFF(i, a, b) for(int i = a; i <= b; i++)
#define FD(i, a, b) for(int i = a - 1; i >= b; i--)
#define FDD(i, a, b) for(int i = a; i >= b; i--)
///INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RFI(n) scanf("%lf", &n)
#define RFII(n, m) scanf("%lf%lf", &n, &m)
#define RFIII(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define RFIV(n, m, k, p) scanf("%lf%lf%lf%lf", &n, &m, &k, &p)
#define RS(s) scanf("%s", s)
///OUTPUT
#define PN printf("\n")
#define PI(n) printf("%d\n", n)
#define PIS(n) printf("%d ", n)
#define PS(s) printf("%s\n", s)
#define PSS(s) printf("%s ", n)
#define PC(n) printf("Case %d: ", n)
///OTHER
#define PB(x) push_back(x)
#define CLR(a, b) memset(a, b, sizeof(a))
#define CPY(a, b) memcpy(a, b, sizeof(b))
#define display(A, n, m) {REP(i, n){REP(j, m)PIS(A[i][j]);PN;}}
#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;
const int MOD = 9901;
const int INFI = 1e9 * 2;
const LL LINFI = 1e17;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int N = 55555;
const int M = 22;
const int move[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1};

int ls[N << 2], rs[N << 2], ms[N << 2], col[N << 2];

void pushup(int rt, int m)
{
    ls[rt] = ls[rt << 1];
    rs[rt] = rs[rt << 1 | 1];
    if(ls[rt] == (m - (m >> 1)))ls[rt] += ls[rt << 1 | 1];
    if(rs[rt] == (m >> 1))rs[rt] += rs[rt << 1];
    ms[rt] = max(ls[rt << 1 | 1] + rs[rt << 1], max(ms[rt << 1], ms[rt << 1 | 1]));
}

void pushdown(int rt, int m)
{
    if(col[rt] != -1)
    {
        col[rt << 1] = col[rt << 1 | 1] = col[rt];
        ms[rt << 1] = ls[rt << 1] = rs[rt << 1] = col[rt] ? m - (m >> 1) : 0;
        ms[rt << 1 | 1] = ls[rt << 1 | 1] = rs[rt << 1 | 1] = col[rt] ? (m >> 1) : 0;
        col[rt] = -1;
    }
}

void build(int l, int r, int rt)
{
    col[rt] = -1;
    ls[rt] = rs[rt] = ms[rt] = r - l + 1;
    if(l == r)return ;
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
}

void update(int L, int R, int c, int l, int r, int rt)
{
    if(L <= l && r <= R)
    {
        col[rt] = c;
        ms[rt] = ls[rt] = rs[rt] = c ? r - l + 1 : 0;
        return ;
    }
    pushdown(rt, r - l + 1);
    int m = (l + r) >> 1;
    if(L <= m)update(L, R, c, lson);
    if(R > m)update(L, R, c, rson);
    pushup(rt, r - l + 1);
}

int query(int c, int l, int r, int rt)
{
    if(l == r)return l;
    pushdown(rt, r - l + 1);
    int m = (l + r) >> 1;
    if(ms[rt << 1] >= c) return query(c, lson);
    else if(ls[rt << 1 | 1] + rs[rt << 1] >= c)return m - rs[rt << 1] + 1;
    else return query(c, rson);
}

int main()
{
    //freopen("input.txt", "r", stdin);

    int a, b, c, n, m;
    while(RII(n, m) != EOF)
    {
        build(1, n, 1);
        REP(i, m)
        {
            RI(a);
            if(a == 1)
            {
                RI(c);
                if(ms[1] < c)
                {
                    puts("0");
                    continue;
                }
                b = query(c, 1, n, 1);
                PI(b);
                update(b, b + c - 1, 0, 1, n, 1);
            }
            else
            {
                RII(b, c);
                update(b, b + c - 1, 1, 1, n, 1);
            }
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值