zoukankan      html  css  js  c++  java
  • Memory Control<HDU 2871>

    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
    New5
    New 2
    New 2
    Free 3
    Get 1
    Get 2
    Get 3
    Free 3
    Reset
     Sample Output
    New at 1
    RejectNew
    New at 3
    New at 5
    Free from 3 to 4
    Get at 1
    Get at 5
    Reject Get
    Reject Free
    Reset Now

    题目大意:

    有n个内存m种操作;

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

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

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

    Reset:释放所有内存清除;

    #include <bits/stdc++.h>
    #define N 50005
    #define INF 0x3f3f3f3f
    using namespace std;
    int read(){
        int x=0,f=1;char ch=getchar();
        while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
        while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
        return f*x;
    }
    typedef struct node{
        int l,r,l0,r0,l1,r1,len,flag,keyl,keyr,count;
    }node;
    node d[N<<2];
    int n,m;
    void push(int rt){
        if(d[rt].flag>0){
            d[rt<<1].l0=0;d[rt<<1].r0=0;d[rt<<1].l1=(d[rt<<1].r-d[rt<<1].l+1);d[rt<<1].r1=(d[rt<<1].r-d[rt<<1].l+1);d[rt<<1].len=0;
            d[rt<<1|1].l0=0;d[rt<<1|1].r0=0;d[rt<<1|1].l1=(d[rt<<1|1].r-d[rt<<1|1].l+1);d[rt<<1|1].r1=(d[rt<<1|1].r-d[rt<<1|1].l+1);d[rt<<1|1].len=0;
            d[rt<<1].keyl=d[rt<<1].keyr=d[rt<<1|1].keyl=d[rt<<1|1].keyr=d[rt].flag;
            d[rt<<1].count=d[rt<<1|1].count=1;
            d[rt<<1].flag=d[rt].flag;d[rt<<1|1].flag=d[rt].flag;d[rt].flag=0;
        }
        else if(d[rt].flag==-1){
            d[rt<<1].l1=0;d[rt<<1].r1=0;d[rt<<1].l0=(d[rt<<1].r-d[rt<<1].l+1);d[rt<<1].r0=(d[rt<<1].r-d[rt<<1].l+1);d[rt<<1].len=(d[rt<<1].r-d[rt<<1].l+1);
            d[rt<<1|1].l1=0;d[rt<<1|1].r1=0;d[rt<<1|1].l0=(d[rt<<1|1].r-d[rt<<1|1].l+1);d[rt<<1|1].r0=(d[rt<<1|1].r-d[rt<<1|1].l+1);d[rt<<1|1].len=(d[rt<<1|1].r-d[rt<<1|1].l+1);
            d[rt<<1].keyl=d[rt<<1].keyr=d[rt<<1|1].keyl=d[rt<<1|1].keyr=0;
            d[rt<<1].count=d[rt<<1|1].count=0;        
            d[rt<<1].flag=-1;d[rt<<1|1].flag=-1;d[rt].flag=0;
        }
    }
    void up(int x){
        d[x].len=max(d[x<<1].len,d[x<<1|1].len);
        d[x].len=max(d[x].len,d[x<<1].r0+d[x<<1|1].l0);
        d[x].keyl=d[x<<1].keyl;d[x].keyr=d[x<<1|1].keyr;
        if(d[x<<1].keyr==d[x<<1|1].keyl&&d[x<<1].keyr!=0){
            d[x].count=d[x<<1].count+d[x<<1|1].count-1;
        }
        else d[x].count=d[x<<1].count+d[x<<1|1].count;
        if(d[x<<1].l0==d[x<<1].r-d[x<<1].l+1) d[x].l0=d[x<<1].l0+d[x<<1|1].l0;
        else d[x].l0=d[x<<1].l0;
        if(d[x<<1|1].r0==d[x<<1|1].r-d[x<<1|1].l+1) d[x].r0=d[x<<1|1].r0+d[x<<1].r0;
        else d[x].r0=d[x<<1|1].r0;
        if(d[x<<1].keyr==d[x<<1|1].keyl&&d[x<<1].l1==d[x<<1].r-d[x<<1].l+1) d[x].l1=d[x<<1].l1+d[x<<1|1].l1;
        else d[x].l1=d[x<<1].l1;
        if(d[x<<1].keyr==d[x<<1|1].keyl&&d[x<<1|1].r1==d[x<<1|1].r-d[x<<1|1].l+1) d[x].r1=d[x<<1|1].r1+d[x<<1].r1;
        else d[x].r1=d[x<<1|1].r1;    
    }
    void built(int root,int l,int r){
        if(l==r){
            d[root].l=d[root].r=l;d[root].keyl=d[root].keyr=0;d[root].count=0;
            d[root].len=d[root].l0=d[root].r0=1;d[root].flag=d[root].l1=d[root].r1=0;
            return ;
        }
        int mid=(l+r)>>1;
        built(root<<1,l,mid);
        built(root<<1|1,mid+1,r);
        d[root].l=d[root<<1].l;d[root].r=d[root<<1|1].r;d[root].flag=0;d[root].keyl=d[root].keyr=0;
        d[root].count=0;
        up(root);
    }
    void update(int root,int l,int r,int t){
        if(l<=d[root].l&&d[root].r<=r){
            if(t>0){
                d[root].keyl=d[root].keyr=t;d[root].count=1;
                d[root].len=d[root].l0=d[root].r0=0;d[root].l1=d[root].r1=d[root].r-d[root].l+1;d[root].flag=t;
            }
            else{
                d[root].keyl=d[root].keyr=0;d[root].count=0;
                d[root].l1=d[root].r1=0;d[root].len=d[root].l0=d[root].r0=d[root].r-d[root].l+1;d[root].flag=-1;
            }
            return ;
        }
        push(root);
        int mid=(d[root].l+d[root].r)>>1;
        if(l<=mid) update(root<<1,l,r,t);
        if(r>mid) update(root<<1|1,l,r,t);
        up(root);
    }
    int ans1;
    void querty_1(int root,int t){
        if(d[root].l==d[root].r){
            ans1=d[root].l;
            return ;
        }
        push(root);
    //    cout<<d[root].len<<" "<<d[root<<1].len<<" "<<d[root].l<<" "<<d[root].r<<endl;
        if(d[root<<1].len>=t) querty_1(root<<1,t);
        else if(d[root<<1].r0+d[root<<1|1].l0>=t){
            ans1=min(d[root<<1].r-d[root<<1].r0+1,ans1);
            return ;
        }
        else querty_1(root<<1|1,t);
        up(root);
    }
    int ans2,ans3;int fpp;
    bool flag1,flag2;
    void querty_2(int root,int t){
        if(d[root].l==d[root].r){
            if(d[root].keyl==0){
                flag1=flag2=1;
            }
            else{
                fpp=d[root].keyl;ans2=0;ans3=0;
            }
            return ;
        }
        push(root);
        int mid=(d[root].l+d[root].r)>>1;
        if(t<=mid) querty_2(root<<1,t);
        else querty_2(root<<1|1,t);
        up(root);
        //cout<<flag2<<" "<<d[root<<1].keyr<<" "<<d[root<<1|1].keyl<<" "<<d[root<<1].l<<" "<<d[root<<1].r<<" "<<ans2<<" "<<ans3<<endl;
        if((d[root<<1].l<=t)&&(d[root<<1].r>=t)){
        if(flag2==0&&d[root<<1].keyr==d[root<<1|1].keyl){
            ans3+=d[root<<1|1].l1;
            //cout<<"sb"<<endl;
            if(d[root<<1|1].l1!=d[root<<1|1].r-d[root<<1|1].l+1) flag2=1;
        }
        else flag2=1;}    
        else{
        if(flag1==0&&d[root<<1].keyr==d[root<<1|1].keyl){
            ans2+=d[root<<1].r1;
            if(d[root<<1].r1!=d[root<<1].r-d[root<<1].l+1) flag1=1; 
        }
        else flag1=1;}
    }
    void querty_3(int root,int t){
        if(d[root].l==d[root].r){
            ans2=d[root].l;
            return ;
        }
        push(root);
        int mid=(d[root].l+d[root].r)>>1;
    //    cout<<d[root].l<<" "<<d[root].r<<" "<<d[root<<1].count<<endl;
        if(d[root<<1].count>=t) querty_3(root<<1,t);
        else{
            if(d[root<<1].keyr==d[root<<1|1].keyl&&d[root<<1].keyr!=0) t++;
            querty_3(root<<1|1,t-d[root<<1].count);
        }
        up(root);
    }
    int main(){
        ios::sync_with_stdio(false);
        //freopen("test.in","r",stdin);
        while(scanf("%d%d",&n,&m)==2){
            built(1,1,n);char str[20];
            int t;int cnt=0;
            while(m--){
                scanf(" %s",str);
                if(str[0]=='R'){
                    update(1,1,n,-1);
                    printf("Reset Now
    ");
                }
                else if(str[0]=='N'){
                    t=read();
                    ans1=INF;
                    if(d[1].len<t) printf("Reject New
    ");
                    else{
                        querty_1(1,t);cnt++;
                        printf("New at %d
    ",ans1);
                        update(1,ans1,ans1+t-1,cnt);
                    }
                }
                else if(str[0]=='F'){
                    t=read();
                    flag1=flag2=0;ans2=ans3=-1;
                    querty_2(1,t);
                    if(ans2==-1) printf("Reject Free
    ");
                    else{
                        printf("Free from %d to %d
    ",t-ans2,t+ans3);
                        update(1,t-ans2,t+ans3,-1);
                    }
                }
                else if(str[0]=='G'){
                    t=read();        
                    if(t>d[1].count) printf("Reject Get
    ");
                    else{
                        querty_3(1,t);
                        printf("Get at %d
    ",ans2);
                    }
                }
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    LeetCode100——same tree
    Stl——Vector.erase()用法
    xor异或逻辑运算
    爬楼梯问题——迭代or递归
    简单博弈论取石子
    纪念我的leetcode开门之旅
    JiuDuOj——1049
    [Codeforces 872]比赛记录
    [BZOJ 4563]放棋子
    10.14
  • 原文地址:https://www.cnblogs.com/wang9897/p/8387889.html
Copyright © 2011-2022 走看看