hdu 2871 Memory Control(成段更新,区间合并)

题意:给n个单位内存(编号从1开始),有几种操作:Reset把所有的内存清空,然后输出“Reset Now”。New x申请一块长度为x的内存块(如多解,左边优先),然后输出“New at A”。Free x把包含第x块单位内存的内存块清除,然后输出“Free from A to B”。Get x返回第x块内存块的起始内存单位编号,然后输出 “Get at A”。

        大部分操作和POJ 3667 HOTEL一样.对于get操作,我把之前new操作得到的内存块的起点都放到了set,然后get的时候,暴力跑.

/*代码风格更新后*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;

#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
#define MID(a,b) (a+((b-a)>>1))
const int N=50005;

struct node
{
	int lft,rht;
	int lmx,rmx,mx,flag;
	int len(){return rht-lft+1;}
	int mid(){return MID(lft,rht);}
	void init(){lmx=rmx=mx=len();}
	void fun(int valu)
	{
	    flag=valu;		
		if(valu>0) lmx=rmx=mx=0;
		else lmx=rmx=mx=len();
	}
};

int n,m,st[N],ed[N];
set<int> adj;
set<int>::iterator it;

struct Segtree
{
	node tree[N*4];
	void down(int ind)
	{
		if(tree[ind].flag)
		{
			tree[LL(ind)].fun(tree[ind].flag);
			tree[RR(ind)].fun(tree[ind].flag);
			tree[ind].flag=0;
		}
	}
	void up(node &a,node &b,node &c)
	{
		a.lmx=b.lmx;	a.rmx=c.rmx;
		a.mx=max(b.rmx+c.lmx,max(b.mx,c.mx));
		if(b.len()==b.lmx) a.lmx+=c.lmx;
		if(c.len()==c.rmx) a.rmx+=b.rmx;
	}
	void build(int lft,int rht,int ind)
	{
		tree[ind].lft=lft;	tree[ind].rht=rht;
		tree[ind].flag=0;	tree[ind].init();
		if(lft==rht) tree[ind].flag=-1;
		else
		{
			int mid=tree[ind].mid();
			build(lft,mid,LL(ind));
			build(mid+1,rht,RR(ind));
		}
	}
	void updata(int st,int ed,int ind,int valu)
	{
		int lft=tree[ind].lft,rht=tree[ind].rht;
		if(st<=lft&&rht<=ed) tree[ind].fun(valu);
		else
		{
			down(ind);
			int mid=tree[ind].mid();
			if(st<=mid) updata(st,ed,LL(ind),valu);
			if(ed> mid) updata(st,ed,RR(ind),valu);
			up(tree[ind],tree[LL(ind)],tree[RR(ind)]);
		}
	}
	int getID(int pos,int ind)
	{
		if(tree[ind].flag) return tree[ind].flag;
		else
		{//这里可以不用判断延迟操作,因而也不用合并区间
			int mid=tree[ind].mid();
			if(pos<=mid) return getID(pos,LL(ind));
			else return getID(pos,RR(ind));
		}
	}
	int query(int valu,int ind)
	{
		if(tree[ind].lft==tree[ind].rht) return tree[ind].lft;
		else
		{
			int pos;
			down(ind);
			if(tree[LL(ind)].mx>=valu) pos=query(valu,LL(ind));
			else if(tree[LL(ind)].rmx+tree[RR(ind)].lmx>=valu)
				pos=tree[LL(ind)].rht-tree[LL(ind)].rmx+1;
			else pos=query(valu,RR(ind));
			up(tree[ind],tree[LL(ind)],tree[RR(ind)]);
			return pos;
		}
	}
}seg;
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		adj.clear();
		seg.build(1,n,1);

		int a;
		char cmd[10];
		for(int i=1;i<=m;i++)
		{
			scanf("%s",cmd);
			if(strcmp(cmd,"Reset")==0)
			{
				adj.clear();
				seg.updata(1,n,1,-1);
				puts("Reset Now");
			}
			else
			{
				scanf("%d",&a);
				if(strcmp(cmd,"New")==0)
				{
					if(seg.tree[1].mx<a) puts("Reject New");
					else
					{
						st[i]=seg.query(a,1);
						seg.updata(st[i],ed[i]=st[i]+a-1,1,i);
						adj.insert(st[i]);
						printf("New at %d\n",st[i]);
					}
				}
				else if(strcmp(cmd,"Free")==0)
				{
					int id=seg.getID(a,1);
					if(id==-1) puts("Reject Free");
					else
					{
						adj.erase(st[id]);
						seg.updata(st[id],ed[id],1,-1);
						printf("Free from %d to %d\n",st[id],ed[id]);
					}
				}
				else
				{
					if(a>(int)adj.size()) puts("Reject Get");
					else
					{
						a--; it=adj.begin();
						while(a--) it++;
						printf("Get at %d\n",*it);
					}
				}
			}
		}
		puts("");
	}
	return 0;
}


/*代码风格更新前*/
#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
using namespace std;
const int N=50005;
struct XD
{
    int be,end;
    XD(){be=0;end=0;};
    XD(int a,int b){be=a;end=b;}
};
struct node
{
    int left,right,co;
    int lmax,rmax,mmax;
    int mid(){return left+(right-left)/2;}
    int dis(){return right-left+1;}
    void change(int r)
    {
        co=r;
        if(r!=0) {lmax=rmax=mmax=0;}
        else {lmax=rmax=mmax=dis();}
    }
};
void unin(node &a,node &b,node &c)
{
    a.lmax=b.lmax;  a.rmax=c.rmax;
    a.mmax=max(b.rmax+c.lmax,max(b.mmax,c.mmax));
    if(b.dis()==b.lmax) a.lmax+=c.lmax;
    if(c.dis()==c.rmax) a.rmax+=b.rmax;
}
struct Segtree
{
    node tree[N*4];
    void build(int left,int right,int r)
    {
        tree[r].left=left;  tree[r].right=right;
        tree[r].lmax=tree[r].rmax=tree[r].mmax=tree[r].dis();
        tree[r].co=0;
        if(left<right)
        {
            int mid=tree[r].mid();
            build(left,mid,r*2);
            build(mid+1,right,r*2+1);
        }
    }
    void updata(int be,int end,int r,int co)
    {
        if(be<=tree[r].left&&tree[r].right<=end)
        {
            tree[r].change(co);
        }
        else
        {
            if(tree[r].co!=-1)
            {
                tree[r*2].change(tree[r].co);   tree[r*2+1].change(tree[r].co);
                tree[r].co=-1;
            }
            int mid=tree[r].mid();
            if(be<=mid) updata(be,end,r*2,co);
            if(end>mid) updata(be,end,r*2+1,co);
            unin(tree[r],tree[r*2],tree[r*2+1]);
        }
    }
    int query(int len,int r)
    {
        if(tree[r].lmax>=len) return tree[r].left;
        else
        {
            if(tree[r*2].mmax>=len) return query(len,r*2);
            else if(tree[r*2].rmax+tree[r*2+1].lmax>=len) return tree[r*2].right-tree[r*2].rmax+1;
            else return query(len,r*2+1);
        }
    }
    int check(int pos,int r)
    {
        if(tree[r].co!=-1) return tree[r].co;
        else
        {
            if(tree[r].co!=-1)
            {
                tree[r*2].change(tree[r].co);   tree[r*2+1].change(tree[r].co);
                tree[r].co=-1;
            }
            int mid=tree[r].mid();
            if(pos<=mid) return check(pos,r*2);
            else return check(pos,r*2+1);
        }
    }
}seg;
int main()
{
    int n,m;
    map<int,XD> adj;
    map<int,XD>::iterator it;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int a;
        char cmd[10];
        adj.clear();

        seg.build(1,n,1);
        for(int i=0;i<m;i++)
        {
            scanf("%s",cmd);
            if(strcmp(cmd,"Reset")==0)
            {
                adj.clear();
                seg.updata(1,n,1,0);
                puts("Reset Now");
            }
            else
            {
                scanf("%d",&a);
                if(strcmp(cmd,"New")==0)
                {
                    if(seg.tree[1].mmax<a)  puts("Reject New");
                    else
                    {
                        int pos=seg.query(a,1);
                        seg.updata(pos,pos+a-1,1,pos);
                        adj.insert(pair<int,XD>(pos,XD(pos,pos+a-1)));
                        printf("New at %d\n",pos);
                    }
                }
                else if(strcmp(cmd,"Get")==0)
                {
                    if(a>adj.size())puts("Reject Get");
                    else 
                    {
                        int cnt=1;
                        for(it=adj.begin();it!=adj.end();it++)
                        {
                            if(cnt==a) break;
                            cnt++;
                        }
                        printf("Get at %d\n",it->first);
                    }
                }
                else
                {
                    int pos=seg.check(a,1);
                    if(pos==0) puts("Reject Free");
                    else
                    {
                        int be=adj[pos].be,end=adj[pos].end;
                        adj.erase(pos);
                        seg.updata(be,end,1,0);
                        printf("Free from %d to %d\n",be,end);
                    }
                }
            }
        }
        puts("");
    }
    return 0;
}



你可能感兴趣的:(struct,tree,cmd,iterator,query,Build)