您的位置:首页 > 其它

HDU-2871 Memory Control(线段树)

2015-06-24 22:35 239 查看


Memory Control

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

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

刚学线段树就遇上这么难的题,前后大概用了12h,也是看了别人的思路之后慢慢更正自己的各种方法才完成的
有几点需要注意:①Reset时要用update,不能用build,否则会TLE②数据读入时注意Reset后面没有数字(一开始就错在这,却一直没有检查到...时间全浪费在这,还把代码改了好多)
#include <iostream>
#include <cstring>
#include <cstdio>
#define lson (i<<1)
#define rson (i<<1|1)
using namespace std;

const int MAXN = 100005;
int n,m,tmp,l,r,k,index;
int LL,RR;

struct tree {
int l,r,len,s,e,cnt,lm,rm,m,lazy;
}tr[MAXN<<4];

inline int MyMax(int a,int b) {
return a>b?a:b;
}

inline void pushUp(int i) {
tr[i].lm=tr[LL=lson].lm;
tr[i].rm=tr[RR=rson].rm;
if(tr[i].lm==tr[LL].len)
tr[i].lm+=tr[RR].lm;
if(tr[i].rm==tr[RR].len)
tr[i].rm+=tr[LL].rm;
tr[i].m=MyMax(MyMax(tr[LL].m,tr[RR].m),tr[LL].rm+tr[RR].lm);
}

inline void pushDown(int i) {
if(tr[i].lazy!=-1&&tr[i].len!=1) {
tr[LL=lson].s=tr[RR=rson].s=tr[i].s;
tr[LL].e=tr[RR].e=tr[i].e;
tr[LL].cnt=tr[i].cnt;
tr[RR].cnt=0;
if(tr[i].lazy==1)
tr[LL].lm=tr[LL].rm=tr[LL].m=tr[RR].lm=tr[RR].rm=tr[RR].m=0;
else {
tr[LL].lm=tr[LL].rm=tr[LL].m=tr[LL].len;
tr[RR].lm=tr[RR].rm=tr[RR].m=tr[RR].len;
}
tr[LL].lazy=tr[RR].lazy=tr[i].lazy;
tr[i].lazy=-1;
}
}

void build(int i,int l,int r) {
tr[i].l=l;
tr[i].r=r;
tr[i].len=r-l+1;
tr[i].lazy=-1;
tr[i].s=tr[i].e=tr[i].cnt=0;
if(l==r) {
tr[i].lm=tr[i].rm=tr[i].m=1;
tr[lson].lm=tr[lson].rm=tr[lson].m=tr[rson].lm=tr[rson].rm=tr[rson].m=0;
return ;
}
int mid=(l+r)>>1;
build(lson,l,mid);
build(rson,mid+1,r);
pushUp(i);
}

void update(int i) {
if(l<=tr[i].l&&tr[i].r<=r) {
if(k==1) {
tr[i].s=l;
tr[i].e=r;
tr[i].cnt=tr[i].l>l?0:1;
tr[i].lm=tr[i].rm=tr[i].m=0;
}
else {
tr[i].s=0;
tr[i].e=0;
tr[i].cnt=0;
tr[i].lm=tr[i].rm=tr[i].m=tr[i].len;
}
tr[i].lazy=k;
return ;
}
pushDown(i);
int mid=tr[lson].r;
if(l<=mid)
update(lson);
if(mid<r)
update(rson);
pushUp(i);
tr[i].cnt=tr[lson].cnt+tr[rson].cnt;
}

void New(int i) {
if((tmp=tr[i].r-tr[i].rm)<k) {
l=tmp+1;
return ;
}
pushDown(lson);
if(tr[lson].m>=k) {
New(lson);
return ;
}
pushDown(rson);
if(tr[lson].rm+tr[rson].lm>=k) {
l=tr[lson].r-tr[lson].rm+1;
return ;
}
New(rson);
}

void Get(int i,int x) {
if(tr[i].l==tr[i].r) {
l=tr[i].r;
return ;
}
pushDown(lson);
if(tr[lson].cnt>=x) {
Get(lson,x);
return ;
}
pushDown(rson);
x-=tr[lson].cnt;
Get(rson,x);
}

void Free(int i) {
if(tr[i].l==tr[i].r) {
index=i;
return ;
}

int mid=tr[lson].r;
if(k<=mid) {
pushDown(lson);
Free(lson);
}
else {
pushDown(rson);
Free(rson);
}
}

int main() {
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
char cmd[9];
while (2==scanf("%d%d",&n,&m)) {
build(1,1,n);
while(m--) {
scanf("%s",cmd);
if(cmd[0]=='R') {
k=0;
l=1;
r=n;
tr[1].s=tr[1].e=tr[1].cnt=tr[1].lazy=0;
tr[1].lm=tr[1].rm=tr[1].m=n;
update(1);
printf("Reset Now\n");
}
else if(cmd[0]=='N') {
scanf("%d",&k);
pushDown(1);
if(tr[1].m<k)
printf("Reject New\n");
else {
New(1);
r=l+k-1;
k=1;
update(1);
printf("New at %d\n",l);
}
}
else if(cmd[0]=='F') {
scanf("%d",&k);
pushDown(1);
Free(1);
if(tr[index].s==0)
printf("Reject Free\n");
else {
l=tr[index].s;
r=tr[index].e;
k=0;
update(1);
printf("Free from %d to %d\n",l,r);
}
}
else {//Get x
scanf("%d",&k);
pushDown(1);
if(tr[1].cnt<k)
printf("Reject Get\n");
else {
Get(1,k);
printf("Get at %d\n",l);
}
}
}
printf("\n");
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: