Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

HDU287:Memory Control(线段树区间合并)

$
0
0
Problem Description
Memory units are numbered from 1 up to N.
A sequence of memory units is called a memory block. 
The memory control system we consider now has four kinds of operations:
1.  Reset Reset all memory units free.
2.  New x Allocate a memory block consisted of x continuous free memory units with the least start number
3.  Free x Release the memory block which includes unit x
4.  Get x Return the start number of the xth memory block(Note that we count the memory blocks allocated from left to right)
Where 1<=x<=N.You are request to find out the output for M operations. 
 

Input
Input contains multiple cases.
Each test case starts with two integer N,M(1<=N,M<=50000) ,indicating that there are N units of memory and M operations.
Follow by M lines,each line contains one operation as describe above.
 

Output
For each “Reset” operation, output “Reset Now”.
For each “New” operation, if it’s possible to allocate a memory block,
output “New at A”,where Ais the least start number,otherwise output “Reject New”.
For each “Free” operation, if it’s possible to find a memory block occupy unit x,
output “Free from A to B”,where A and B refer to the start and end number of the memory block,otherwise output “Reject Free”.
For each “Get” operation, if it’s possible to find the xth memory blocks,
output “Get at A”,where A is its start number,otherwise output “Reject Get”.
Output one blank line after each test case.
 

Sample Input
6 10 New 2 New 5 New 2 New 2 Free 3 Get 1 Get 2 Get 3 Free 3 Reset
 

Sample Output
New at 1 Reject New New at 3 New at 5 Free from 3 to 4 Get at 1 Get at 5 Reject Get Reject Free Reset Now


 

 

题意:内存是一定的

New x :从内存编号1开始分配一个x的空间,能发下则输出这个区间的头地址,如果放不下则输出Reject New

Free x:释放包含x的那个区间,并且输出那个区间的头地址与尾地址,x这个地方不能被释放则输出Reject Free

Get x:得到第x个区间的头地址,如果这个区间不存在,输出Reject Get

Reset:释放所有内存

 

思路:标准线段树了,给每个操作设定一个函数,并且每次操作进行更新即可

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define lson 2*i
#define rson 2*i+1
#define lc l,mid,2*i
#define rc mid+1,r,2*i+1
const int L = 50000+10;

struct node
{
    int start,end;
    int ls,rs,ms;
    int lazy,cnt,cover;
} a[L<<2];

void pushdown(int mid,int L,int R,int i)
{
    if(a[i].lazy!=-1)
    {
        a[lson].lazy = a[rson].lazy = a[i].lazy;
        a[lson].ms = a[lson].ls = a[lson].rs = (mid-L+1)*a[i].lazy;
        a[rson].ms = a[rson].ls = a[rson].rs = (R-mid)*a[i].lazy;
        a[lson].start = a[rson].start = a[i].start;
        a[lson].end = a[rson].end = a[i].end;
        a[i].lazy = -1;
    }
}

void pushup(int mid,int L,int R,int i)
{
    a[i].ls = a[lson].ls;
    a[i].rs = a[rson].rs;
    if(a[i].ls == mid-L+1)
        a[i].ls+=a[rson].ls;
    if(a[i].rs == R-mid)
        a[i].rs+=a[lson].rs;
    a[i].ms = max(max(a[lson].ms,a[rson].ms),a[lson].rs+a[rson].ls);
}

void insert(int L,int R,int t,int l,int r,int i)//t为0释放该区间,为1则分配区间
{
    if(L<=l && r<=R)
    {
        a[i].lazy = t;
        a[i].ls = a[i].rs = a[i].ms = t*(r-l+1);
        if(t)
            a[i].start = a[i].end = -1;
        else
        {
            a[i].start = L;
            a[i].end = R;
        }
    }
    else
    {
        int mid = (l+r)>>1;
        pushdown(mid,l,r,i);
        if(L<=mid)
            insert(L,R,t,lc);
        if(R>mid)
            insert(L,R,t,rc);
        pushup(mid,l,r,i);
    }
}

void Reset(int n)
{
    insert(1,n,1,1,n,1);
    a[1].cover = 1;
    a[1].cnt = 0;
}

int New(int x,int l,int r,int i)
{
    if(l == r)
        return l;
    int mid = (l+r)>>1;
    pushdown(mid,l,r,i);
    if(a[lson].ms>=x)
        return New(x,lc);
    else if(a[lson].rs+a[rson].ls>=x)
        return mid-a[lson].rs+1;
    else
        return New(x,rc);
}

int Free(int x,int l,int r,int i)
{
    if(l == r)
        return i;
    int mid = (l+r)>>1;
    pushdown(mid,l,r,i);
    if(x<=mid)
        return Free(x,lc);
    else
        return Free(x,rc);
}

void countup(int i)
{
    a[i].cnt = a[lson].cnt+a[rson].cnt;
}

void countdown(int i)
{
    if(a[i].cover)
    {
        a[lson].cnt = a[rson].cnt = 0;
        a[lson].cover = a[rson].cover = 1;
        a[i].cover = 0;
    }
}

int Get(int x,int l,int r,int i)
{
    if(l == r)
        return l;
    else
    {
        int mid = (l+r)>>1;
        countdown(i);
        if(a[lson].cnt>=x)
            return Get(x,lc);
        else
            return Get(x-a[lson].cnt,rc);
    }
}

void count(int x,int t,int l,int r,int i)
{
    if(l == r)
        a[i].cnt = t;
    else
    {
        int mid = (l+r)>>1;
        countdown(i);
        if(x<=mid)
            count(x,t,lc);
        else
            count(x,t,rc);
        countup(i);
    }
}

int main()
{
    char s[20];
    int n,m,x,ans;
    while(~scanf("%d%d",&n,&m))
    {
        Reset(n);//初始状态既是内存全部释放的状态
        while(m--)
        {
            scanf(" %s",s);
            if(!strcmp(s,"Reset"))
            {
                Reset(n);
                printf("Reset Now\n");
            }
            else if(!strcmp(s,"New"))
            {
                scanf("%d",&x);
                if(a[1].ms>=x)
                {
                    ans = New(x,1,n,1);
                    printf("New at %d\n",ans);
                    count(ans,1,1,n,1);
                    insert(ans,ans+x-1,0,1,n,1);
                }
                else
                    printf("Reject New\n");
            }
            else if(!strcmp(s,"Free"))
            {
                scanf("%d",&x);
                ans = Free(x,1,n,1);
                if(a[ans].start<0)
                    printf("Reject Free\n");
                else
                {
                    printf("Free from %d to %d\n",a[ans].start,a[ans].end);
                    count(a[ans].start,0,1,n,1);
                    insert(a[ans].start,a[ans].end,1,1,n,1);
                }
            }
            else
            {
                scanf("%d",&x);
                if(x>a[1].cnt)
                    printf("Reject Get\n");
                else
                    printf("Get at %d\n",Get(x,1,n,1));
            }
        }
        printf("\n");
    }

    return 0;
}


 

 

作者:libin56842 发表于2013-11-21 23:13:38 原文链接
阅读:40 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>