HDU4553-约会安排-区间合并

(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦

目录


题意:传送门

 原题目描述在最下面。
 中文题面。

思路:

 维护两颗线段树,分别处理屌丝和女神的时间安排。
l a z y = 0 表示子区间时间全部被安排了, l a z y = 1 表示子区间时间全部空闲,而可以安排。
 对于区间合并的题一般都有维持三个变量 l s u m , r s u m , s u m _ m a x 分别表示子区间左端点开始向右的可用时间长度,子区间右端点开始向左的可用时间长度,子区间最大可用时间长度。
p u s h _ u p p u s h _ d o w n 的细节请看代码,有注释。

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<assert.h>
#include<bitset>
#include<unordered_map>
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) (x)&(-(x))
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int N = (int)1e5 +107;
int n, m;
struct lp{
  int l, r, d;
  int lm,rm,mmax,lazy;
}ns[N<<2][2];
//o==0表示屌丝的线段树,o==1表示女神的线段树
inline void push_up(int o,int rt){//更新父节点
  ns[rt][o].lm = ns[lson][o].lm;//更新父亲左端点区间最长长度
  ns[rt][o].rm = ns[rson][o].rm;//更新右端点
  //父亲的最大长度,一定是左儿子最大长度,右儿子最大长度,和(左儿子rsum+右儿子lsum)这三者中的一个
  ns[rt][o].mmax = max(max(ns[lson][o].mmax,ns[rson][o].mmax), ns[lson][o].rm+ns[rson][o].lm);
  //如果左儿子是满的,就要把右儿子的lsum加给父亲
  if(ns[lson][o].lm==ns[lson][o].d){
    ns[rt][o].lm+=ns[rson][o].lm;
  }
  //rsum同理
  if(ns[rson][o].rm==ns[rson][o].d){
    ns[rt][o].rm+=ns[lson][o].rm;
  }
}
void build(int o,int l,int r,int rt){
  int mid = (l + r)>>1;
  ns[rt][o].l=l;ns[rt][o].r=r;
  ns[rt][o].d=r-l+1;//区间长度
  ns[rt][o].lazy = -1;
  ns[rt][o].lm=ns[rt][o].rm=ns[rt][o].mmax=r-l+1;
  if(l==r){
    return;
  }
  build(o,l,mid,lson);build(o,mid+1,r,rson);
  //push_up(o,rt);
}
void push_down(int o,int rt){//把信息传给儿子节点
  if(ns[rt][o].lazy!=-1){
    if(ns[rt][o].lazy==0){//lazy==0清空区间
      ns[lson][o].lazy=ns[rson][o].lazy=0;
      ns[lson][o].lm=ns[lson][o].rm=ns[lson][o].mmax=0;
      ns[rson][o].lm=ns[rson][o].rm=ns[rson][o].mmax=0;
    }else{//全部可以安排
      ns[lson][o].lazy=ns[rson][o].lazy=1;
      ns[lson][o].lm=ns[lson][o].rm=ns[lson][o].mmax=ns[lson][o].d;
      ns[rson][o].lm=ns[rson][o].rm=ns[rson][o].mmax=ns[rson][o].d;
    }
    ns[rt][o].lazy=-1;
  }
}
void update(int o,int L,int R,int c,int rt){
  int l=ns[rt][o].l,r=ns[rt][o].r,mid=(l+r)>>1;
  if(L==l&&r==R){
    ns[rt][o].lazy=c;
    if(c)ns[rt][o].lm=ns[rt][o].rm=ns[rt][o].mmax=r-l+1;
    else ns[rt][o].lm=ns[rt][o].rm=ns[rt][o].mmax=0;
    return;
  }
  if(ns[rt][o].l==ns[rt][o].r)return;
  push_down(o,rt);
  if(L>mid)update(o,L,R,c,rson);
  else if(R<=mid)update(o,L,R,c,lson);
  else{
    update(o,L,mid,c,lson),update(o,mid+1,R,c,rson);
  }
  push_up(o,rt);
}
//query函数要返回第一个可行的时间点
int query(int o,int L,int R,int len,int rt){//len表示要安排的长度
  int l=ns[rt][o].l,r=ns[rt][o].r,mid=(l+r)>>1;
  if(l==r){
    return l;
  }
  if(ns[rt][o].l==ns[rt][o].r)return 0;
  push_down(o,rt);
  //如果左儿子的max_sum够用就去左儿子,如果左右儿子衔接处够用就返回衔接处,反之就去右儿子
  if(ns[lson][o].mmax>=len)return query(o,1,mid,len,lson);
  else if(ns[lson][o].rm+ns[rson][o].lm>=len)return mid-ns[lson][o].rm+1;
  else return query(o,mid+1,R,len,rson);
}
int main(){
  int tim, tc = 0;
  scanf("%d",&tim);
  while(tim--){
    scanf("%d%d",&n,&m);
    build(0,1,n,1);build(1,1,n,1);
    printf("Case %d:\n", ++tc);
    for(int i=0;i<m;++i){
      char s[10];
      int l,r,tmp;
      scanf("%s",s);
      if(s[0]=='D'){
        scanf("%d", &r);
        if(ns[1][0].mmax>=r){//如果屌丝最大空闲长度大于r可以安排
          tmp=query(0,1,n,r,1);
          printf("%d", tmp);
          printf(",let's fly\n"); 
          update(0,tmp,tmp+r-1,0,1);
        }else printf("fly with yourself\n");
      }else if(s[0]=='N'){
        scanf("%d", &r);//女神优先级比较高
        if(ns[1][0].mmax>=r){//先用屌丝的时间,不过女神的区间也要更新
          tmp=query(0,1,n,r,1);
          printf("%d", tmp);
          printf(",don't put my gezi\n");
          update(0,tmp,tmp+r-1,0,1);
          update(1,tmp,tmp+r-1,0,1);
        }else if(ns[1][1].mmax>=r){//屌丝不足,就安排女神的时间,二者都要更新
          tmp=query(1,1,n,r,1);
          printf("%d", tmp);
          printf(",don't put my gezi\n");
          update(0,tmp,tmp+r-1,0,1);
          update(1,tmp,tmp+r-1,0,1);
        }else printf("wait for me\n");
      }else{
        printf("I am the hope of chinese chengxuyuan!!\n");
        scanf("%d%d",&l,&r);
        update(0,l,r,1,1);
        update(1,l,r,1,1);
      }
    }
  }
  return 0;
}


原题目描述:

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
2013金山西山居创意游戏程序挑战赛——初赛(3)

Recommend

猜你喜欢

转载自blog.csdn.net/qq_39599067/article/details/81227706