您的位置:首页 > 其它

hdoj 4553 约会安排 【线段树区间合并】

2016-01-09 02:52 148 查看

约会安排

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 861    Accepted Submission(s): 243


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

 

思路:建立两棵线段树维护区间最大左连续、右连续,连续长度。Up、Down同步操作,DS单独更新,NS同步更新。

又一次无脑犯二。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (100000+10)
#define MAXM (500000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
using namespace std;
struct Tree
{
int l, r, len;
int lsum, rsum, sum;
int lazy;
};
Tree tree[2][MAXN<<2];
void PushUp(int o)
{
for(int i = 0; i < 2; i++)
{
tree[i][o].lsum = tree[i][ll].lsum;
tree[i][o].rsum = tree[i][rr].rsum;
tree[i][o].sum = max(tree[i][ll].sum, tree[i][rr].sum);
//tree[i][o].sum = max(tree[i][o].lsum, tree[i][o].rsum);//犯二写错了
tree[i][o].sum = max(tree[i][o].sum, tree[i][ll].rsum + tree[i][rr].lsum);
if(tree[i][o].lsum == tree[i][ll].len)
tree[i][o].lsum += tree[i][rr].lsum;
if(tree[i][o].rsum == tree[i][rr].len)
tree[i][o].rsum += tree[i][ll].rsum;
}
}
void PushDown(int o)
{
for(int i = 0; i < 2; i++)
{
if(tree[i][o].lazy != -1)
{
tree[i][ll].lazy = tree[i][rr].lazy = tree[i][o].lazy;
tree[i][ll].sum = tree[i][ll].lsum = tree[i][ll].rsum = tree[i][o].lazy * tree[i][ll].len;
tree[i][rr].sum = tree[i][rr].lsum = tree[i][rr].rsum = tree[i][o].lazy * tree[i][rr].len;
tree[i][o].lazy = -1;
}
}
}
void Build(int o, int l, int r)
{
for(int i = 0; i < 2; i++)
{
tree[i][o].l = l; tree[i][o].r = r;
tree[i][o].len = r-l+1;
tree[i][o].sum = tree[i][o].lsum = tree[i][o].rsum = r-l+1;
tree[i][o].lazy = -1;
}
if(l == r)
return ;
int mid = (l + r) >> 1;
Build(lson); Build(rson);
}
void Update(int o, Tree *T, int L, int R, int v)
{
if(T[o].l >= L && T[o].r <= R)
{
T[o].lazy = v;
T[o].sum = T[o].lsum = T[o].rsum = T[o].len * v;
return ;
}
PushDown(o);
int mid = (T[o].l + T[o].r) >> 1;
if(R <= mid)
Update(ll, T, L, R, v);
else if(L > mid)
Update(rr, T, L, R, v);
else
{
Update(ll, T, L, mid, v);
Update(rr, T, mid+1, R, v);
}
PushUp(o);
}
int Query(int o, Tree *T, int len)
{
if(T[o].l == T[o].r)
return T[o].l;
PushDown(o);
if(T[ll].sum >= len)
return Query(ll, T, len);
else if(T[ll].rsum + T[rr].lsum >= len)
return T[ll].r - T[ll].rsum + 1;
else
return Query(rr, T, len);
}
int main()
{
int t, kcase = 1; Ri(t);
W(t)
{
int N, Q; Ri(N); Ri(Q);
Build(1, 1, N);
printf("Case %d:\n", kcase++);
W(Q)
{
char op[10];
Rs(op); int x, y;
if(op[0] == 'D')
{
Ri(x);
if(tree[0][1].sum < x)
printf("fly with yourself\n");
else
{
int s = Query(1, tree[0], x);
printf("%d,let's fly\n", s);
Update(1, tree[0], s, s+x-1, 0);
}
}
else if(op[0] == 'N')
{
Ri(x);
if(tree[0][1].sum < x && tree[1][1].sum < x)
printf("wait for me\n");
else if(tree[0][1].sum >= x)
{
int s = Query(1, tree[0], x);
printf("%d,don't put my gezi\n", s);
Update(1, tree[1], s, s+x-1, 0);
Update(1, tree[0], s, s+x-1, 0);
}
else
{
int s = Query(1, tree[1], x);
printf("%d,don't put my gezi\n", s);
Update(1, tree[1], s, s+x-1, 0);
Update(1, tree[0], s, s+x-1, 0);
}
}
else
{
Ri(x); Ri(y);
Update(1, tree[0], x, y, 1);
Update(1, tree[1], x, y, 1);
printf("I am the hope of chinese chengxuyuan!!\n");
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: