POJ.3667 Hotel ( 线段树 )

POJ.3667 Hotel ( 线段树 )

题意分析

还需要好好理解一下

代码总览

#include <cstdio>
#include <cstring>
#include <algorithm>
#define nmax 200010
using namespace std;
struct Tree{
    int l,r;
    int lenmax;
    int llen,rlen;
    int lazy;
    int mid(){
        return (l+r)>>1;
    }
    int interval_len(){
        return (r-l+1);
    }
    void update_len(){
        lenmax = llen = rlen = (lazy ?0:interval_len());
    }
};
Tree tree[nmax<<2];
int ans = 0;
void PushUp(int rt)
{
    int tmp = max(tree[rt<<1].lenmax,tree[rt<<1|1].lenmax);
    tree[rt].lenmax = max(tmp, (tree[rt<<1].rlen + tree[rt<<1|1].llen));
    tree[rt].llen = tree[rt<<1].llen;
    tree[rt].rlen = tree[rt<<1|1].rlen;
    if(tree[rt<<1].lenmax == tree[rt<<1].interval_len()){
        tree[rt].llen += tree[rt<<1|1].llen;
    }
    if(tree[rt<<1|1].lenmax == tree[rt<<1|1].interval_len()){
        tree[rt].rlen += tree[rt<<1].rlen;
    }
}
void PushDown(int rt)
{
    tree[rt<<1].lazy = tree[rt<<1|1].lazy = tree[rt].lazy;
    tree[rt].lazy = -1;
    tree[rt<<1].update_len();
    tree[rt<<1|1].update_len();
}
void Build(int l, int r, int rt)
{
    tree[rt].l = l; tree[rt].r = r;
    tree[rt].lenmax = tree[rt].llen = tree[rt].rlen = tree[rt].interval_len();
    tree[rt].lazy = 0;
    if(l == r){
        return;
    }
    Build(l,tree[rt].mid(),rt<<1);
    Build(tree[rt].mid()+1,r,rt<<1|1);
    //PushUp(rt);
}
void UpdateInterval(int val, int l, int r, int rt)
{
    //if(tree[rt].l >r || tree[rt].r < l) return;
    if(tree[rt].l == l && tree[rt].r == r){
        tree[rt].lazy = val;
        tree[rt].update_len();
        return;
    }
    if(tree[rt].lazy != -1) PushDown(rt);
    if(l>tree[rt].mid()){
        UpdateInterval(val,l,r,rt<<1|1);
    }else if(r <=tree[rt].mid()){
        UpdateInterval(val,l,r,rt<<1);
    }else{
        UpdateInterval(val,l,tree[rt].mid(),rt<<1);
        UpdateInterval(val,tree[rt].mid() +1,r,rt<<1|1);
    }
    PushUp(rt);
}
int Query(int val ,int rt)
{
    if(tree[rt].l == tree[rt].r && val == 1)
        return tree[rt].l;
    if(tree[rt].lazy != -1) PushDown(rt);

    if(tree[rt<<1].lenmax >= val){
        return Query(val,rt<<1);
    }else if(tree[rt<<1].rlen + tree[rt<<1|1].llen >= val){
        return tree[rt<<1].r - tree[rt<<1].rlen + 1;
    }else if(tree[rt<<1|1].lenmax >= val){
        return Query(val,rt<<1|1);
    }else{
        return 0;
    }
}

int main()
{
    //freopen("in3667.txt","r",stdin);
    int n,m,w,index;
    scanf("%d%d",&n,&m);
    Build(1,n,1);
    while(m--){
        int op;
        scanf("%d",&op);
        if(op == 1){
            scanf("%d",&w);
            index = Query(w,1);
            printf("%d\n",index);
            if(index) UpdateInterval(1,index,index+w-1,1);
        }else{
            int l,len;
            scanf("%d%d",&l,&len);
            UpdateInterval(0,l,l+len-1,1);
        }
    }
    return 0;
}
posted @ 2017-08-14 11:49  pengwill  阅读(92)  评论(0编辑  收藏  举报