hdu4553-幽会安排

hdu4553----约会安排

约会安排

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 402    Accepted Submission(s): 127


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
 

Source
2013金山西山居创意游戏程序挑战赛——初赛(3)
 

Recommend

这题我开了2个线段树,第一个线段树用来存屌丝区间,第二个用女神区间,对于屌丝的请求,只要去更新查询第一个线段树, 对于女神的请求,先查询屌丝区间,如果有,那么把2个线段树上对应区间更新,如果没有,去第二个线段树找,如果找到,更新2个线段树上相应区间(女神的级别高,所以可以无视第一个线段树上被占用的区间(屌丝的级别低2333333))
如果是清空,那么2个线段树都要去清空

/*************************************************************************
    > File Name: hdu4553.cpp
    > Author: ALex
    > Mail: 405045132@qq.com 
    > Created Time: 2015年01月14日 星期三 12时21分40秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;

struct node
{
    int l, r;
    int add;
    int l0, r0, s0;
}tree1[N << 2], tree2[N << 2];

void pushup (node tree[], int p)
{
    tree[p].l0 = tree[p << 1].l0;
    tree[p].r0 = tree[p << 1 | 1].r0;
    if (tree[p << 1].l0 == tree[p << 1].r - tree[p << 1].l + 1)
    {
        tree[p].l0 += tree[p << 1 | 1].l0;
    }
    if (tree[p << 1 | 1].r0 == tree[p << 1 | 1].r - tree[p << 1 | 1].l + 1)
    {
        tree[p].r0 += tree[p << 1].r0;
    }
    tree[p].s0 = max (tree[p << 1].r0 + tree[p << 1 | 1].l0, max(tree[p << 1].s0, tree[p << 1 | 1].s0));
}

void pushdown (node tree[], int p)
{
    if (tree[p].add != -1)
    {
        if (tree[p].add == 1)
        {
            tree[p << 1].l0 = tree[p << 1].r0 = tree[p << 1].s0 = 0;
            tree[p << 1 | 1].l0 = tree[p << 1 | 1].r0 = tree[p << 1 | 1].s0 = 0;
            tree[p << 1].add = 1;
            tree[p << 1 | 1].add = 1;
        }
        else
        {
            tree[p << 1].l0 = tree[p << 1].r0 = tree[p << 1].s0 = tree[p << 1].r - tree[p << 1].l + 1;
            tree[p << 1 | 1].l0 = tree[p << 1 | 1].r0 = tree[p << 1 | 1].s0 = tree[p << 1 | 1].r - tree[p << 1 | 1].l + 1;
            tree[p << 1].add = 0;
            tree[p << 1 | 1].add = 0;
        }
        tree[p].add = -1;
    }
}

void build (node tree[], int p, int l, int r)
{
    tree[p].l = l;
    tree[p].r = r;
    tree[p].l0 = tree[p].r0 = tree[p].s0 = r - l + 1;
    tree[p].add = -1;
    if (l == r)
    {
        return;
    }
    int mid = (l + r) >> 1;
    build (tree, p << 1, l, mid);
    build (tree, p << 1 | 1, mid + 1, r);
}

void update (node tree[], int p, int l, int r, int x)
{
    if (tree[p].l == l && tree[p].r == r)
    {
        if (x == 1)
        {
            tree[p].l0 = tree[p].r0 = tree[p].s0 = 0;
            tree[p].add = 1;
        }
        else
        {
            tree[p].l0 = tree[p].r0 = tree[p].s0 = tree[p].r - tree[p].l + 1;
            tree[p].add = 0;
        }
        return;
    }
    pushdown (tree, p);
    int mid = (tree[p].l + tree[p].r) >> 1;
    if (r <= mid)
    {
        update (tree, p << 1, l, r, x);
    }
    else if (l > mid)
    {
        update (tree, p << 1 | 1, l, r, x);
    }
    else
    {
        update (tree, p << 1, l, mid, x);
        update (tree, p << 1 | 1, mid + 1, r, x);
    }
    pushup (tree, p);
}

int query (node tree[], int p, int x)
{
    if (tree[p].l == tree[p].r)
    {
        return tree[p].l; 
    }
    pushdown (tree, p);
    int mid = (tree[p].l + tree[p].r) >> 1;
    if (tree[p << 1].s0 >= x)
    {
        return query (tree, p << 1, x);
    }
    else if (tree[p << 1].r0 + tree[p << 1 | 1].l0 >= x)
    {
        return mid - tree[p << 1].r0 + 1;
    }
    else
    {
        return query (tree, p << 1 | 1, x);
    }
}

int main()
{
    int t;
    int icase = 1;
    char str[22];
    int n, m;
    int x, y;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d%d", &n, &m);
        printf("Case %d:\n", icase++);
        build (tree1, 1, 1, n); //屌丝
        build (tree2, 1, 1, n); //女神
        while (m--)
        {
            scanf("%s", str);
            if (str[0] == 'S')
            {
                scanf("%d%d", &x, &y);
                update (tree1, 1, x, y, 0);
                update (tree2, 1, x, y, 0);
                printf("I am the hope of chinese chengxuyuan!!\n");
            }
            else
            {
                scanf("%d", &x);
                if (str[0] == 'D')
                {
                    if (tree1[1].s0 < x)
                    {
                        printf("fly with yourself\n");
                    }
                    else
                    {
                        int s = query (tree1, 1, x);
                        printf("%d,let's fly\n", s);
                        update (tree1, 1, s, s + x - 1, 1);
                    }
                }
                else
                {
                    if (tree1[1].s0 >= x)
                    {
                        int s = query (tree1, 1, x);
                        printf("%d,don't put my gezi\n", s);
                        update (tree1, 1, s, s + x - 1, 1);
                        update (tree2, 1, s, s + x - 1, 1);
                    }
                    else
                    {
                        if (tree2[1].s0 >= x)
                        {
                            int s = query (tree2, 1, x);
                            printf("%d,don't put my gezi\n", s);
                            update (tree1, 1, s, s + x - 1, 1);
                            update (tree2, 1, s, s + x - 1, 1);
                        }
                        else
                        {
                            printf("wait for me\n");
                        }
                    }
               }
            }
        }
    }
    return 0;
}