Memory Control
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4429 Accepted Submission(s): 1060
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 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
题意:
给出 N(1 ~ 50000), M(1 ~ 50000),代表有 N 个记忆块,M 个操作。有 4 类操作,New 说明要申请连续 ans 个的记忆块,Free 说明要解除包含 ans 这个数的记忆块,Get 说明要输出第 ans 个的记忆块的起始编号,Reject 说明要初始化所有的记忆。
思路:
线段树 + vector + 区间统计。New 操作就是区间合并操作,每次 New 完按起点由小到大顺序插入到 vector 中,Free 和 Get 操作都可以用 upper_bound 操作取得,Reject 则是 vector 的 clear 操作,因为是多组数据,所以每次开始前都要 clear 一次。否则会导致 TLE。
AC:
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; const int MAX = 50005 * 10; typedef struct { int s, e; } node; vector<node> G; int memory[MAX]; int lmax[MAX], rmax[MAX], mmax[MAX]; bool cmp (node a, node b) { return a.s < b.s; } void push_down(int node, int l, int r) { if (memory[node] != -1) { memory[node << 1] = memory[node]; memory[node << 1 | 1] = memory[node]; int mid = (r + l) >> 1; mmax[node << 1] = lmax[node << 1] = rmax[node << 1] = (memory[node] ? 0 : mid - l + 1); mmax[node << 1 | 1] = lmax[node << 1 | 1] = rmax[node << 1 | 1] = (memory[node] ? 0 : r - mid); memory[node] = -1; } } void push_up(int node, int l, int r) { int mid = (r + l) >> 1; lmax[node] = lmax[node << 1]; rmax[node] = rmax[node << 1 | 1]; if (lmax[node] == mid - l + 1) lmax[node] += lmax[node << 1 | 1]; if (rmax[node] == r - mid) rmax[node] += rmax[node << 1]; mmax[node] = max( rmax[node << 1] + lmax[node << 1 | 1], max(mmax[node << 1], mmax[node << 1 | 1]) ); } void build (int node, int l, int r) { if (l == r) { mmax[node] = lmax[node] = rmax[node] = 1; memory[node] = -1; } else { int mid = (r + l) >> 1; build(node << 1, l, mid); build(node << 1 | 1, mid + 1, r); memory[node] = -1; push_up(node, l, r); } } void updata (int node, int l, int r, int al, int ar, int s) { if (al > r || ar < l) return; if (al <= l && ar >= r) { memory[node] = s; mmax[node] = lmax[node] = rmax[node] = (s ? 0 : r - l + 1); return; } push_down(node, l, r); int mid = (r + l) >> 1; if (al <= mid) updata(node << 1, l, mid, al, ar, s); if (ar > mid) updata(node << 1 | 1, mid + 1, r, al, ar, s); push_up(node, l, r); } int query (int node, int l, int r, int num) { if (l == r) return l; push_down(node, l, r); int mid = (r + l) >> 1; if (mmax[node << 1] >= num) return query(node << 1, l, mid, num); else if (rmax[node << 1] + lmax[node << 1 | 1] >= num) return (mid - rmax[node << 1] + 1); return query(node << 1 | 1, mid + 1, r, num); } int main() { int n, m; while (~scanf("%d%d", &n, &m)) { build (1, 1, n); G.clear(); while (m--) { char op[10]; int num; scanf("%s", op); if (!strcmp(op, "New")) { scanf("%d", &num); if (mmax[1] < num) puts("Reject New"); else { node in; in.s = query(1, 1, n, num); in.e = in.s + num - 1; vector<node>::iterator it; it = upper_bound(G.begin(), G.end(), in, cmp); G.insert(it, in); printf("New at %d\n", in.s); updata(1, 1, n, in.s, in.e, 1); } } else if (!strcmp(op, "Free")) { node in; scanf("%d", &in.s); in.e = in.s; vector<node>::iterator it; it = upper_bound(G.begin(), G.end(), in, cmp); if (it == G.begin()) puts("Reject Free"); else { --it; if ((it -> e) < in.s) puts("Reject Free"); else { printf("Free from %d to %d\n", it -> s, it -> e); updata(1, 1, n, it -> s, it -> e, 0); G.erase(it); } } } else if (!strcmp(op, "Get")) { scanf("%d", &num); if (num > G.size()) puts("Reject Get"); else printf("Get at %d\n", G[num - 1].s); } else if (!strcmp(op, "Reset")) { updata(1, 1, n, 1, n, 0); puts("Reset Now"); G.clear(); } } printf("\n"); } return 0; }
相关推荐
Hdu 2871 Memory Control 是一个典型的线段树题目,题目要求我们实现一个内存控制系统,系统可以进行四种操作:Reset、New、Free 和 Get。 Reset 操作将所有内存单元释放。New 操作将分配一个内存块,Free 操作将...
这题如果没有输出2个解就很简单。 是个之前做过的类型: 把所有限制按R排序, 然后每次取出R最小的,然后从其L开始选,尽量选能选的中最小的。 这样选如果能选完,就说明有解。 贪心正确性显然:R大的至少可以选则R...
本教程重点讲解如何在Quartus综合环境中将内存(memory)转换为向量(vector)。Quartus是Altera公司(现已被Intel收购)开发的一款强大的FPGA(现场可编程门阵列)设计工具,它提供了从高层次的系统级描述到低层次...
【标题】:索尼记忆棒数据恢复 - Memory Stick File Rescue 【正文】: 在数字设备的世界里,记忆棒(Memory Stick)作为索尼推出的一种便携式存储设备,被广泛应用于数码相机、摄像机、掌上电脑和其他支持该格式...
OceanBase是一款分布式关系型数据库,其核心数据结构为In-Memory B+ Tree,它是在内存中实现的B+树数据结构。OceanBase的架构主要由Update Server、Root Server、Merge Server、Chunk Server和Client组成。其架构的...
DotNet Memory Profiler 5.6 是一款专用于检测和分析.NET应用程序内存使用情况的专业工具。这个官方版本的发布旨在帮助开发者识别并解决可能导致性能问题或内存泄漏的代码片段。由于官网下载速度可能较慢,这里提供...
JSR133-memory_model-1_0-pfd2-spec.pdf JSR133中文版.pdf j-jtp02244-Java.theory.and.practice-Fixing.the.Java.Memory.Model.Part1/2.pdf Java Memory Model Pragmatics (transcript).pdf 读JSR133笔记 - 十年磨...
C语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY....
### 日志结构合并树(LSM Tree)及在NAND闪存中的应用 #### 日志结构合并树(LSM Tree)概述 日志结构合并树(Log-Structured Merge Tree,简称LSM Tree)是一种用于优化写操作的数据结构,广泛应用于数据库、文件...
User space control memory failure recovery
标题“memory_write_control.rar_avalon_memory”表明这个压缩包包含了一个与Avalon内存接口相关的实现,主要涉及内存写控制。描述提到“实现了外界显示器的自定义Avalon接口设计”,这意味着设计可能用于连接外部...
记忆hmemory 是 c/c++ 程序的内存错误检测器。1. 概述hmemory 是用于 c/c++ 程序的轻量级内存错误检测器,专为嵌入式系统设计。 主要用例可能包括嵌入式系统,其中 - 或memcheck支持不可用。 并具有以下好处: 对...
Memory Selection Network for Video Propagation+论文笔记
The installation can be changed and uninstalled using "Program and Features" under the Windows Control panel. NOTE! .NET Memory Profiler can be run on Windows Vista, Windows 7/8/8.1/10, or Windows ...
### B+树C语言代码实现解析 #### 一、引言 本文将深入解析一份C语言实现的B+树代码,这份代码不仅演示了如何在操作系统文件系统之上构建B+树,而且还展示了如何通过文件操作来模拟B+树的建立过程。通过分析这段代码...
This article provides an overview of recent developments in mainmemory database systems. With growing memory sizes and memory prices dropping by a factor of 10 every 5 years, data having a “primary ...
《Dr.Memory:深入解析C++内存管理的利器》 在软件开发中,内存管理是至关重要的环节,尤其是在C++这种不提供自动垃圾回收的语言中。内存泄漏、野指针等问题往往是程序崩溃或性能下降的罪魁祸首。针对这些问题,一...