约会安排HDU - 4553(线段树区间合并+优先级+最长连续1)

原题目中输出语句中夹杂有中文,我已修改
  寒假来了,又到了小明和女神们约会的季节。
  小明虽为屌丝级码农,但非常活跃,女神们常常在小明网上的大段发言后热情回复“呵呵”,所以,小明的最爱就是和女神们约会。与此同时,也有很多基友找他开黑,由于数量实在过于巨大,怎么安排时间便成了小明的一大心事。
  我们已知小明一共有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!!”,不过小明一般都是三分钟热度,再有人来预定的话,小明就会按耐不住寂寞把学习新知识的时间分配出去。


思路:如果做了hotel那题,这题就可以容易想出。同样是维护区间最长连续的1,不过这里开两棵树,一棵屌丝树,一棵女神树。根据实际对应的操作在两棵树上区间修改和查询。

代码上注意bug:输出的是英文引号。

我的查了5天的bug,由于测试的时候自己都是单组测试,没有发现Case %d:对应的输出变量写成了常量。感谢辛格的对拍(希望他不会打死我

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+1000;
typedef long long LL;
struct Tree{
	LL l,r,sum,tag,pre,suf;
}tree1[maxn*4],tree2[maxn*4];///第一个是屌丝,第二个是女神
LL rmax(LL A,LL B,LL C){return max(A,max(B,C));}
void push_up(Tree tree[],LL p){
	tree[p].pre=tree[p*2].pre;
	tree[p].suf=tree[p*2+1].suf;

	if(tree[p*2].pre==tree[p*2].r-tree[p*2].l+1) tree[p].pre+=tree[p*2+1].pre;
	if(tree[p*2+1].suf==tree[p*2+1].r-tree[p*2+1].l+1) tree[p].suf+=tree[p*2].suf;
	tree[p].sum=rmax(tree[p*2].sum,tree[p*2+1].sum,tree[p*2].suf+tree[p*2+1].pre);
}
void addtag(Tree tree[],LL p,LL d){
	tree[p].tag=d;
	tree[p].pre=tree[p].suf=tree[p].sum=d?(tree[p].r-tree[p].l+1):0;
}
void push_down(Tree tree[],LL p){
	if(tree[p].tag!=-1){
		addtag(tree,p*2,tree[p].tag);
		addtag(tree,p*2+1,tree[p].tag);
		tree[p].tag=-1;
	}
}
void build(Tree tree[],LL p,LL l,LL r)
{
	tree[p].l=l;tree[p].r=r;tree[p].sum=tree[p].pre=tree[p].suf=(r-l+1);
	tree[p].tag=-1;//表示没有修改
	if(l==r) return;
	LL mid=(l+r)>>1;
	build(tree,p*2,l,mid);
	build(tree,p*2+1,mid+1,r);
	push_up(tree,p);
}
void modify(Tree tree[],LL p,LL l,LL r,LL d){
	if(l<=tree[p].l&&r>=tree[p].r){
		addtag(tree,p,d);
		return;
	}
	push_down(tree,p);
	LL mid=(tree[p].l+tree[p].r)>>1;
	if(l<=mid) modify(tree,p*2,l,r,d);
	if(r>mid) modify(tree,p*2+1,l,r,d);
	push_up(tree,p);
}
LL query(Tree tree[],LL p,LL l,LL r,LL d){
	if(l==r) return l;
	push_down(tree,p);
	LL mid=(l+r)>>1;
		if(tree[p*2].sum>=d) return query(tree,p*2,l,mid,d);
		else if(tree[p*2].suf+tree[p*2+1].pre>=d) return mid-tree[p*2].suf+1;
        else return query(tree,p*2+1,mid+1,r,d);
}
int main(void)
{

  LL t;scanf("%lld",&t);
  for(LL T=1;T<=t;T++)
  {
  	 LL n,m;scanf("%lld%lld",&n,&m);
  	 build(tree1,1,1,n);
  	 build(tree2,1,1,n);
  	 printf("Case %lld:\n",T);///我写成小t输出....查了一星期代码
  	 while(m--){
  	 char str[35];
  	 scanf("%s",str);
  	 if(str[0]=='D'){
  	 	LL times;scanf("%lld",&times);
  	 	if(tree1[1].sum<times){
            printf("fly with yourself\n");
  	 	}
  	 	else{
           LL pos=query(tree1,1,1,n,times);
           printf("%lld,let's fly\n",pos);
           modify(tree1,1,pos,pos+times-1,0);
  	 	}
	 }
	 else if(str[0]=='N'){
	 	LL times;scanf("%lld",&times);
	 	if(tree1[1].sum>=times){
            LL pos1=query(tree1,1,1,n,times);
            printf("%lld,don't put my gezi\n",pos1);
			modify(tree1,1,pos1,pos1+times-1,0);
			modify(tree2,1,pos1,pos1+times-1,0);
	 	}
	 	else{
            if(tree2[1].sum>=times){
                LL pos1=query(tree2,1,1,n,times);
                printf("%lld,don't put my gezi\n",pos1);
                modify(tree1,1,pos1,pos1+times-1,0);
                modify(tree2,1,pos1,pos1+times-1,0);
            }
            else{
                printf("wait for me\n");
            }
	 	}
	 }
    else if(str[0]=='S'){///学习
        LL l,r;scanf("%lld%lld",&l,&r);
        printf("I am the hope of chinese chengxuyuan!!\n");
        modify(tree1,1,l,r,1);
        modify(tree2,1,l,r,1);
	 }
    }
  }
return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值