【POJ】3667-Hotel(线段树的区间合并)

标记线段树的时候利用 lsum rsum msum 记录最左边 zui右边 以及整个区间的最大连续空位的值

维护的时候注意合并

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson (pos<<1)
#define rson (pos<<1|1)
const int maxn = 55555;
int n,m;
struct Node{
    int l,r,lsum,rsum,msum,col;
    int len(){
        return r - l + 1;
    }
    int mid(){
        return (l + r) >> 1;
    }
}node[maxn << 2];
void build(int l,int r,int pos){
    node[pos].l = l; node[pos].r = r;
    node[pos].col = -1;
    node[pos].lsum = node[pos].rsum = node[pos].msum = node[pos].len();
    if(l == r) return;
    int mid = node[pos].mid();
    build(l,mid,lson);
    build(mid + 1,r,rson);
}
void pushdown(int pos){
    if(node[pos].col != -1){
        if(!node[pos].col){
            node[lson].lsum = node[lson].rsum = node[lson].msum = 0;
            node[rson].lsum = node[rson].rsum = node[rson].msum = 0;
            node[lson].col  = node[rson].col = 0;
        }
        else{
            node[lson].lsum = node[lson].rsum = node[lson].msum = node[lson].len();
            node[rson].lsum = node[rson].rsum = node[rson].msum = node[rson].len();
            node[lson].col  = node[rson].col = 1;
        }
        node[pos].col = -1;
    }
}
void pushup(int pos){
    node[pos].lsum = node[lson].lsum;
    node[pos].rsum = node[rson].rsum;
    if(node[lson].lsum == node[lson].len()) node[pos].lsum += node[rson].lsum;
    if(node[rson].rsum == node[rson].len()) node[pos].rsum += node[lson].rsum;
    node[pos].msum = node[lson].rsum + node[rson].lsum;
    node[pos].msum = max(node[pos].msum,max(node[lson].msum,node[rson].msum));
}
void update(int l,int r,int pos,int d){
    if(l <= node[pos].l && node[pos].r <= r){
        if(d == 1){
            node[pos].lsum = node[pos].rsum = node[pos].msum = node[pos].len();
            node[pos].col = 1;
        }
        if(d == 0){
            node[pos].lsum = node[pos].rsum = node[pos].msum = 0;
            node[pos].col = 0;
        }
        return;
    }
    pushdown(pos);
    int mid = node[pos].mid();
    if(l <= mid)
        update(l,r,lson,d);
    if(r  > mid)
        update(l,r,rson,d);
    pushup(pos);
}
int query(int l,int r,int pos,int v){
    if(l == r) return l;
    pushdown(pos);
    if(node[lson].msum >= v)
        return query(l,r,lson,v);
    else if(node[lson].rsum + node[rson].lsum >= v)
        return node[pos].mid() - node[lson].rsum + 1;
    return query(l,r,rson,v);
}
int main(){
    while(scanf("%d%d",&n,&m) != EOF){
        build(1,n,1);
        for(int i = 0; i < m; i++){
            int op,l,r;
            scanf("%d",&op);
            if(op == 1){
                scanf("%d",&l);
                if(node[1].msum < l)
                    printf("0\n");
                else{
                    int v = query(1,n,1,l);
                    printf("%d\n",v);
                    update(v,v + l - 1,1,0);
                    //printf("%d\n",node[1].msum);
                }
            }
            else{
                scanf("%d%d",&l,&r);
                update(l,l + r - 1,1,1);
            }
        }
    }
    return 0;
}
时间: 2024-10-15 07:01:00

【POJ】3667-Hotel(线段树的区间合并)的相关文章

POJ 3667 Hotel(线段树区间合并)

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 Stree

POJ 3667 Hotel ( 线段树区间合并 )

题目链接~~> 做题感悟:这题是接触线段树区间合并的第一题,做的很纠结. 解题思路: 注意线段树上节点代表的信息 : 每个节点需要维护 lc , rc , mc ,add ,见下图: add 为懒惰标记.假设 i 代表父亲节点编号,左儿子为  i * 2  ,右儿子为 i * 2  + 1 ,那么我们可以得到 : T [ i ] .lc 首先加上左儿子的左边的空格数,然后需要判断一下,如果左儿子的左节点占满了整个左区间时需要再加上右儿子的左边的空格数.同理 T [ i ] .rc 也可以这样得到

POJ 3667 Hotel 线段树 区间合并

题意: 1 输入a:询问是不是有连续长度为a的空房间,有的话住进最左边 2 输入a b:将[a,a+b-1]的房间清空 思路:记录区间中最长的空房间 线段树操作: update:区间替换 query:询问满足条件的最左端点 #include <cstdio> #include <iostream> #include <algorithm> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1

poj 3667 Hotel - 线段树

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 v

POJ 3667(线段树区间合并)

http://poj.org/problem?id=3667 题意:两个操作 : 1 选出靠左的长度为a的区间. 2 把从 a到a+b的区间清空. 线段树区间合并+lazy // by caonima // hehe #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cmath> using namespace std; co

POJ 3667 线段树的区间合并简单问题

题目大意:有一排标号1-N的房间.操作一:询问是不是有连续长度为a的空房间,有的话住进最左边(占用a个房间)操作二:将[a,a+b-1]的房间清空(腾出b个房间)思路:记录每个区间中“靠左”“靠右”“中间”的空房间线段树操作:update:区间替换query:询问满足条件的最左端点 题目链接: http://vjudge.net/problem/viewProblem.action?id=10354 题目操作: 因为这里找从最左边住起的房间,所以这里不能像常规地写query函数那样写了,终于发现

HDU 1540 &amp;&amp; POJ 2892 Tunnel Warfare (线段树,区间合并).

~~~~ 第一次遇到线段树合并的题,又被律爷教做人.TAT. ~~~~ 线段树的题意都很好理解吧.. 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1540 http://poj.org/problem?id=2892 ~~~~ 我的代码:200ms #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #defin

HDU 3308 线段树(区间合并)

LCIS Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3896    Accepted Submission(s): 1766 Problem Description Given n integers.You have two operations:U A B: replace the Ath number by B. (index

Codeforces Round #222 (Div. 1) D. Developing Game 线段树有效区间合并

D. Developing Game Pavel is going to make a game of his dream. However, he knows that he can't make it on his own so he founded a development company and hired n workers of staff. Now he wants to pick n workers from the staff who will be directly res