HDU 2871 Memory Control 区间合并+区间更新

Memory Control

Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

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
 

做法和hotal类似,多了一个vector查找;

vector:
lower_bound()找到第一个大于val的位置返回;
insert()找到第一个大于val的位置从val左边插入;

#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
//#pragma comment(linker, "/STACK:1024000000,1024000000");

using namespace std;

#define INF 0x3f3f3f3f

struct node
{
    int l,r;
    int tag;
    int lr,rt,len;
} T[400000];

node Merge(node A,node B)
{
    node C;
    C.l=A.l,C.r=B.r;
    C.tag=-1;
    C.lr=A.lr;
    C.rt=B.rt;
    C.len=max(A.len,B.len);
    C.len=max(C.len,A.rt+B.lr);
    if(A.len==A.r-A.l+1) C.lr+=B.lr;
    if(B.len==B.r-B.l+1) C.rt+=A.rt;
    C.len=max(C.len,max(C.lr,C.rt));
    return C;
}

void pushdown(int k)
{
    if(T[k].tag==-1||T[k].l==T[k].r) return ;
    T[2*k].tag=T[2*k+1].tag=T[k].tag;
    if(T[k].tag==0)
    {
        T[2*k].lr=T[2*k].rt=T[2*k].len=T[2*k].r-T[2*k].l+1;
        T[2*k+1].lr=T[2*k+1].rt=T[2*k+1].len=T[2*k+1].r-T[2*k+1].l+1;
    }
    else
    {
        T[2*k].lr=T[2*k].rt=T[2*k].len=0;
        T[2*k+1].lr=T[2*k+1].rt=T[2*k+1].len=0;
    }
    T[k].tag=-1;
}

void init(int l,int r,int k)
{
    T[k].l=l;
    T[k].r=r;
    T[k].tag=-1;
    if(l==r)
    {
        T[k].len=T[k].rt=T[k].lr=1;
        return ;
    }
    int mid=(l+r)>>1;
    init(l,mid,2*k);
    init(mid+1,r,2*k+1);
    T[k]=Merge(T[2*k],T[2*k+1]);
}

void Insert(int d,int l,int r,int k)
{
    pushdown(k);
    if(T[k].l==l&&T[k].r==r)
    {
        T[k].tag=d;
        if(d==0) T[k].lr=T[k].rt=T[k].len=T[k].r-T[k].l+1;
        else T[k].lr=T[k].rt=T[k].len=0;
        return ;
    }
    int mid=(T[k].l+T[k].r)>>1;
    if(r<=mid) Insert(d,l,r,2*k);
    else if(l>mid) Insert(d,l,r,2*k+1);
    else
    {
        Insert(d,l,mid,2*k);
        Insert(d,mid+1,r,2*k+1);
    }
    T[k]=Merge(T[2*k],T[2*k+1]);
}

int Find(int d,int l,int r,int k)
{
    pushdown(k);
    if(l==r) return l;
    int mid=(T[k].l+T[k].r)>>1;
    if(T[2*k].len>=d) Find(d,l,mid,2*k);
    else if(T[2*k].rt+T[2*k+1].lr>=d) return mid-T[2*k].rt+1;
    else Find(d,mid+1,r,2*k+1);
}

vector<int>l,r;
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        l.clear();
        r.clear();
        init(1,n,1);
        while(m--)
        {
            char s[10];
            scanf("%s",s);
            if(strcmp(s,"New")==0)
            {
                int temp;
                scanf("%d",&temp);
                if(temp>T[1].len)
                {
                    printf("Reject New\n");
                    continue;
                }
                int vim=Find(temp,1,n,1);
                int vis=lower_bound(r.begin(),r.end(),vim)-r.begin();
                l.insert(l.begin()+vis,vim);
                r.insert(r.begin()+vis,vim+temp-1);
                Insert(1,vim,vim+temp-1,1);
                printf("New at %d\n",vim);
            }
            else if(strcmp(s,"Free")==0)
            {
                int temp;
                scanf("%d",&temp);
                int vim=lower_bound(r.begin(),r.end(),temp)-r.begin();
                if(vim==l.size())
                {
                     printf("Reject Free\n");
                     continue;
                }
                if(temp>=l[vim]&&temp<=r[vim])
                {
                    Insert(0,l[vim],r[vim],1);
                    printf("Free from %d to %d\n",l[vim],r[vim]);
                    l.erase(l.begin()+vim);
                    r.erase(r.begin()+vim);
                }
                else
                {
                    printf("Reject Free\n");
                }
            }
            else if(strcmp(s,"Get")==0)
            {
                int temp;
                scanf("%d",&temp);
                if(temp>l.size())
                {
                    printf("Reject Get\n");
                    continue;
                }
                printf("Get at %d\n",l[temp-1]);
            }
            else if(strcmp(s,"Reset")==0)
            {
                l.clear();
                r.clear();
                Insert(0,1,n,1);
                printf("Reset Now\n");
            }
        }
        puts("");
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值