HDU 2871 Memory Control (恶心线段树,HOTEL加强版)

//4677952 2011-09-29 17:10:55 Accepted 2871 421MS 3700K 5156 B G++ nkhelloworld
//4677960 2011-09-29 17:11:58 Accepted 2871 281MS 3628K 5156 B C++ nkhelloworld
/*
POJ上Hotel的加强版,多了Get操作,Free操作有一些变化。
用vector记录已经开出来的内存,维持有序,遇到get和free二分查找
*/
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define MAXN 50010
struct SEGMENTTREE
{
    int lt,rt,lval,rval,totval,state;
}tree[4*MAXN];
int n,m;
void buildsegtree(int root,int lt,int rt)
{
    tree[root].lt = lt; tree[root].rt = rt;
    tree[root].lval = tree[root].rval = tree[root].totval = rt - lt + 1;
    tree[root].state = 0;
    if(lt != rt)
    {
        int mid = (lt+rt)>>1;
        buildsegtree(root<<1,lt,mid);
        buildsegtree(root<<1|1,mid+1,rt);
    }
}
void pushdown(int root)
{
    tree[root<<1].state = tree[root<<1|1].state = tree[root].state;
    if(tree[root].state == 0)
    {
        tree[root<<1].lval = tree[root<<1].rval = tree[root<<1].totval = tree[root<<1].rt - tree[root<<1].lt + 1;
        tree[root<<1|1].lval = tree[root<<1|1].rval = tree[root<<1|1].totval = tree[root<<1|1].rt - tree[root<<1|1].lt +1;
    }
    else
    {
        tree[root<<1].lval = tree[root<<1].rval = tree[root<<1].totval = 0;
        tree[root<<1|1].lval = tree[root<<1|1].rval = tree[root<<1|1].totval = 0;
    }
    tree[root].state = -1;
}
int query(int root,int lenth)
{
    if(tree[root].lt == tree[root].rt)
    {
        if(tree[root].state == false && lenth == 1)
            return tree[root].lt;
        return 0;
    }
    if(tree[root].state != -1)
        pushdown(root);
    if(tree[root<<1].totval >= lenth)
        return query(root<<1,lenth);
    if(tree[root<<1].rval + tree[root<<1|1].lval >= lenth)
        return tree[root<<1].rt - tree[root<<1].rval + 1;
    if(tree[root<<1|1].totval >= lenth)
        return query(root<<1|1,lenth);
    return 0;
}

void update(int root,int lt,int rt,int state)
{
    if(tree[root].lt == lt && tree[root].rt == rt)
    {
        tree[root].state = state;
        if(state==1)
            tree[root].lval = tree[root].rval = tree[root].totval = 0;
        else if(state == 0)
            tree[root].lval = tree[root].rval = tree[root].totval = rt - lt+ 1;
        return ;
    }
    if(tree[root].state != -1)
        pushdown(root);
    int mid = (tree[root].lt + tree[root].rt)>>1;
    if( rt <= mid )
        update(root<<1,lt,rt,state);
    else if(lt > mid)
        update(root<<1|1,lt,rt,state);
    else
    {
        update(root<<1,lt,mid,state);
        update(root<<1|1,mid+1,rt,state);
    }
    tree[root].lval = tree[root<<1].lval;
    tree[root].rval = tree[root<<1|1].rval;
    tree[root].totval = max( max(tree[root<<1].totval,tree[root<<1|1].totval) , tree[root<<1].rval + tree[root<<1|1].lval);
    if(tree[root].lval == tree[root<<1].rt - tree[root<<1].lt + 1)
        tree[root].lval += tree[root<<1|1].lval;
    if(tree[root].rval == tree[root<<1|1].rt - tree[root<<1|1].lt +1)
        tree[root].rval += tree[root<<1].rval;
    if(tree[root<<1].state == tree[root<<1|1].state)
        tree[root].state = tree[root<<1].state;
}

struct BLOCK
{
    int st,ed;
};

vector<BLOCK> vec;
bool cmp(const BLOCK &b1,const BLOCK &b2)
{
    return b1.st < b2.st;
}

int main()
{
    int i,a,b;
    char op[10];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        buildsegtree(1,1,n);
        vec.clear();
        while(m--)
        {
            scanf("%s",op);
            if(op[0]=='R')
            {
                printf("Reset Now\n");
                update(1,1,n,0);
                vec.clear();
            }
            else if(op[0] == 'N')
            {
                scanf("%d",&a);
                b = query(1,a);
                if(b)
                {
                    printf("New at %d\n",b);
                    update(1,b,b+a-1,1);
                    BLOCK tmp;
                    tmp.st = b; tmp.ed = b + a - 1;
                    if(vec.size()==0)
                        vec.push_back(tmp);
                    else
                    {
                        vector<BLOCK>::iterator it;
                        it = upper_bound(vec.begin(),vec.end(),tmp,cmp);
                        vec.insert(it,tmp);
                    }
                }
                else
                    printf("Reject New\n");
            }
            else if(op[0] == 'F')
            {
                scanf("%d",&a);
                BLOCK tmp;
                tmp.st = a; tmp.ed = a;
                vector<BLOCK>::iterator it;
                it = upper_bound(vec.begin(),vec.end(),tmp,cmp);
                if(it == vec.begin() || (--it)->ed < a)
                {
                    printf("Reject Free\n");
                }
                else
                {
                    printf("Free from %d to %d\n",it->st,it->ed);
                    update(1,it->st,it->ed,0);
                    vec.erase(it);
                }
            }
            else
            {
                scanf("%d",&a);
                if(vec.size() < a)
                    printf("Reject Get\n");
                else
                    printf("Get at %d\n",vec[a-1].st);
            }
        }
        printf("\n");
    }
    return 0;
}

转载于:https://www.cnblogs.com/nkhelloworld/archive/2011/09/29/2195702.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值