Appointment arrangement HDU-4553 (line segment tree interval merge + priority + longest continuous 1)

The output sentence in the original title is mixed with Chinese. I have modified it.
  Winter vacation is here, and it’s the season for Xiao Ming to date the goddesses.
  Although Xiao Ming is a diaosi-level code farmer, he is very active. The goddesses often enthusiastically reply "ha ha" after a long speech on Xiao Ming. Therefore, Xiao Ming's favorite is to date the goddesses. At the same time, there are also many friends who ask him to open hacks. Because the number is too large, how to arrange the time has become a big concern for Xiao Ming.
  We know that Xiao Ming has a total of T free time, during which many goddesses or friends will come to Xiao Ming.
  As a great god who has ever scored 71 points on the operating system, Xiaoming thought of an algorithm, the "first adaptation algorithm". According to the description of the operating system textbook, it is to find the top continuous space that meets the requirements and allocate it to each request. From this, Xiao Ming made a decision:
  When a friend comes to Xiao Ming, Xiao Ming uses the "first adaptation algorithm" to find some free time to make an appointment with the friend, and if he finds it, he says "X, let's fly" (Here, X is the start time), otherwise just say "fly with yourself";
  when the goddess comes to find Xiao Ming, first use the "first adaptation algorithm", if not found, Xiao Ming risked the risk of ignoring everything The agreement of Diaosi friends is to use the "Ignore the first adaptation algorithm of the friends" again. As long as you find it once twice, you will say "X, don't put my gezi" (here, X is the start time), otherwise you will say " "Wait for me"
  Of course, we know that Xiao Ming is not a person with infinite ethics. If he finishes dating the goddess and there is still time left, he will still go to dota with his original friends. (For example: Xiaoxi (Diaosi) and Xiaoming have agreed to play dota within the time unit of 1~5. At this time, the goddess comes to make an appointment with Xiaoming for a time period of 3, so in the end it will be 1~3 Xiaoming go to the goddess Dating, play dota with Konishi at 4~5 after you get it done)
  Xiao Ming occasionally wants to learn new knowledge. At this time, Xiao Ming will clear all the scheduled time in a certain time interval for learning and shout "I am the hope of chinese chengxuyuan!!", but Xiao Ming generally does After three minutes of heat, if someone comes to book again, Xiao Ming will allocate time for learning new knowledge according to his loneliness.


Idea: If you do the hotel question, this question can be easily figured out. It is also the longest continuous 1 in the maintenance interval, but there are two trees here, one diaosi tree and one goddess tree. Modify and query the interval between the two trees according to the actual corresponding operation.

Note the bug in the code: the output is in English quotation marks.

I checked the bug for 5 days, but I didn't find that the output variable corresponding to Case %d: was written as a constant because it was a single-group test during the test. Thanks Singh for the match (hope he won't kill me

#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;
}


 

Guess you like

Origin blog.csdn.net/zstuyyyyccccbbbb/article/details/109825570