hdu 2871 Memory Control(线段树)
题目大意:模拟一个内存分配机制。
- Reset:重置,释放全部空间
- New x:申请内存为x的空间,输出左地址
- Free x:释放地址x所在的内存块
- Get x:查询第x个内存块,输出左地址
解题思路:一開始全用线段树去做,写的乱七八糟,事实上仅仅要用线段树维护可用内存。然后用户一个vector记录全部的内存块。
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 50005;
#define lson(x) ((x)<<1)
#define rson(x) (((x)<<1)|1)
int lc[maxn << 2], rc[maxn << 2], set[maxn << 2];
int L[maxn << 2], R[maxn << 2], S[maxn << 2];
inline int length (int u) {
return rc[u] - lc[u] + 1;
}
inline void maintain (int u, int v) {
set[u] = v;
L[u] = R[u] = S[u] = (v ? 0 : length(u));
}
inline void pushup (int u) {
S[u] = max( max(S[lson(u)], S[rson(u)]), L[rson(u)] + R[lson(u)]);
L[u] = L[lson(u)] + (L[lson(u)] == length(lson(u)) ? L[rson(u)] : 0);
R[u] = R[rson(u)] + (R[rson(u)] == length(rson(u)) ? R[lson(u)] : 0);
}
inline void pushdown (int u) {
if (set[u] != -1) {
maintain(lson(u), set[u]);
maintain(rson(u), set[u]);
set[u] = -1;
}
}
void build (int u, int l, int r) {
lc[u] = l;
rc[u] = r;
set[u] = -1;
if (l == r) {
maintain(u, 0);
return;
}
int mid = (l + r) / 2;
build(lson(u), l, mid);
build(rson(u), mid + 1, r);
pushup(u);
}
void modify (int u, int l, int r, int v) {
if (l <= lc[u] && rc[u] <= r) {
maintain(u, v);
return;
}
pushdown(u);
int mid = (lc[u] + rc[u]) / 2;
if (l <= mid)
modify(lson(u), l, r, v);
if (r > mid)
modify(rson(u), l, r, v);
pushup(u);
}
int query (int u, int len) {
if (S[u] < len)
return 0;
if (lc[u] == rc[u])
return lc[u];
pushdown(u);
int mid = (lc[u] + rc[u]) / 2, ret;
if (S[lson(u)] >= len)
ret = query(lson(u), len);
else if (L[rson(u)] + R[lson(u)] >= len)
ret = mid - R[lson(u)] + 1;
else
ret = query(rson(u), len);
pushup(u);
return ret;
}
typedef pair<int, int> pii;
int N, M;
vector<pii> list;
int find (int k) {
int l = 0, r = list.size() - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (list[mid].first > k)
r = mid - 1;
else
l = mid + 1;
}
return l;
}
int main () {
while (scanf("%d%d", &N, &M) == 2) {
build (1, 1, N);
list.clear();
int k;
char op[5];
while (M--) {
scanf("%s", op);
if (op[0] == 'R') {
modify(1, 1, N, 0);
list.clear();
printf("Reset Now\n");
} else {
scanf("%d", &k);
if (op[0] == 'N') {
int x = query(1, k);
if (x) {
modify(1, x, x + k - 1, 1);
pii u = make_pair(x, x + k - 1);
list.insert(list.begin() + find(x), u);
printf("New at %d\n", x);
} else
printf("Reject New\n");
} else if (op[0] == 'F') {
int x = find(k) - 1;
if (x != -1 && k <= list[x].second) {
modify(1, list[x].first, list[x].second, 0);
printf("Free from %d to %d\n", list[x].first, list[x].second);
list.erase(list.begin() + x);
} else
printf("Reject Free\n");
} else if (op[0] == 'G') {
if (k <= list.size()) {
printf("Get at %d\n", list[k-1].first);
} else
printf("Reject Get\n");
}
}
}
printf("\n");
}
return 0;
}
hdu 2871 Memory Control(线段树)的更多相关文章
- hdu 2871 Memory Control(伸展树splay tree)
hdu 2871 Memory Control 题意:就是对一个区间的四种操作,NEW x,占据最左边的连续的x个单元,Free x 把x单元所占的连续区间清空 , Get x 把第x次占据的区间输出 ...
- hdu 2871 Memory Control (区间合并 连续段的起始位置 点所属段的左右端点)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2871 题意: 四种操作: 1.Reset 清空所有内存2.New x 分配一个大小为x的内存块返回,返 ...
- HDU 2871 Memory Control
一共4种操作 其中用线段树 区间合并,来维护连续空的长度,和找出那个位置.其他用vector维护即可 #include<cstring> #include<cstdio> #i ...
- HDU 2871"Memory Control"(线段树区间和并+set.lower_bound)
传送门 •题意 有 n 个内存单元(编号从1开始): 给出 4 种操作: (1)Reset :表示把所有的内存清空,然后输出 "Reset Now". (2)New x :表示申请 ...
- ●HDU 2871 Memory Control(Splay)
●赘述题目 四种操作: ○Reset:将整个内存序列清空. ○New a:在尽量靠左的位置新建一个长度为a的内存块,并输出改内存块起始位置.(各个内存块即使相邻也不会合并..) ○Free a:将a点 ...
- hdu 5700区间交(线段树)
区间交 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...
- Snacks HDU 5692 dfs序列+线段树
Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...
- HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Sub ...
随机推荐
- 嵌入式Linux支持LCD console【转】
转自:http://blog.sina.com.cn/s/blog_664c545f0100v9zl.html 转载:http://www.mculee.cn/post/48.html [1]LCD ...
- UVA 10910 Marks Distribution
题意 把数字T分成N个数的和,保证这N个数中最小的数大于P.求方案数目 另f[i][j]表示把i分成j个数的和的方案数 f[i][j]=f[i][j-1]+f[i-1][j-1]+f[i-2][j-1 ...
- 理解python的super
def super(cls, inst): mro = inst.__class__.mro() return mro[mro.index(cls) + 1] super做了这些事 摘自:https: ...
- net页面生命周期
ASP.NET 页运行时,此页将经历一个生命周期,在生命周期中将执行一系列处理步骤.这些步骤包括初始化.实例化控件.还原和维护状态.运行事件处理程序代码以及进行呈现.了解页的生命周期非常重要,这样就能 ...
- kylin
Kylin只能导入扁平化的Hive表,简而言之,其不支持Hive的复杂数据类型,如array.struct.map等
- java.sql.SQLException: Access denied for user 'roo'@'localhost' (using password: YES)
初学mysql,安装了mysql8.0.11,激动的用jdbc连接数据库,出现error,折腾了三天依旧无解,最后无奈装了比较稳定的mysql5.5,问题得以解决,很迷,但只要error没了就开心. ...
- NOIP2016_day1_No1玩具谜题
#include <iostream> #include <cstdio> using namespace std; int main () { freopen("t ...
- 每天一个liunx命令4之 ps -ef ,ps -aux ,ps aux
1ps aux和ps –aux 请注意"ps -aux"不同于"ps aux".POSIX和UNIX的标准要求"ps -aux"打印用户名为 ...
- sql server mvp 發糞塗牆
http://blog.csdn.net/dba_huangzj/article/details/38295753
- ylb:事务处理
ylbtech_sqlserver --1.定义三个变量分别保存你的姓名,年龄和身高,然后赋值并且输出 --DECLARE @name varchar(10) , @age int , @height ...