HDU-2871 Memory Control(线段树)
来源:互联网 发布:长相忆五色石南叶 知乎 编辑:程序博客网 时间:2024/05/29 19:26
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.
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刚学线段树就遇上这么难的题,前后大概用了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;}
0 0
- HDU 2871 Memory Control(线段树)
- HDU-2871 Memory Control(线段树)
- hdu 2871 Memory Control 线段树
- 线段树 HDU 2871 memory control
- hdu 2871 vector + 线段树 Memory Control
- 线段树 hdu 2871 memory control
- hdu 2871 Memory Control (线段树&vector)
- hdu 2871 Memory Control(线段树)
- HDU 2871 Memory Control 线段树
- hdu 2871 Memory Control 线段树
- hdu 2871 Memory Control(线段树)
- hdu 2871 -Memory Control--线段树
- HDU 2871 Memory Control(线段树区间合并+二分)
- HDU 2871 Memory Control(线段树:区间合并)
- HDU 2871 Memory Control (线段树,区间合并)
- HDU 2871 Memory Control(线段树+区间合并+vector)
- HDU 2871 Memory Control (线段树神题)
- HDU 2871 Memory Control 线段树(区间合并)+二分查找+vector的常用方法
- 浏览器的工作原理-介绍篇
- 链表中的倒数第k个结点
- opengl之vsh、fsh简易介绍+cocos2dx 3.0 shader 变灰
- 同一端口如何区分不同的Socket
- 李括号定义解说
- HDU-2871 Memory Control(线段树)
- C/C++——程序的内存分配
- comparetor和comparable的区别
- jsp自定义标签
- OJ-虚函数
- 信息论的知识
- [设计模式学习笔记]FACADE外观模式
- Android开发_Fragment
- leetcode 88 Merge Sorted Array