Memory Control
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
题意:
模拟四种操作
Reset 将n个内存单元全部清空
New x 申请一个长度为x的连续内存块 申请成功就输出左端
Free x 将x所在的内存块空间释放 释放成功输出释放的内存始末位置
Get x 输出第x个内存块的起始位置
#include <cstdio>
#include <cstring>
#include <cctype>
#include <vector>
#include <algorithm>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
struct block
{
int begin, end;
};
vector<block> v;
bool cmp(const block &a, const block &b)
{
return a.begin < b.begin;
}
const int maxn = 50100;
int lsum[maxn << 2], rsum[maxn << 2], msum[maxn << 2];
int cover[maxn << 2];
void PushDown(int rt, int m) {
if (cover[rt] != -1) {
cover[rt << 1] = cover[rt << 1 | 1] = cover[rt];
msum[rt << 1] = lsum[rt << 1] = rsum[rt << 1] = cover[rt] ? 0 : m - (m >> 1);
msum[rt << 1 | 1] = lsum[rt << 1 | 1] = rsum[rt << 1 | 1] = cover[rt] ? 0 : (m >> 1);
cover[rt] = -1;
}
}
void PushUp(int rt, int m) {
lsum[rt] = lsum[rt << 1];
rsum[rt] = rsum[rt << 1 | 1];
if (lsum[rt] == m - (m >> 1)) lsum[rt] += lsum[rt << 1 | 1];
if (rsum[rt] == (m >> 1)) rsum[rt] += rsum[rt << 1];
msum[rt] = max(lsum[rt << 1 | 1] + rsum[rt << 1], max(msum[rt << 1], msum[rt << 1 | 1]));
}
void build(int l, int r, int rt) {
msum[rt] = lsum[rt] = rsum[rt] = r - l + 1;
cover[rt] = -1;
if (l == r) return;
int m = (l + r) >> 1;
build(lson);
build(rson);
}
void update(int L, int R, int c, int l, int r, int rt) {
if (L <= l && r <= R) {
msum[rt] = lsum[rt] = rsum[rt] = c ? 0 : r - l + 1;
cover[rt] = c;
return;
}
PushDown(rt, r - l + 1);
int m = (l + r) >> 1;
if (L <= m) update(L, R, c, lson);
if (m < R) update(L, R, c, rson);
PushUp(rt, r - l + 1);
}
int query(int w, int l, int r, int rt) {
if (l == r) return l;
PushDown(rt, r - l + 1);
int m = (l + r) >> 1;
if (msum[rt << 1] >= w) return query(w, lson);
else if (rsum[rt << 1] + lsum[rt << 1 | 1] >= w) return m - rsum[rt << 1] + 1;
return query(w, rson);
}
int main() {
int n, m;
while (scanf("%d%d", &n, &m) != EOF) {
build(1, n, 1);
v.clear();
while (m--) {
char op[20];
int a;
scanf("%s", &op);
if (op[0] == 'N') {
scanf("%d", &a);
if (msum[1] < a) printf("Reject New\n");
else {
int p = query(a, 1, n, 1);
printf("New at %d\n", p);
update(p, p + a - 1, 1, 1, n, 1);
block y;
y.begin = p;
y.end = p + a - 1;
//找到位置插入,保证vector里v.begin是递增的
vector<block>::iterator it;
it = upper_bound(v.begin(), v.end(), y, cmp);
v.insert(it, y);
}
}
else if (op[0] == 'R')
{
update(1, n, 0, 1, n, 1);
v.clear();
printf("Reset Now\n");
}
else if (op[0] == 'F')
{
scanf("%d", &a);
block y;
y.begin = a, y.end = a;
vector<block>::iterator it;
it = upper_bound(v.begin(), v.end(), y, cmp);
int tmp = it - v.begin() - 1;
if (tmp == -1 || v[tmp].end < a)//不存在
printf("Reject Free\n");
else
{
printf("Free from %d to %d\n", v[tmp].begin, v[tmp].end);
update(v[tmp].begin, v[tmp].end, 0,1,n,1);
v.erase(v.begin() + tmp);
}
}
else
{
scanf("%d", &a);
if (a > v.size())
printf("Reject Get\n");
else printf("Get at %d\n", v[a - 1].begin);
}
}
printf("\n");
}
return 0;
}