HDU 4553 约会安排 (线段树 -- 区间合并(多种优先级的区间合并) )

27 篇文章 0 订阅

题意:

题意不说了= =~毕竟中文题。

思路:

求长度为len 的最左位置,显然是一个 区间合并的线段树问题。

但是有两个优先级, 一个是屌丝,一个是女神。

所以 直接开两个状态在结点中记录。

第一个状态 记录的是    一个区间中  什么都没有的  左边最大连续长度l1, 右边最大连续长度r1, 和最大连续长度m1.

第二个状态记录的是  一个区间中  无视屌丝的  左边最大连续长度l2, 右边最大连续长度r2, 和最大连续长度m2.

然后直接区间合并后。


对于屌丝操作。

直接看能不能用第一个状态更新。

对于女神操作:

先看能不能用第一个状态更新。

在看能不能用第二个状态更新。


对于学习操作:

直接把女神标记 和 屌丝标记全都初始化即可。(学习操作 也要pushdown, 我想当然的以为改为结点直接pushup就行了,其实他有可能改变非路径上的结点, wa了一次)


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;


int T, ks;
int n,q;
char cmd[20];

const int maxn = 100000 + 10;
struct node{
    int l1,r1,m1; /// space
    int l2,r2,m2; /// DS
    int l,r,len; /// [l,r];
}nod[maxn<<2];


void pushdown(int o){
    int l = nod[o].l;
    int r = nod[o].r;
    int lson = o << 1;
    int rson = o << 1 | 1;
    if (nod[o].m1 == 0){
        nod[lson].l1 = nod[lson].r1 = nod[lson].m1 = 0;
        nod[rson].l1 = nod[rson].r1 = nod[rson].m1 = 0;
    }
    else if (nod[o].m1 == nod[o].len){
        nod[lson].l1 = nod[lson].r1 = nod[lson].m1 = nod[lson].len;
        nod[rson].l1 = nod[rson].r1 = nod[rson].m1 = nod[rson].len;
    }


    if (nod[o].m2 == 0){
        nod[lson].l2 = nod[lson].r2 = nod[lson].m2 = 0;
        nod[rson].l2 = nod[rson].r2 = nod[rson].m2 = 0;
    }
    else if (nod[o].m2 == nod[o].len){
        nod[lson].l2 = nod[lson].r2 = nod[lson].m2 = nod[lson].len;
        nod[rson].l2 = nod[rson].r2 = nod[rson].m2 = nod[rson].len;
    }
}


void pushup(int o){
    int lson = o<<1;
    int rson = o<<1|1;
    nod[o].l1 = nod[lson].l1;
    if (nod[lson].l1 == nod[lson].len){
        nod[o].l1 += nod[rson].l1;
    }

    nod[o].r1 = nod[rson].r1;
    if (nod[rson].r1 == nod[rson].len){
        nod[o].r1 += nod[lson].r1;
    }

    nod[o].m1 = max(nod[o].l1, nod[o].r1);
    nod[o].m1 = max(nod[o].m1, nod[lson].m1);
    nod[o].m1 = max(nod[o].m1, nod[rson].m1);
    nod[o].m1 = max(nod[o].m1, nod[lson].r1 + nod[rson].l1);

///================================

    nod[o].l2 = nod[lson].l2;
    if (nod[lson].l2 == nod[lson].len){
        nod[o].l2 += nod[rson].l2;
    }

    nod[o].r2 = nod[rson].r2;
    if (nod[rson].r2 == nod[rson].len){
        nod[o].r2 += nod[lson].r2;
    }

    nod[o].m2 = max(nod[o].l2, nod[o].r2);
    nod[o].m2 = max(nod[o].m2, nod[lson].m2);
    nod[o].m2 = max(nod[o].m2, nod[rson].m2);
    nod[o].m2 = max(nod[o].m2, nod[lson].r2 + nod[rson].l2);
}
void update_study(int L,int R,int l,int r,int o){
    if (L <= l && r <= R){
        nod[o].l1 = nod[o].r1 = nod[o].m1 = r - l + 1;
        nod[o].l2 = nod[o].r2 = nod[o].m2 = r - l + 1;
        return;
    }
    pushdown(o);
    int m = l + r >> 1;
    if (m >= L){
        update_study(L,R,l,m,o<<1);
    }
    if (m < R){
        update_study(L,R, m+1, r, o << 1 | 1);
    }
    pushup(o);
}


