和poj 3667 Hotel差不多,三种操作。
节点信息有
struct node
{
int ds;//基友标记
int ns;//女神标记
int st;//学习标记
int most,lmost,rmost;//最长空时间,最左空时间,最右空时间
int tmost,tlmost,trmost;//女神专用最长空时间,最左空时间,最右空时间
}seg[N<<2];
1.DS,查询most,并且更新。
2.NS,查询tmost,并且更新。
3.STUDY,清除ds,ns标记,更新区间信息。
注意pushdown()的时候一定要先传递st标记。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
#define lc l,m,index<<1
#define rc m+1,r,index<<1|1
#define N 100005
struct node
{
int ds;//基友标记
int ns;//女神标记
int st;//学习标记
int most,lmost,rmost;//最长空时间,最左空时间,最右空时间
int tmost,tlmost,trmost;//女神专用最长空时间,最左空时间,最右空时间
}seg[N<<2];
void fds(int l,int r,int index)//打dota
{
seg[index].ds=1;
seg[index].most=seg[index].lmost=seg[index].rmost=0;
}
void fns(int l,int r,int index)//约会
{
seg[index].ns=1;
seg[index].ds=0;
seg[index].most=seg[index].lmost=seg[index].rmost=0;
seg[index].tmost=seg[index].tlmost=seg[index].trmost=0;
}
void fst(int l,int r,int index)//学习
{
seg[index].st=1;
seg[index].ds=0;
seg[index].ns=0;
seg[index].most=seg[index].lmost=seg[index].rmost=r-l+1;
seg[index].tmost=seg[index].tlmost=seg[index].trmost=r-l+1;
}
void pushup(int l,int r,int index)
{
int m=(l+r)>>1;
node& father=seg[index];
node& lson=seg[index<<1];
node& rson=seg[index<<1|1];
father.most=max(lson.most,rson.most);
father.most=max(father.most,lson.rmost+rson.lmost);
father.lmost=lson.lmost;
father.rmost=rson.rmost;
if(lson.lmost==m-l+1)father.lmost+=rson.lmost;
if(rson.rmost==r-m)father.rmost+=lson.rmost;
father.tmost=max(lson.tmost,rson.tmost);
father.tmost=max(father.tmost,lson.trmost+rson.tlmost);
father.tlmost=lson.tlmost;
father.trmost=rson.trmost;
if(lson.tlmost==m-l+1)father.tlmost+=rson.tlmost;
if(rson.trmost==r-m)father.trmost+=lson.trmost;
}
void pushdown(int l,int r,int index)
{
int m=(l+r)>>1;
node& father=seg[index];
node& lson=seg[index<<1];
node& rson=seg[index<<1|1];
if(father.st)
{
fst(lc);
fst(rc);
father.st=0;
}
if(father.ds)
{
fds(lc);
fds(rc);
father.ds=0;
}
if(father.ns)
{
fns(lc);
fns(rc);
father.ns=0;
}
}
int query(int flag,int num,int l,int r,int index)
{
int m=(l+r)>>1;
if(l==r)return l;
pushdown(l,r,index);
if(!flag)
{
if(seg[index<<1].most>=num)return query(flag,num,lc);
else if(seg[index<<1].rmost+seg[index<<1|1].lmost>=num)return m-seg[index<<1].rmost+1;
else return query(flag,num,rc);
}
else
{
if(seg[index<<1].tmost>=num)return query(flag,num,lc);
else if(seg[index<<1].trmost+seg[index<<1|1].tlmost>=num)return m-seg[index<<1].trmost+1;
else return query(flag,num,rc);
}
}
void updata(int flag,int L,int R,int l,int r,int index)
{
int m=(l+r)>>1;
if(L==l&&R==r)
{
if(!flag)fds(l,r,index);
else fns(l,r,index);
return;
}
pushdown(l,r,index);
if(R<=m)updata(flag,L,R,lc);
else if(L>m)updata(flag,L,R,rc);
else
{
updata(flag,L,m,lc);
updata(flag,m+1,R,rc);
}
pushup(l,r,index);
}
void study(int L,int R,int l,int r,int index)
{
int m=(l+r)>>1;
if(L==l&&R==r)
{
fst(l,r,index);
return;
}
pushdown(l,r,index);
if(R<=m)study(L,R,lc);
else if(L>m)study(L,R,rc);
else
{
study(L,m,lc);
study(m+1,R,rc);
}
pushup(l,r,index);
}
int n,q;
int main()
{
int t,tcase=1,a,b;
char op[15];
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&q);
study(1,n,1,n,1);
printf("Case %d:\n",tcase++);
while(q--)
{
scanf("%s",op);
if(op[0]=='D')
{
scanf("%d",&a);
if(seg[1].most<a)printf("fly with yourself\n");
else
{
b=query(0,a,1,n,1);
updata(0,b,b+a-1,1,n,1);
printf("%d,let's fly\n",b);
}
}
else if(op[0]=='N')
{
scanf("%d",&a);
if(seg[1].most<a)
{
if(seg[1].tmost<a)printf("wait for me\n");
else
{
b=query(1,a,1,n,1);
updata(1,b,b+a-1,1,n,1);
printf("%d,don't put my gezi\n",b);
}
}
else
{
b=query(0,a,1,n,1);
updata(1,b,b+a-1,1,n,1);
printf("%d,don't put my gezi\n",b);
}
}
else
{
scanf("%d%d",&a,&b);
study(a,b,1,n,1);
printf("I am the hope of chinese chengxuyuan!!\n");
}
}
}
return 0;
}