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高级程序设计】(第九章)进程间通信-管道 2
文件描述符重定向 cat<test01 :将输入重定向到test01文件 cat>test02<test01 :将标准正确输出重定向到test02文件,输入设备重定向到test0 ...
- 二十六个月Android学习工作总结【转】
原文:二十六个月Android学习工作总结 1.客户端的功能逻辑不难,UI界面也不难,但写UI花的时间是写功能逻辑的两倍. 2.写代码前的思考过程非常重要,即使在简单的功能,也需要在本子上把该 ...
- Python的功能模块[4] -> pdb/ipdb -> 实现 Python 的单步调试
pdb / ipdb 模块 / pdb / ipdb Module pdb 和 ipdb 的主要作用是用于 Python 程序的单步调试,Python 的调试可参考链接. 下面是一个简单的使用示例 i ...
- 洛谷——P2368 EXCEEDED WARNING B
P2368 EXCEEDED WARNING B 题目背景 SGU 107 题目描述 求有多少个平方后末尾为987654321的n位数 输入输出格式 输入格式: 整数n 输出格式: 答案,即[b]“平 ...
- How to not display “Commit point reached - logical record count” counts
You can use the keyword silent, which is available in the options clause. You can set the followin ...
- Missing Ranges -- LeetCode
Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its mi ...
- lor实践
1.启动一个nginx监听8888端口, -p 指定工作目录 -c 指定加载配置文件 在nginx.conf中,写一个server,进入lor项目的入口文件main.lua 2.main.lua执行r ...
- Orchard EventBus 事件总线及 IEventHandler作用
事件总线接口定义: public interface IEventBus : IDependency { IEnumerable Notify(string messageName, IDiction ...
- linux之openssh协议
SSH的全称是Secure Shell,简单说来ssh是一种安全的外壳协议,用于两个计算机间安全的远程登陆,说它安全,是因为ssh采用公钥加密的机制.最开始时用作远程管理的工具是telnet,这个协议 ...
- 设计模式之组合模式(PHP实现)
github地址:https://github.com/ZQCard/design_pattern /** 组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作 ...