void build(int l,int r,int o){
    nod[o].l = l;
    nod[o].r = r;
    nod[o].len = r - l + 1;
    if (l == r){
        nod[o].l1 = nod[o].r1 = nod[o].m1 = 1;
        nod[o].l2 = nod[o].r2 = nod[o].m2 = 1;
        return ;
    }

    int m = l + r >> 1;
    build(l,m,o<<1);
    build(m+1,r,o<<1|1);
    pushup(o);
}


void update_fillspace(int L,int R,int l,int r,int o,int ns){
    if (L <= l && r <= R){
        nod[o].l1 = nod[o].r1 = nod[o].m1 = 0;
        if (ns) {
            nod[o].l2 = nod[o].r2 = nod[o].m2 = 0;
        }
        return ;
    }
    int m = l + r >> 1;
    pushdown(o);
    if (m >= L){
        update_fillspace(L,R,l,m,o<<1,ns);
    }
    if (m < R){
        update_fillspace(L,R,m+1,r,o<<1|1,ns);
    }
    pushup(o);
}

void update_fillds(int L,int R,int l,int r,int o){
    if (L <= l && r <= R){
        nod[o].l2 = nod[o].r2 = nod[o].m2 = 0;
        nod[o].l1 = nod[o].r1 = nod[o].m1 = 0;
        return ;
    }

    int m = l + r >> 1;
    pushdown(o);
    if (m >= L){
        update_fillds(L,R, l,m,o<<1);
    }
    if (m < R){
        update_fillds(L,R,m+1,r,o<<1|1);
    }
    pushup(o);
}


int query_space(int len,int l,int r,int o){
    if (l == r) {
        return l;
    }

    int m = l + r >> 1;
    int lson = o << 1;
    int rson = o << 1 | 1;
    pushdown(o);
    if (nod[lson].m1 >= len){
        return query_space(len, l, m, lson);
    }
    else if (nod[lson].r1 + nod[rson].l1 >= len){
        return m - nod[lson].r1 + 1;
    }
    else {
        return query_space(len, m+1, r, rson);
    }
    pushup(o);
}


int query_ds(int len,int l,int r,int o){
    if (l == r) {
        return l;
    }
    int m = l + r >> 1;
    int lson = o << 1;
    int rson = o << 1 | 1;
    pushdown(o);
    if (nod[lson].m2 >= len){
        return query_ds(len, l, m, lson);
    }
    else if (nod[lson].r2 + nod[rson].l2 >= len){
        return m - nod[lson].r2 + 1;
    }
    else {
        return query_ds(len, m+1, r, rson);
    }
    pushup(o);

}

int main(){
    scanf("%d",&T);

    while(T--){
        scanf("%d %d",&n, &q);
        build(1,n,1);
        int x,y;
        printf("Case %d:\n",++ks);
        while(q--){
            scanf("%s%d", cmd,&x);
            if (cmd[0] == 'S'){
                scanf("%d",&y);
                update_study(x,y,1,n,1);
                printf("I am the hope of chinese chengxuyuan!!\n");
            }
            else if (cmd[0] == 'D'){
                if (nod[1].m1 < x){
                    printf("fly with yourself\n");
                }
                else {
//                    printf("m1 = %d\n", nod[1].m1);
                    int pos = query_space(x,1,n,1);
                    printf("%d,let's fly\n", pos);
                    update_fillspace(pos, pos+x-1, 1, n, 1,0);
                }
            }

            else if (cmd[0] == 'N'){
                if (nod[1].m1 >= x){
                    int pos = query_space(x,1,n,1);
                    printf("%d,don't put my gezi\n", pos);
                    update_fillspace(pos, pos+x-1, 1, n, 1,1);
                }
                else if (nod[1].m2 >= x){
                    int pos = query_ds(x,1,n,1);
//                    printf("m2 = %d\n", nod[1].m2);
                    printf("%d,don't put my gezi\n", pos);
                    update_fillds(pos, pos+x-1, 1, n, 1);
                }
                else {
                    printf("wait for me\n");
                }
            }
        }
    }
    return 0;
}





