线段树区间维护(各种操作)hdu2871

Memory Control

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4355    Accepted Submission(s): 1040


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
 

Source
 

又是一个神题,这个跟poj3667很像

题意:让你模拟内存控制,可能申请连续x个空间,返回编号最小的位置,或者清空包含x单元的整个空间,返回空间的头和尾,或者获取从左到右数的第x的空间,或者整个内存重置为空。。。。

分析:申请空间和释放空间,只要申请的时候把被覆盖改成被哪个id覆盖,然后开个数组记录这个id的头和尾,释放的时候就可以找到覆盖的id,进而找到头和尾,重置内存其实就是把整个内存都释放掉。。。不用多写函数。。。至于寻找第x个空间,这个比较麻烦,如果另开一棵线段树来统计,我想回好写很多,就单点更新和成段清零,用个延迟标记就行,不过我最后选择直接写在一起了,这个就得有两个延迟标记,一个记录是否要累加,一个记录是否清零,当然,清零的时候累加标记也要清零。。。


sum表示区间内有多少个不同的内存块,ls表示左边连续的空块,rs表示右边连续的空块,idx表示区间被哪个快覆盖(0表示被清空),clr表示是否被释放(1表示被释放),add表示区间是否被内存块覆盖。


#include<cstdio>
#include<iostream>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define uprt rt,m-l+1,r-m
using namespace std;
const int mm=55555;
const int mn=mm<<2;
int idx[mn],add[mn],clr[mn],sum[mn],ml[mn],lm[mn],rm[mn];
int sl[mm],sr[mm];
void set(int rt,int id,int len)
{
    ml[rt]=lm[rt]=rm[rt]=id?0:len;
    if(!id)sum[rt]=add[rt]=0,clr[rt]=1;
    idx[rt]=id;
}
void pushdown(int rt,int l1,int l2)
{
    if(idx[rt]>-1)
    {
        set(rt<<1,idx[rt],l1);
        set(rt<<1|1,idx[rt],l2);
        idx[rt]=-1;
    }
    if(clr[rt])
    {
        sum[rt<<1]=sum[rt<<1|1]=add[rt<<1]=add[rt<<1|1]=0;
        clr[rt<<1]=clr[rt<<1|1]=1;
        clr[rt]=0;
    }
    if(add[rt])
    {
        ++sum[rt<<1],add[rt<<1]=1;//求的是第x个内存块,所以只需要知道开头就可以了
        add[rt]=0;
    }
}
void pushup(int rt,int l1,int l2)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    ml[rt]=max(rm[rt<<1]+lm[rt<<1|1],max(ml[rt<<1],ml[rt<<1|1]));
    lm[rt]=lm[rt<<1],rm[rt]=rm[rt<<1|1];
    if(lm[rt]>=l1)lm[rt]+=lm[rt<<1|1];
    if(rm[rt]>=l2)rm[rt]+=rm[rt<<1];
}
void build(int l,int r,int rt)
{
    idx[rt]=-1,clr[rt]=add[rt]=sum[rt]=0;
    ml[rt]=lm[rt]=rm[rt]=r-l+1;
    if(l==r)return;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}
void updata(int L,int R,int id,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        set(rt,id,r-l+1);
        if(id&&L==l)++sum[rt],add[rt]=1;
        return;
    }
    int m=(l+r)>>1;
    pushdown(uprt);
    if(L<=m)updata(L,R,id,lson);
    if(R>m)updata(L,R,id,rson);
    pushup(uprt);
}
int NEW(int x,int l,int r,int rt)
{
    if(lm[rt]>=x)return l;
    int m=(l+r)>>1,ret;
    pushdown(uprt);
    if(ml[rt<<1]>=x)ret=NEW(x,lson);
    else if(rm[rt<<1]+lm[rt<<1|1]>=x)ret=m-rm[rt<<1]+1;
    else ret=NEW(x,rson);
    pushup(uprt);
    return ret;
}
int FREE(int x,int l,int r,int rt)
{
    if(idx[rt]>-1)return idx[rt];
    if(l==r)return 0;
    int m=(l+r)>>1,ret;
    pushdown(uprt);
    if(x<=m)ret=FREE(x,lson);
    else ret=FREE(x,rson);
    pushup(uprt);
    return ret;
}
int GET(int x,int l,int r,int rt)
{
    if(l==r)return l;
    int m=(l+r)>>1,ret;
    pushdown(uprt);
    if(sum[rt<<1]>=x)ret=GET(x,lson);
    else ret=GET(x-sum[rt<<1],rson);
    pushup(uprt);
    return ret;
}
int main()
{
    int x,n,m,top;
    char op[55];
    while(~scanf("%d%d",&n,&m))
    {
        build(1,n,1);
        top=0;
        while(m--)
        {
            scanf("%s",op);
            if(op[0]=='R')
            {
                updata(1,n,0,1,n,1);
                puts("Reset Now");
            }
            else scanf("%d",&x);
            if(op[0]=='N')
            {
                if(ml[1]>=x)
                {
                    ++top;
                    sl[top]=NEW(x,1,n,1);
                    sr[top]=sl[top]+x-1;
                    updata(sl[top],sr[top],top,1,n,1);
                    printf("New at %d\n",sl[top]);
                }
                else puts("Reject New");
            }
            if(op[0]=='F')
            {
                x=FREE(x,1,n,1);
                if(x)
                {
                    updata(sl[x],sr[x],0,1,n,1);
                    printf("Free from %d to %d\n",sl[x],sr[x]);
                }
                else puts("Reject Free");
            }
            if(op[0]=='G')
            {
                if(sum[1]>=x)printf("Get at %d\n",GET(x,1,n,1));
                else puts("Reject Get");
            }
        }
        puts("");
    }
    return 0;
}

