寒假来了,又到了小明和女神们约会的季节。
小明虽为屌丝级码农,但非常活跃,女神们常常在小明网上的大段发言后热情回复“呵呵”,所以,小明的最爱就是和女神们约会。与此同时,也有很多基友找他开黑,由于数量实在过于巨大,怎么安排时间便成了小明的一大心事。
我们已知小明一共有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 = 100000 + 10;
struct f
{
int l, r, len;
int Max, Lmax, Rmax;
int Maxx, Lmaxx, Rmaxx;
}a[MAXN * 4];
void build(int l, int r, int rt)
{
a[rt].l = l; a[rt].r = r; a[rt].len = r - l + 1;
a[rt].Max = a[rt].Lmax = a[rt].Rmax = a[rt].len;
a[rt].Maxx = a[rt].Lmaxx = a[rt].Rmaxx = a[rt].len;
if(l == r)
return;
int mid = (l + r) >> 1;
build(l, mid, rt << 1);
build(mid + 1, r, rt << 1 | 1);
}
void pushup(int rt)
{
if(a[rt].l == a[rt].r)
return;
a[rt].Lmax = a[rt << 1].Lmax;
if(a[rt << 1].Lmax == a[rt << 1].len)
a[rt].Lmax += a[rt << 1 | 1].Lmax;
a[rt].Rmax = a[rt << 1 | 1].Rmax;
if(a[rt << 1 | 1].Rmax == a[rt << 1 | 1].len)
a[rt].Rmax += a[rt << 1].Rmax;
a[rt].Lmaxx = a[rt << 1].Lmaxx;
if(a[rt << 1].Lmaxx == a[rt << 1].len)
a[rt].Lmaxx += a[rt << 1 | 1].Lmaxx;
a[rt].Rmaxx = a[rt << 1 | 1].Rmaxx;
if(a[rt << 1 | 1].Rmaxx == a[rt << 1 | 1].len)
a[rt].Rmaxx += a[rt << 1].Rmaxx;
a[rt].Max = max (a[rt << 1].Max, a[rt << 1 | 1].Max);
a[rt].Max = max (a[rt].Max, a[rt].Lmax);
a[rt].Max = max (a[rt].Max, a[rt].Rmax);
a[rt].Max = max (a[rt].Max, a[rt << 1].Rmax + a[rt << 1 | 1].Lmax);
a[rt].Maxx = max (a[rt << 1].Maxx, a[rt << 1 | 1].Maxx);
a[rt].Maxx = max (a[rt].Maxx, a[rt].Lmaxx);
a[rt].Maxx = max (a[rt].Maxx, a[rt].Rmaxx);
a[rt].Maxx = max (a[rt].Maxx, a[rt << 1].Rmaxx + a[rt << 1 | 1].Lmaxx);
}
void pushdown(int rt)
{
if(a[rt].Max == a[rt].len)
{
a[rt << 1].Max = a[rt << 1].Lmax = a[rt << 1].Rmax = a[rt << 1].len;
a[rt << 1 | 1].Max = a[rt << 1 | 1].Lmax = a[rt << 1 | 1].Rmax = a[rt << 1 | 1].len;
}
else if(a[rt].Max == 0)
{
a[rt << 1].Max = a[rt << 1].Lmax = a[rt << 1].Rmax = 0;
a[rt << 1 | 1].Max = a[rt << 1 | 1].Lmax = a[rt << 1 | 1].Rmax = 0;
}
if(a[rt].Maxx == a[rt].len)
{
a[rt << 1].Maxx = a[rt << 1].Lmaxx = a[rt << 1].Rmaxx = a[rt << 1].len;
a[rt << 1 | 1].Maxx = a[rt << 1 | 1].Lmaxx = a[rt << 1 | 1].Rmaxx = a[rt << 1 | 1].len;
}
else if(a[rt].Maxx == 0)
{
a[rt << 1].Maxx = a[rt << 1].Lmaxx = a[rt << 1].Rmaxx = 0;
a[rt << 1 | 1].Maxx = a[rt << 1 | 1].Lmaxx = a[rt << 1 | 1].Rmaxx = 0;
}
}
int query(int l, int r, int rt, int k)
{
pushdown(rt);
if(a[rt].Max < k)
return 0;
if(a[rt].Lmax >= k)
return a[rt].l;
int mid = (l + r) >> 1;
if(a[rt << 1].Max >= k)
return query(l, mid, rt << 1, k);
if(a[rt << 1].Rmax + a[rt << 1 | 1].Lmax >= k)
return a[rt << 1].r - a[rt << 1].Rmax + 1;
return query(mid + 1, r, rt << 1 | 1, k);
}
int query2(int l, int r, int rt, int k)
{
if(a[rt].Maxx < k)
return 0;
if(a[rt].Lmaxx >= k)
return a[rt].l;
int mid = (l + r) >> 1;
if(a[rt << 1].Maxx >= k)
return query2(l, mid, rt << 1, k);
if(a[rt << 1].Rmaxx + a[rt << 1 | 1].Lmaxx >= k)
return a[rt << 1].r - a[rt << 1].Rmaxx + 1;
return query2(mid + 1, r, rt << 1 | 1, k);
}
void update(int x, int y, int l, int r, int rt, int flag)
{
if(l >= x && r <= y)
{
if(flag == 0)
a[rt].Max = a[rt].Lmax = a[rt].Rmax = 0;
else if(flag == 1)
{
a[rt].Max = a[rt].Lmax = a[rt].Rmax = a[rt].len;
a[rt].Maxx = a[rt].Lmaxx = a[rt].Rmaxx = a[rt].len;
}
else if(flag == 2)
{
a[rt].Max = a[rt].Lmax = a[rt].Rmax = 0;
a[rt].Maxx = a[rt].Lmaxx = a[rt].Rmaxx = 0;
}
return;
}
pushdown(rt);
int mid = (l + r) >> 1;
if(mid >= x)
update(x, y, l, mid, rt << 1, flag);
if(mid < y)
update(x, y, mid + 1, r, rt << 1 | 1, flag);
pushup(rt);
}
int main()
{
int T;
int cas = 0, k;
char q[20];
scanf("%d", &T);
while(T --)
{
cas ++;
int n, m, x, y;
scanf("%d %d", &n, &m);
build(1, n, 1);
printf("Case %d:\n", cas);
while(m --)
{
scanf("%s", q);
if(q[0] == 'D')
{
scanf("%d", &k);
int L = query(1, n, 1, k);
if(L != 0)
{
printf("%d,let's fly\n", L);
update(L, L + k - 1, 1, n, 1, 0);
}
else
printf("fly with yourself\n");
}
else if(q[0] == 'S')
{
scanf("%d %d", &x, &y);
printf("I am the hope of chinese chengxuyuan!!\n");
update(x, y, 1, n, 1, 1);
}
else
{
scanf("%d", &k);
int L = query(1, n, 1, k);
if(L != 0)
{
printf("%d,don't put my gezi\n", L);
update(L, L + k - 1, 1, n, 1, 2);
continue;
}
L = query2(1, n, 1, k);
if(L != 0)
{
printf("%d,don't put my gezi\n", L);
update(L, L + k - 1, 1, n, 1, 2);
continue;
}
else
printf("wait for me\n");
}
}
}
return 0;
}