[线段树]poj3667 Hotel(区间合并、更新、延迟/懒惰标记

来源:互联网 发布:合肥工业大学真空知乎 编辑:程序博客网 时间:2024/04/27 15:31

Hotel
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 16089 Accepted: 6998

Description

The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever the competent travel agent, has named the Bullmoose Hotel on famed Cumberland Street as their vacation residence. This immense hotel has N (1 ≤ N ≤ 50,000) rooms all located on the same side of an extremely long hallway (all the better to see the lake, of course).

The cows and other visitors arrive in groups of size Di (1 ≤ Di ≤ N) and approach the front desk to check in. Each group i requests a set of Di contiguous rooms from Canmuu, the moose staffing the counter. He assigns them some set of consecutive room numbers r..r+Di-1 if they are available or, if no contiguous set of rooms is available, politely suggests alternate lodging. Canmuu always chooses the value of rto be the smallest possible.

Visitors also depart the hotel from groups of contiguous rooms. Checkout i has the parameters Xi and Di which specify the vacating of rooms Xi ..Xi +Di-1 (1 ≤ Xi ≤ N-Di+1). Some (or all) of those rooms might be empty before the checkout.

Your job is to assist Canmuu by processing M (1 ≤ M < 50,000) checkin/checkout requests. The hotel is initially unoccupied.

Input

* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Line i+1 contains request expressed as one of two possible formats: (a) Two space separated integers representing a check-in request: 1 and D(b) Three space-separated integers representing a check-out: 2, Xi, and Di

Output

* Lines 1.....: For each check-in request, output a single line with a single integer r, the first room in the contiguous sequence of rooms to be occupied. If the request cannot be satisfied, output 0.

Sample Input

10 61 31 31 31 32 5 51 6

Sample Output

14705

Source

USACO 2008 February Gold

题意:


思路:

区间合并模板,思路都在代码里

代码:

#include <iostream>#include <stdio.h>#include <string.h>#define inf 1<<29using namespace std;const int N = 5e4+10;struct{    int l, r;    int mark;//-1未标记,0入住,1退宿(之所以-1、1不合成一个是因为有延迟/懒惰标记    int lsum, rsum, msum;//左区间(下界为起点的连续区间),右区间(上界为终点的连续区间),最大连续区间}Tree[N*4];//建树void build(int root, int left, int right){    Tree[root].mark = -1;    Tree[root].l = left;    Tree[root].r = right;    Tree[root].msum = Tree[root].lsum = Tree[root].rsum = right - left + 1;    if(left == right)        return;    int mid = (left + right) >> 1;    build(root<<1, left, mid);    build(root<<1|1, mid+1, right);}//延迟标记向下传递,维护root节点之下区间的准确性void pushdown(int root){    int k = Tree[root].mark;    if(k != -1)    {        int mid = (Tree[root].l + Tree[root].r) >> 1;        Tree[root<<1].mark = Tree[root<<1|1].mark = k;        Tree[root<<1].msum = Tree[root<<1].lsum = Tree[root<<1].rsum = k ? (mid-Tree[root].l+1) : 0;        Tree[root<<1|1].msum = Tree[root<<1|1].lsum = Tree[root<<1|1].rsum = k ? (Tree[root].r-mid) : 0;        Tree[root].mark = -1;    }}int query(int root, int len){    if(Tree[root].l == Tree[root].r)        return Tree[root].l;    //向下维护,延迟/懒惰标记传递    pushdown(root);    int mid = (Tree[root].l + Tree[root].r) >> 1;    //左子节点的最大连续区间    if(Tree[root<<1].msum >= len)        return query(root<<1, len);    //左子节点的右区间+右子节点的左区间    else if(Tree[root<<1].rsum + Tree[root<<1|1].lsum >= len)        return mid - Tree[root<<1].rsum + 1;    return query(root<<1|1, len);}//延迟标记向上更新,维护root节点之上区间的准确性void pushup(int root){    int mid = (Tree[root].l + Tree[root].r) >> 1;    //当前节点的左区间一定包含左节点的左区间    Tree[root].lsum = Tree[root<<1].lsum;    //当前节点的右区间一定包含右节点的右区间    Tree[root].rsum = Tree[root<<1|1].rsum;    if(Tree[root<<1].lsum == mid-Tree[root].l+1)    //当前节点的左区间等于左节点的最大连续区间,也包含右节点的左区间        Tree[root].lsum += Tree[root<<1|1].lsum;    if(Tree[root<<1|1].rsum == Tree[root].r-mid)    //当前节点的右区间等于右节点的最大连续区间,也包含左节点的右区间        Tree[root].rsum += Tree[root<<1].rsum;    //当前节点的最大连续区间 = max{左节点的最大连续区间,右节点的最大连续区间,左节点右区间+右节点左区间};    int t = max(Tree[root<<1].msum, Tree[root<<1|1].msum);    Tree[root].msum = max(Tree[root<<1].rsum+Tree[root<<1|1].lsum, t);}void update(int root, int l, int r, int value){    if(l > Tree[root].r || r < Tree[root].l)        return;    if(l <= Tree[root].l && r >= Tree[root].r)    {        Tree[root].mark = value;        Tree[root].msum = Tree[root].lsum = Tree[root].rsum = value ? (Tree[root].r-Tree[root].l+1) : 0;        return;    }    //延迟标记向下传递    pushdown(root);    update(root<<1, l, r, value);    update(root<<1|1, l , r, value);    //向上恢复标记    pushup(root);}int main(){    int n, m;    int a, b, c;    scanf("%d%d", &n, &m);    build(1, 1, n);    while(m-->0)    {        scanf("%d", &a);        if(a == 1)//入住,区间查询+更新        {            scanf("%d", &b);            if(b > Tree[1].msum)                printf("0\n");            else            {                int t = query(1, b);                printf("%d\n", t);                update(1, t, t+b-1, 0);            }        }        else//退宿,区间更新        {            scanf("%d%d", &b, &c);            update(1, b, b+c-1, 1);        }    }    return 0;}/*10 61 31 31 31 32 5 51 6------14705*/


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 玩久了不想工作怎么办 我很懒不想工作怎么办 金寒水冷的八字怎么办 两岁宝宝内八字怎么办 小孩走路脚内八字怎么办 10岁走路内八字怎么办 8岁孩走路内八字怎么办 一岁宝宝足外翻怎么办 2岁宝宝小腿弯怎么办啊 一岁宝宝小腿弯怎么办 一岁小儿o型腿怎么办 两岁宝宝o型腿怎么办 狗狗前腿外八字怎么办 20岁走路内八字怎么办 9岁儿童脚内八字怎么办 5岁宝宝脚内八字怎么办 一岁宝宝内八字怎么办 两人八字合不合怎么办 考到不好的大学怎么办 考的大学不理想怎么办 只考上二本大学怎么办 w7电脑中病毒了怎么办 电脑中病毒了该怎么办 泰迪呼吸急促怎么办啊 狗狗呼吸急促是怎么办 狗狗着凉了呕吐怎么办 狗鼻子流黄鼻涕怎么办 刚出生婴儿睡觉不踏实怎么办 有人溺水后你该怎么办 借钱不还怎么办没欠条 私人欠货款不还怎么办 公司欠货款不还怎么办 两个人离婚一方不同意怎么办 比亚迪l3油耗高怎么办 u盘密码忘记了怎么办 主板没有m.2接口怎么办 点痣留下了疤怎么办 危险三角区长痘痘怎么办 挤了危险三角区怎么办 三角区长痘挤了怎么办 三角区发红长痘怎么办