HDU2871--Memory Control

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

 

其他处理都比较好做。那个FREE比较难搞。。不过我一次编译将其他功能都实现了^_^不是题解~

/*
清除所有内存
新开X个内存,连续左边最小的位置,这里就是HOTEL的问题
(len,rlen,llen,l,r)这里其实是query
释放包括X的内存块,感觉这里用递归能实现
返回第X个内存快的起点,这个要怎么建树?mid?mid+1,感觉这里就是区间合并的问题
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#define maxn 50008
#define lson 2*id,l,mid
#define rson 2*id+1,mid+1,r
bool flag;
inline int max(int a,int b,int c)
{
	if(a<b) a=b;
	if(a<c) a=c;
	return a;
}
struct ST
{
	int l,r,len,rlen,llen;//len是指连续空余的内存
	int key,rkey,lkey;//key是指连续占用的内存
	int line,rcov,lcov;//区间个数,左右覆盖情况
	int set;//懒惰标记。
}st[4*maxn];
void buildtree(int id,int l,int r)
{
	st[id].l=l;
	st[id].r=r;
	if(l==r)
	{
		st[id].len=st[id].rlen=st[id].llen=1;
		st[id].line=st[id].rcov=st[id].lcov=0;
		st[id].key=st[id].rkey=st[id].lkey=0;
		return;
	}
	int mid=(l+r)>>1;
	buildtree(lson);
	buildtree(rson);
	st[id].len=st[id].rlen=st[id].llen=r-l+1;
	st[id].line=st[id].rcov=st[id].lcov=0;
	st[id].key=st[id].rkey=st[id].lkey=0;
}
void PushDown(int id)
{
	if(st[id].set!=-1)
	{
		st[2*id].set=st[2*id+1].set=st[id].set;
		if(st[id].set)
		{
			st[2*id].len=st[2*id].rlen=st[2*id].llen=0;
			st[2*id].line=st[2*id].lcov=st[2*id].rcov=1;
			st[2*id].key=st[2*id].lkey=st[2*id].rkey=st[2*id].r-st[2*id].l+1;
			st[2*id+1].len=st[2*id+1].rlen=st[2*id+1].llen=0;
			st[2*id+1].line=st[2*id+1].lcov=st[2*id+1].rcov=1;
			st[2*id+1].key=st[2*id+1].lkey=st[2*id+1].rkey=st[2*id+1].r-st[2*id+1].l+1;
		}
		else
		{
			st[2*id].len=st[2*id].rlen=st[2*id].llen=st[2*id].r-st[2*id].l+1;
			st[2*id].line=st[2*id].lcov=st[2*id].rcov=0;
			st[2*id].key=st[2*id].rkey=st[2*id].lkey=0;
			st[2*id+1].len=st[2*id+1].rlen=st[2*id+1].llen=st[2*id+1].r-st[2*id+1].l+1;
			st[2*id+1].line=st[2*id+1].lcov=st[2*id+1].rcov=0;
			st[2*id+1].key=st[2*id+1].lkey=st[2*id+1].rkey=0;
		}
		st[id].set=-1;
	}
}
void PushUp(int id)
{
	st[id].len=max(st[2*id].len,st[2*id+1].len,st[2*id].rlen+st[2*id+1].llen);
	st[id].rlen=(st[2*id+1].rlen==st[2*id+1].r-st[2*id+1].l+1?st[2*id].rlen+st[2*id+1].len:st[2*id+1].rlen);
	st[id].llen=(st[2*id].llen==st[2*id].r-st[2*id].l+1?st[2*id].len+st[2*id+1].llen:st[2*id].llen);
	st[id].key=max(st[2*id].key,st[2*id+1].key,st[2*id].rkey+st[2*id+1].lkey);
	st[id].rkey=(st[2*id+1].rkey==st[2*id+1].r-st[2*id+1].l+1?st[2*id].rkey+st[2*id+1].key:st[2*id+1].rkey);
	st[id].lkey=(st[2*id].lkey==st[2*id].r-st[2*id].l+1?st[2*id].key+st[2*id+1].lkey:st[2*id].lkey);
	st[id].lcov=st[2*id].lcov;
	st[id].rcov=st[2*id+1].rcov;
	st[id].line=st[2*id].line+st[2*id+1].line-(st[2*id].rcov && st[2*id+1].lcov);
}
void update(int id,int l,int r,int ope)//这里用来实现区间置0和区间置1
{
	if(st[id].l == l && st[id].r == r)
	{
		st[id].set = ope;
		if(ope)
		{
			st[id].len = st[id].llen = st[id].rlen = 0;
			st[id].key = st[id].lkey = st[id].rkey = r-l+1;
			st[id].line = st[id].rcov = st[id].lcov = 1;
		}
		else 
		{
			st[id].len = st[id].llen = st[id].rlen = r-l+1;
			st[id].key = st[id].lkey = st[id].rkey = 0;
			st[id].line = st[id].rcov = st[id].lcov = 0;
		}
		return;
	}
	PushDown(id);
	if(st[2*id].r >= r)
	{
		update(2*id,l,r,ope);
		PushUp(id);
		return;
	}
	if(st[2*id+1].l <= l)
	{
		update(2*id+1,l,r,ope);
		PushUp(id);
		return;
	}
	update(2*id,l,st[2*id].r,ope);
	update(2*id+1,st[2*id+1].l,r,ope);
	PushUp(id);
}
int query(int id,int l,int r,int cost)//这里实现了第二个任务的主体,其实只要等下多个clear就行
{
	if(l==r) return l;
	PushDown(id);
	if(st[2*id].len >= cost)
	{
		return query(2*id,l,st[2*id].r,cost);
	}
	else if(st[2*id].rlen + st[2*id+1].llen >= cost)
	{
		return st[2*id].r-st[2*id].rlen+1;
	}
	else return query(2*id+1,l,r,cost);
}
//接下要完成一个任务,释放包括X的内存块,用递归?
void clearx(int id,int l,int r)
{
	if(st[id].l==l && st[id].r==r)
	{
		if(l==r && st[id].set==0)
		{
			flag=false;return;
		}
		if(id&1)
		{
			if(st[id/2].rkey > r-l+1)
			{
				update(1,l,r,0);
				clearx(id/2,r+1-st[id/2].rkey,l-1);
			}
			if(st[id/2+1].lcov)
			{
				update(1,l,r,0);
				clearx(id/2+1,st[id/2+1].l,st[id/2+1].l+st[id/2+1].lkey-1);
			}
		}
		else
		{
			if(st[id/2].rkey > r-l+1)
			{
				update(1,l,r,0);
				clearx(id/2,r+1-st[id/2].rkey,l-1);
			}
			if(st[id/2-1].rcov)
			{
				update(1,l,r,0);
				clearx(id/2-1,st[id/2-1].r-st[id/2-1].rkey+1,st[id/2-1].r);
			}
		}
		update(1,l,r,0);
		return;
	}
	PushDown(id);
	if(st[2*id].r >= r)
	{
		clearx(2*id,l,r);
		PushUp(id);
	}
	else if(st[2*id+1].l <= l)
	{
		clearx(2*id+1,l,r);
		PushUp(id);
	}
	else 
	{
		clearx(2*id,l,st[2*id].r);
		clearx(2*id+1,st[2*id+1].l,r);
		PushUp(id);
	}
}		
int queryx(int id,int l,int r,int x)
{
	if(l==r) return l;
	PushDown(id);
	if(st[2*id].line >= x)
	{
		return query(2*id,l,st[2*id].r,x);
	}
	else
	{
		if(st[2*id].rcov && st[2*id+1].lcov) x++;
		return query(2*id+1,st[2*id+1].l,r,x-st[2*id].line);
	}
}
int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)==2)
	{
		buildtree(1,1,n);
		char str[10];int pos;
		for(int i=1;i<=m;i++)
		{
			scanf("%s%d",str,&pos);
			if(str[0]=='N')
			{
				if(st[1].len >= pos)
				{
					int fuck=query(1,1,n,pos);
					printf("New at %d\n",fuck);
					update(1,fuck,fuck+pos-1,1);
				}
				else printf("Reject New\n");
			}
			if(str[0]=='R')
			{
				update(1,1,n,0);
				printf("Reset now\n");
			}
			if(str[0]=='G')
			{
				if(st[1].line >= pos)
				printf("Get at %d\n",queryx(1,1,n,pos));
				else printf("Reject Get\n");
			}
			if(str[0]=='F')
			{
				flag=true;
				clearx(1,pos,pos);
				if(!flag)printf("Reject Free\n");
				else
				{
					printf("hehe\n");
				}
			}
		}
	}
	return 0;
}


已经AC:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 500080
#define lson id<<1,l,mid
#define rson id<<1|1,mid+1,r
#define lson id<<1,l,mid
#define rson id<<1|1,mid+1,r
#define LL long long int
LL key[maxn];
int s,t;
struct ST
{
	int l,r,a,b,left,right;
	LL maxsum,sum,lsum,rsum;
}st[maxn<<2];

LL max (LL a,LL b)
{
	return a>b?a:b;
}

void PushUp(int id)
{
	st[id].left = st[id<<1].left;
	st[id].right = st[id<<1|1].right;
	st[id].lsum = st[id<<1].lsum;
	st[id].rsum = st[id<<1|1].rsum;
	st[id].sum = st[id<<1].sum + st[id<<1|1].sum;
	LL ans1 = st[id<<1].maxsum,ans2 = st[id<<1|1].maxsum;
	if(ans1 >= ans2)
	{
		st[id].maxsum = st[id<<1].maxsum;
		st[id].a = st[id<<1].a;
		st[id].b = st[id<<1].b;
	}
	else 
	{
		st[id].maxsum = st[id<<1|1].maxsum;
		st[id].a = st[id<<1|1].a;
		st[id].b = st[id<<1|1].b;
	}
	//lsum,rsum,maxsum,a,b,left,right都有可能更改
	//lsum
	if(st[id].lsum == st[id<<1].sum && st[id<<1|1].lsum > 0)
	{
		st[id].lsum = st[id].lsum + st[id<<1|1].lsum;
		st[id].left = st[id<<1|1].left;
	}
	//rsum
	if(st[id].rsum == st[id<<1|1].sum && st[id<<1].rsum >= 0)
	{
		st[id].rsum = st[id].rsum + st[id<<1].rsum;
		st[id].right = st[id<<1].right;
	}
	//maxsum
	if((st[id<<1].rsum + st[id<<1|1].lsum > st[id].maxsum) || 
	  (st[id<<1].right < st[id].a && st[id<<1].rsum + st[id<<1|1].lsum == st[id].maxsum))
	{
		st[id].maxsum = st[id<<1].rsum + st[id<<1|1].lsum;
		st[id].a = st[id<<1].right;
		st[id].b = st[id<<1|1].left;
	}
}

void buildtree(int id,int l,int r)
{
	st[id].l = l,st[id].r = r;
	if(l == r)
	{
		st[id].sum = st[id].lsum = st[id].rsum = st[id].maxsum = key[l];
		st[id].left = st[id].right = st[id].a = st[id].b = l;
		return;
	}
	int mid = (l + r) >> 1;
	buildtree(lson);
	buildtree(rson);
	PushUp(id);
}

LL query(int id,int l,int r)
{
	if(st[id].l == l && st[id].r == r)
	{
		s = st[id].a;
		t = st[id].b;
		return st[id].maxsum;
	}
	if(st[id<<1].r >= r)
	{
		return query(id<<1,l,r);
	}
	else if(st[id<<1|1].l <= l)
	{
		return query(id<<1|1,l,r);
	}
	else 
	{
		LL ans1 = query(id<<1,l,st[id<<1].r);
		LL ans2 = query(id<<1|1,st[id<<1|1].l,r);
		if(ans1 >= ans2)	query(id<<1,l,st[id<<1].r);
		LL ans = max(ans1,ans2);
		if((st[id<<1].rsum + st[id<<1|1].lsum > ans) || 
		   (st[id<<1].rsum + st[id<<1|1].lsum == ans && st[id<<1].right < s))
		{
			s = st[id<<1].right;
			t = st[id<<1|1].left;
		}
		ans = max(ans,st[id<<1].rsum + st[id<<1|1].lsum);
		return ans;
	}
}

int main()
{
	//freopen("in.txt","r",stdin);
	int n,m,cas = 0;
	while(scanf("%d%d",&n,&m)==2)
	{
		cas++;
		for(int i = 1;i <= n;i++)	scanf("%lld",&key[i]);
		buildtree(1,1,n);
		printf("Case %d:\n",cas);
		while(m--)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			query(1,u,v);
			printf("%d %d\n",s,t);
		}
	}
	return 0;
}


 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值