tjut 2871
来源:互联网 发布:东方财富炒股软件 编辑:程序博客网 时间:2024/04/30 18:11
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #define lson 2*i #define rson 2*i+1 #define lc l,mid,2*i #define rc mid+1,r,2*i+1 const int L = 50000+10; struct node { int start,end; int ls,rs,ms; int lazy,cnt,cover; } a[L<<2]; void pushdown(int mid,int L,int R,int i) { if(a[i].lazy!=-1) { a[lson].lazy = a[rson].lazy = a[i].lazy; a[lson].ms = a[lson].ls = a[lson].rs = (mid-L+1)*a[i].lazy; a[rson].ms = a[rson].ls = a[rson].rs = (R-mid)*a[i].lazy; a[lson].start = a[rson].start = a[i].start; a[lson].end = a[rson].end = a[i].end; a[i].lazy = -1; } } void pushup(int mid,int L,int R,int i) { a[i].ls = a[lson].ls; a[i].rs = a[rson].rs; if(a[i].ls == mid-L+1) a[i].ls+=a[rson].ls; if(a[i].rs == R-mid) a[i].rs+=a[lson].rs; a[i].ms = max(max(a[lson].ms,a[rson].ms),a[lson].rs+a[rson].ls); } void insert(int L,int R,int t,int l,int r,int i)//t为0释放该区间,为1则分配区间 { if(L<=l && r<=R) { a[i].lazy = t; a[i].ls = a[i].rs = a[i].ms = t*(r-l+1); if(t) a[i].start = a[i].end = -1; else { a[i].start = L; a[i].end = R; } } else { int mid = (l+r)>>1; pushdown(mid,l,r,i); if(L<=mid) insert(L,R,t,lc); if(R>mid) insert(L,R,t,rc); pushup(mid,l,r,i); } } void Reset(int n) { insert(1,n,1,1,n,1); a[1].cover = 1; a[1].cnt = 0; } int New(int x,int l,int r,int i) { if(l == r) return l; int mid = (l+r)>>1; pushdown(mid,l,r,i); if(a[lson].ms>=x) return New(x,lc); else if(a[lson].rs+a[rson].ls>=x) return mid-a[lson].rs+1; else return New(x,rc); } int Free(int x,int l,int r,int i) { if(l == r) return i; int mid = (l+r)>>1; pushdown(mid,l,r,i); if(x<=mid) return Free(x,lc); else return Free(x,rc); } void countup(int i) { a[i].cnt = a[lson].cnt+a[rson].cnt; } void countdown(int i) { if(a[i].cover) { a[lson].cnt = a[rson].cnt = 0; a[lson].cover = a[rson].cover = 1; a[i].cover = 0; } } int Get(int x,int l,int r,int i) { if(l == r) return l; else { int mid = (l+r)>>1; countdown(i); if(a[lson].cnt>=x) return Get(x,lc); else return Get(x-a[lson].cnt,rc); } } void count(int x,int t,int l,int r,int i) { if(l == r) a[i].cnt = t; else { int mid = (l+r)>>1; countdown(i); if(x<=mid) count(x,t,lc); else count(x,t,rc); countup(i); } } int main() { char s[20]; int n,m,x,ans; while(~scanf("%d%d",&n,&m)) { Reset(n);//初始状态既是内存全部释放的状态 while(m--) { scanf(" %s",s); if(!strcmp(s,"Reset")) { Reset(n); printf("Reset Now\n"); } else if(!strcmp(s,"New")) { scanf("%d",&x); if(a[1].ms>=x) { ans = New(x,1,n,1); printf("New at %d\n",ans); count(ans,1,1,n,1); insert(ans,ans+x-1,0,1,n,1); } else printf("Reject New\n"); } else if(!strcmp(s,"Free")) { scanf("%d",&x); ans = Free(x,1,n,1); if(a[ans].start<0) printf("Reject Free\n"); else { printf("Free from %d to %d\n",a[ans].start,a[ans].end); count(a[ans].start,0,1,n,1); insert(a[ans].start,a[ans].end,1,1,n,1); } } else { scanf("%d",&x); if(x>a[1].cnt) printf("Reject Get\n"); else printf("Get at %d\n",Get(x,1,n,1)); } } printf("\n"); } return 0; }
0 0
- tjut 2871
- tjut 2871
- tjut 5289
- tjut 5288
- tjut 5294
- tjut 2586
- tjut 5296
- tjut 5297
- tjut 5299
- tjut 5384
- tjut 5387
- tjut 5386
- tjut 5381
- tjut 5400
- tjut 5399
- tjut 5396
- tjut 5398
- tjut 5412
- 基于Android的移动图书馆管理系统
- 学习vueJs第一
- [LeetCode 136] Single Number
- Android全局异常捕获
- 倍数表达法用法归纳
- tjut 2871
- 孤儿进程与僵尸进程
- 1898: [Zjoi2005]Swamp 沼泽鳄鱼
- Android 跨进程通信(一)
- CUDA入门基本概念简介
- Tomcat-001-一个简单的web服务器
- Spring+Spring MVC+myBatis整合详细步骤
- Java开发揭开socket编程的面纱
- Java虚拟机(八)——Java内存模型