这个题看到很多人直接用的hotel那提的代码,然后用vector做的,写起来比上面的方法简单很多


#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 55555;
int lsum[maxn<<2],rsum[maxn<<2],msum[maxn<<2],cover[maxn<<2];
struct node {
    int l,r;
    bool operator < (const node &cmp) const {
        return l<cmp.l;
    }
}tno;
vector<node> mem;
void pushup(int rt,int m){
    lsum[rt]=lsum[rt<<1];
    rsum[rt]=rsum[rt<<1|1];
    if(lsum[rt] == m-(m>>1)) lsum[rt]+=lsum[rt<<1|1];
    if(rsum[rt] == (m>>1)) rsum[rt]+=rsum[rt<<1];
    msum[rt]=max(lsum[rt<<1|1]+rsum[rt<<1],max(msum[rt<<1],msum[rt<<1|1]));
}
void pushdown(int rt ,int m){
    if(cover[rt]!=-1){
        cover[rt<<1]=cover[rt<<1|1]=cover[rt];
        msum[rt<<1]=lsum[rt<<1]=rsum[rt<<1]=cover[rt] ? 0 :m-(m>>1);
        msum[rt<<1|1]=lsum[rt<<1|1]=rsum[rt<<1|1]=cover[rt] ? 0 : (m>>1);
        cover[rt]=-1;
    }
}
void build(int l,int r,int rt){
    msum[rt]=lsum[rt]=rsum[rt]=r-l+1;
    cover[rt]=-1;
    if(l==r) return ;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}
void update(int L,int R,int c,int l,int r,int rt){
    if(L<=l&&r<=R){
        msum[rt]=lsum[rt]=rsum[rt]= c ? 0 : r-l+1;
        cover[rt]=c;
        return ;
    }
    pushdown(rt,r-l+1);
    int m=(l+r)>>1;
    if(L <= m) update(L,R,c,lson);
    if(R > m) update(L,R,c,rson);
    pushup(rt,r-l+1);
}
int query(int w,int l,int r,int rt){
    if(l==r) return l;
    pushdown(rt,r-l+1);
    int m=(l+r)>>1;
    if(msum[rt<<1]>=w) return query(w,lson);
    else if(rsum[rt<<1]+lsum[rt<<1|1]>=w) return m-rsum[rt<<1]+1;
    return query(w,rson);
}
int main(){
    int n,m,i,j,k,a,b,c,pos;
    char str[10];
    vector<node> :: iterator it;
    while(~scanf("%d%d",&n,&m)){
        build(1,n,1);
        mem.clear();
        while(m--){
            scanf("%s",str);
            if(str[0]=='N'){    
                scanf("%d",&a);
                if(msum[1]<a) puts("Reject New");
                else {
                    b=query(a,1,n,1);
                    update(b,b+a-1,1,1,n,1);
                    tno.l=b;tno.r=b+a-1;
                    it=upper_bound(mem.begin(),mem.end(),tno);
                    mem.insert(it,tno);
                    printf("New at %d\n",b);
                }
            }
            else if(str[0]=='F'){
                scanf("%d",&a);
                tno.l=a;tno.r=a;
                pos=upper_bound(mem.begin(),mem.end(),tno)-mem.begin()-1;
                if(pos==-1||mem[pos].r<a) puts("Reject Free");
                else {
                    printf("Free from %d to %d\n",mem[pos].l,mem[pos].r);
                    update(mem[pos].l,mem[pos].r,0,1,n,1);
                    mem.erase(mem.begin()+pos);
                }
            }
            else if(str[0]=='G'){
                scanf("%d",&a);
                if(a>mem.size()) puts("Reject Get");
                else     printf("Get at %d\n",mem[a-1].l);
            }
            else if(str[0]=='R'){
                update(1,n,0,1,n,1);
                puts("Reset Now");
                mem.clear();
            }
        }
      puts("");
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值