约会安排

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1627    Accepted Submission(s): 426


Problem Description
  寒假来了,又到了小明和女神们约会的季节。
  小明虽为屌丝级码农,但非常活跃,女神们常常在小明网上的大段发言后热情回复“呵呵”,所以,小明的最爱就是和女神们约会。与此同时,也有很多基友找他开黑,由于数量实在过于巨大,怎么安排时间便成了小明的一大心事。
  我们已知小明一共有T的空闲时间,期间会有很多女神或者基友来找小明。
  作为一个操作系统曾经怒考71分的大神,小明想到了一个算法,即“首次适应算法”,根据操作系统课本的描述,就是找一段最靠前的符合要求的连续空间分配给每个请求,由此小明做出了一个决定:
  当一个基友来找小明时,小明就根据“首次适应算法”来找一段空闲的时间来和基友约好,如果找到,就说“X,let’s fly”(此处,X为开始时间),否则就说“fly with yourself”;
  当女神来找小明时,先使用一次“首次适应算法”,如果没有找到,小明就冒着木叽叽的风险无视所有屌丝基友的约定,再次使用“无视基友首次适应算法”,两次只要有一次找到,就说“X,don’t put my gezi”(此处,X为开始时间),否则就说“wait for me”
  当然,我们知道小明不是一个节操负无穷的人,如果和女神约会完,还有剩余时间,他还是会和原来约好的基友去dota的。(举个例子:小西(屌丝)和小明约好在1~5这个时间单位段内打dota,这时候,女神来和小明预约长度为3的时间段,那么最终就是1~3小明去和女神约会,搞定后在4~5和小西打dota)
  小明偶尔也会想要学习新知识,此时小明就会把某一个时间区间的所有已经预定的时间全部清空用来学习并且怒吼“I am the hope of chinese chengxuyuan!!”,不过小明一般都是三分钟热度,再有人来预定的话,小明就会按耐不住寂寞把学习新知识的时间分配出去。
 

Input
输入第一行为CASE,表示有CASE组测试数据;
每组数据以两个整数T,N开始,T代表总共的时间,N表示预约请求的个数;
接着的N行,每行表示一个女神或者基友的预约,“NS QT”代表一个女神来找小明约一段长为QT的时间,“DS QT”则代表一个屌丝的长为QT的请求,当然也有可能是小明想学知识了,“STUDY!! L R”代表清空L~R区间内的所有请求。

[Technical Specification]
1. 1 <= CASE <= 30
2. 1 <= T, N <= 100000
3. 1 <= QT <= 110000
4. 1 <= L <= R <=T
 

Output
对于每一个case,第一行先输出“Case C:”代表是第几个case,然后N行,每行对应一个请求的结果(参照描述)。
输出样本(可复制此处):
“X,let's fly”,”fly with yourself”,”X,don't put my gezi”,”wait for me”,”I am the hope of chinese chengxuyuan!!”
 

Sample Input
  
  
1 5 6 DS 3 NS 2 NS 4 STUDY!! 1 5 DS 4 NS 2
 

Sample Output
  
  
Case 1: 1,let's fly 4,don't put my gezi wait for me I am the hope of chinese chengxuyuan!! 1,let's fly 1,don't put my gezi
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6032  6031  6030  6029  6028 
 

Statistic |  Submit |  Discuss |  Note

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值