HDU2871--Memory Control
来源:互联网 发布:电磁炉 网络专供 编辑:程序博客网 时间:2024/06/16 04:43
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.
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.
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.
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 10New 2New 5New 2New 2Free 3Get 1Get 2Get 3Free 3Reset
Sample Output
New at 1Reject NewNew at 3New at 5 Free from 3 to 4Get at 1Get at 5Reject GetReject FreeReset 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,rbool 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 intLL 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都有可能更改//lsumif(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;}//rsumif(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;}//maxsumif((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;}
- HDU2871--Memory Control
- HDU2871 Memory Control
- 线段树 hdu2871 Memory Control
- HDU2871:Memory Control(线段树区间合并)
- hdu2871 Memory Control 线段树,二分
- hdu2871 Memory Control 线段树区间合并+STL删除插入
- 区间合并 内纯释放问题 hdu2871 Memory control
- hdu2871 Memory Control--区间操作(待解决)
- memory control block system
- hdu 2871 Memory Control
- hdu 2871 Memory Control
- hdu 2871 Memory Control
- hdoj 2871 Memory Control
- HDU 2871 Memory Control
- Memory Cache Control 摘录
- HDU-2871-Memory Control
- hdu 2871 Memory Control
- HDU 2871 Memory Control
- Hive的预定义UDF函数
- c++创建文件目录
- 创业随想--从网站建设开始(8,把正面给别人,把背面留给自己)
- PM如何写好产品需求文档
- PHP foreach与引用赋值
- HDU2871--Memory Control
- 毕业设计之错误集锦(五)
- SQL2008修改表结构的时候弹出的提示. Saving changes is not permitted...
- NandFlash驱动移植基础知识
- MAC 和 LINUX 平台编译
- UITextView,UIWebView 直接显示html代码
- 获取客户需求的十大沟通技巧
- case when then end
- Linux常用命令