【线段树区间更新+优先级】HDU - 4553 M - 约会安排

M - 约会安排  HDU - 4553

寒假来了,又到了小明和女神们约会的季节。 
  小明虽为屌丝级码农,但非常活跃,女神们常常在小明网上的大段发言后热情回复“呵呵”,所以,小明的最爱就是和女神们约会。与此同时,也有很多基友找他开黑,由于数量实在过于巨大,怎么安排时间便成了小明的一大心事。 
  我们已知小明一共有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
#include <bits/stdc++.h>
using namespace std;
const int maxn=110005;
char Q[10];
int n,m,x,y;
struct node
{
    int l,r,len;
    int dls,drs,das;
    int nls,nrs,nas;
}tree[maxn*4];

void build(int l,int r,int rt)  //初始化,最开始所有都是有空的
{
    tree[rt].l=l,tree[rt].r=r,tree[rt].len=r-l+1;
    tree[rt].dls=tree[rt].drs=tree[rt].das=tree[rt].len;
    tree[rt].nls=tree[rt].nrs=tree[rt].nas=tree[rt].len;
    if(l==r) return;
    int mid=(l+r)/2;
    build(l,mid,2*rt);
    build(mid+1,r,2*rt+1);
}

void pushup(int rt)
{
    if(tree[rt].l==tree[rt].r) return;
    tree[rt].dls=tree[rt*2].dls;        //要是父节点的左连续等于左节点的总长,那就代表左边全有,要加上右节点的左连续
    if(tree[rt*2].dls==tree[2*rt].len)
        tree[rt].dls+=tree[2*rt+1].dls;
    tree[rt].drs=tree[rt*2+1].drs;      //要是父节点的右连续等于右节点的总长,那就代表右边全有,要加上左节点的右连续
    if(tree[rt*2+1].drs==tree[rt*2+1].len)
        tree[rt].drs+=tree[2*rt].drs;

    tree[rt].nls=tree[rt*2].nls;
    if(tree[rt*2].nls==tree[2*rt].len)
        tree[rt].nls+=tree[2*rt+1].nls;
    tree[rt].nrs=tree[rt*2+1].nrs;
    if(tree[rt*2+1].nrs==tree[rt*2+1].len)
        tree[rt].nrs+=tree[2*rt].nrs;

    tree[rt].das=max(tree[2*rt].das,tree[2*rt+1].das);   //父节点的总和要么是左或右节点的总和,要么是自己的左或右最大连续
    tree[rt].das=max(tree[rt].das,tree[rt].dls);         //要么是左节点的右连续+右节点的左连续,选最大就行
    tree[rt].das=max(tree[rt].das,tree[rt].drs);
    tree[rt].das=max(tree[rt].das,tree[2*rt].drs+tree[2*rt+1].dls);

    tree[rt].nas=max(tree[2*rt].nas,tree[2*rt+1].nas);
    tree[rt].nas=max(tree[rt].nas,tree[rt].nls);
    tree[rt].nas=max(tree[rt].nas,tree[rt].nrs);
    tree[rt].nas=max(tree[rt].nas,tree[2*rt].nrs+tree[2*rt+1].nls);
}

void pushdown(int rt)
{
    if(tree[rt].das==0)  //要是占满了
    {
        tree[rt].dls=tree[rt].drs=tree[rt].das=0;
        tree[2*rt].dls=tree[2*rt].drs=tree[2*rt].das=0;
        tree[2*rt+1].dls=tree[2*rt+1].drs=tree[2*rt+1].das=0;
    }

    if(tree[rt].das==tree[rt].len)  //要是全为空
    {
        tree[rt].dls=tree[rt].drs=tree[rt].das=tree[rt].len;
        tree[2*rt].dls=tree[2*rt].drs=tree[2*rt].das=tree[2*rt].len;
        tree[2*rt+1].dls=tree[2*rt+1].drs=tree[2*rt+1].das=tree[2*rt+1].len;
    }

    if(tree[rt].nas==0)
    {
        tree[rt].nls=tree[rt].nrs=tree[rt].nas=0;
        tree[2*rt].nls=tree[2*rt].nrs=tree[2*rt].nas=0;
        tree[2*rt+1].nls=tree[2*rt+1].nrs=tree[2*rt+1].nas=0;
    }

    if(tree[rt].nas==tree[rt].len)
    {
        tree[rt].nls=tree[rt].nrs=tree[rt].nas=tree[rt].len;
        tree[2*rt].nls=tree[2*rt].nrs=tree[2*rt].nas=tree[2*rt].len;
        tree[2*rt+1].nls=tree[2*rt+1].nrs=tree[2*rt+1].nas=tree[2*rt+1].len;
    }
}

