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; }
相关推荐
从简单入门到偏中等的几个题,线段树很灵活,主要懂了lazy操作,其他的自己yy吧。
这题如果没有输出2个解就很简单。 是个之前做过的类型: 把所有限制按R排序, 然后每次取出R最小的,然后从其L开始选,尽量选能选的中最小的。 这样选如果能选完,就说明有解。 贪心正确性显然:R大的至少可以选则R...
OceanBase核心数据结构:In_Memory_B+_Tree.pdf
这是一个Quartus的工程文件和verilog代码,讲如何把memory 变成vector
索尼记忆棒数据恢复可以恢复因文件损坏而丢失的数据
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笔记 - 十年磨...
DotNet Memory Profiler5.6 官方版本 + 使用手册.zip ,官网下载速度太慢,搞了一份供大家下载
C语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY....
记忆hmemory 是 c/c++ 程序的内存错误检测器。1. 概述hmemory 是用于 c/c++ 程序的轻量级内存错误检测器,专为嵌入式系统设计。 主要用例可能包括嵌入式系统,其中 - 或memcheck支持不可用。 并具有以下好处: 对...
Memory Selection Network for Video Propagation+论文笔记
memory of data control system distribution managed
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 ...
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 ...
MemoryAnalyzer使用说明文档/使用指南 Eclipse Memory Analyzer 是一个功能丰富且轻量的 Java 堆内存分析工具,可以用来辅助发现内存泄漏减少内存占用。 使用 Memory Analyzer 来分析生产环境的 Java 堆转储文件,...
MemoryAnalyzer 使用中文
This manual describes the X-CUBE-MCSDK and X-CUBE-MCSDK-FUL STM32 motor control software development kits (SDKs) designed for, and to be used with, STM32 microcontrollers. The SDKs contain a software ...
The Art of Memory Forensics: Detecting Malware and Threats in Windows, Linux, and Mac Memory Memory forensics provides cutting edge technology to help investigate digital attacks Memory forensics is ...
Time Limit:1000MS Memory Limit:65536K Description 在一个圆形操场的四周摆放着 n 堆石子. 现要将石子有次序地合并成一堆, 规定每次只能选相邻的 2 堆石子合并成新的一堆, 并将新的一堆石子数记为该次合并的得分....
MemoryAnalyzer For Mac 百度网盘 解压后是mat.app。 类似执行以下命令:(注意换成自己的地址) /pllhome/software/Linux/mat.app/Contents/MacOS/MemoryAnalyzer -data ./workspace