int query_ds(int l,int r,int rt,int k)
{
    pushdown(rt);
    if(tree[rt].das<k) return 0;  //要是根本没有那么多时间,返回0
    if(tree[rt].dls>=k) return tree[rt].l; //要是左连续就足够了,直接把左端点给出
    int mid=(l+r)/2;
    if(tree[2*rt].das>=k) return query_ds(l,mid,2*rt,k);  //要是左节点足够,去查询左节点
    if(tree[2*rt].drs+tree[2*rt+1].dls>=k) return tree[2*rt].r-tree[2*rt].drs+1;  //要是是左节点和右节点交接
    return query_ds(mid+1,r,2*rt+1,k); //否则查询右节点
}

int query_ns(int l,int r,int rt,int k)
{
    pushdown(rt);
    if(tree[rt].nas<k) return 0;  //要是根本没有那么多时间,返回0
    if(tree[rt].nls>=k) return tree[rt].l; //要是左连续就足够了,直接把左端点给出
    int mid=(l+r)/2;
    if(tree[2*rt].nas>=k) return query_ns(l,mid,2*rt,k);  //要是左节点足够,去查询左节点
    if(tree[2*rt].nrs+tree[2*rt+1].nls>=k) return tree[2*rt].r-tree[2*rt].nrs+1;  //要是是左节点和右节点交接
    return query_ns(mid+1,r,2*rt+1,k); //否则查询右节点
}

void update(int L,int R,int l,int r,int rt,int flag)
{
    if(L<=l&&R>=r)
    {
        if(flag==0)  //屌丝的更新
        {
            tree[rt].das=tree[rt].dls=tree[rt].drs=0;
        }
        else if(flag==1)  //学习的更新
        {
            tree[rt].das=tree[rt].dls=tree[rt].drs=tree[rt].len;
            tree[rt].nas=tree[rt].nls=tree[rt].nrs=tree[rt].len;
        }
        else    //女神的更新
        {
            tree[rt].das=tree[rt].dls=tree[rt].drs=0;
            tree[rt].nas=tree[rt].nls=tree[rt].nrs=0;
        }
        return;
    }
    pushdown(rt);
    int mid=(r+l)/2;
    if(L<=mid) update(L,R,l,mid,2*rt,flag);
    if(R>mid) update(L,R,mid+1,r,2*rt+1,flag);
    pushup(rt);
}

int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        printf("Case %d:\n",cas++);
        build(1,n,1);
        for(int i=1;i<=m;i++)
        {
            scanf("%s",Q);
            if(Q[0]=='D')
            {
                scanf("%d",&x);
                int P=query_ds(1,n,1,x);
                if(P)
                {
                    printf("%d,let's fly\n",P);
                    update(P,P+x-1,1,n,1,0);
                }
                else
                    printf("fly with yourself\n");

            }
            else if(Q[0]=='N')
            {
                scanf("%d",&x);
                int P=query_ds(1,n,1,x);
                if(P)
                {
                    printf("%d,don't put my gezi\n",P);
                    update(P,P+x-1,1,n,1,2);
                    continue;
                }
                P=query_ns(1,n,1,x);
                if(P)
                {
                    printf("%d,don't put my gezi\n",P);
                    update(P,P+x-1,1,n,1,2);
                    continue;
                }
                else
                    printf("wait for me\n");
            }
            else
            {
                scanf("%d%d",&x,&y);
                update(x,y,1,n,1,1);
                printf("I am the hope of chinese chengxuyuan!!\n");
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41037114/article/details/81477870
今日推荐