HDU 2871 Memory Control(线段树)

HDU 2871 Memory Control

题目链接

题意:内存操作,和hotel那题差不多,多一个get操作

思路:线段树区间合并,其他都差不多,多一个get操作,这个用set去乱搞就过了- -,估计数据鶸吧,多这个操作感觉要用splay去搞了

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;

const int N = 50005;
#define MP(a,b) make_pair(a,b)
typedef pair<int, int> pii;

int n, m;

set<pii> g;
set<pii>::iterator it;

#define lson(x) ((x<<1)+1)
#define rson(x) ((x<<1)+2)

struct Node {
    int l, r, lsum, rsum, sum, lazy;
    int size() {return r - l + 1;}
    void gao(int v) {
        lazy = v;
        lsum = rsum = sum = v * size();
    }
} node[N * 4];

void pushdown(int x) {
    if (node[x].lazy != -1) {
        node[lson(x)].gao(node[x].lazy);
        node[rson(x)].gao(node[x].lazy);
        node[x].lazy = -1;
    }
}

void pushup(int x) {
    node[x].lsum = node[lson(x)].lsum;
    node[x].rsum = node[rson(x)].rsum;
    node[x].sum = max(node[lson(x)].sum, node[rson(x)].sum);
    if (node[lson(x)].lsum == node[lson(x)].size())
        node[x].lsum += node[rson(x)].lsum;
    if (node[rson(x)].rsum == node[rson(x)].size())
        node[x].rsum += node[lson(x)].rsum;
    node[x].sum = max(node[x].sum, node[lson(x)].rsum + node[rson(x)].lsum);
}

void build(int l, int r, int x = 0) {
    node[x].l = l; node[x].r = r; node[x].lazy = -1;
    if (l == r) {
        node[x].lsum = node[x].rsum = node[x].sum = 1;
        return;
    }
    int mid = (l + r) / 2;
    build(l, mid, lson(x));
    build(mid + 1, r, rson(x));
    pushup(x);
}

void add(int l, int r, int v, int x = 0) {
    if (node[x].l >= l && node[x].r <= r) {
        node[x].gao(v);
        return;
    }
    int mid = (node[x].l + node[x].r) / 2;
    pushdown(x);
    if (l <= mid) add(l, r, v, lson(x));
    if (r > mid) add(l, r, v, rson(x));
    pushup(x);
}

#define INF 0x3f3f3f3f

int query(int v, int x = 0) {
    if (node[x].l == node[x].r)
        return node[x].l;
	int mid = (node[x].l + node[x].r) / 2;
	pushdown(x);
	int ans = INF;
	if (node[lson(x)].sum >= v) ans = query(v, lson(x));
	else if (node[lson(x)].rsum + node[rson(x)].lsum >= v) ans = node[lson(x)].r - node[lson(x)].rsum + 1;
	else if (node[rson(x)].sum >= v) ans = query(v, rson(x));
	pushup(x);
	return ans;
}

int main() {
	while (~scanf("%d%d", &n, &m)) {
		build(1, n);
		g.clear();
		char op[15];
		int x;
		while (m--) {
			scanf("%s", op);
			if (op[0] == 'R') {
				printf("Reset Now\n");
				add(1, n, 1);
				g.clear();
				continue;
			}
			scanf("%d", &x);
			if (op[0] == 'N') {
				int v = query(x);
				if (v != INF) {
					printf("New at %d\n", v);
					add(v, v + x - 1, 0);
					g.insert(MP(v, v + x - 1));
				}
				else printf("Reject New\n");
			}
			if (op[0] == 'G') {
				if (g.size() < x) printf("Reject Get\n");
				else {
					it = g.begin();
					for (int i = 0; i < x - 1; i++)
						it++;
					printf("Get at %d\n", it->first);
				}
			}
			if (op[0] == 'F') {
				it = g.upper_bound(MP(x, INF));
				if (it == g.begin()) printf("Reject Free\n");
				else {
					it--;
					if (it->second < x) printf("Reject Free\n");
					else {
						printf("Free from %d to %d\n", it->first, it->second);
						add(it->first, it->second, 1);
						g.erase(it);
					}
				}
			}
		}
		printf("\n");
	}
	return 0;
}
时间: 2024-08-06 20:08:37

HDU 2871 Memory Control(线段树)的相关文章

hdu 2871 Memory Control(线段树)

题目链接:hdu 2871 Memory Control 题目大意:模拟一个内存分配机制. Reset:重置,释放所有空间 New x:申请内存为x的空间,输出左地址 Free x:释放地址x所在的内存块 Get x:查询第x个内存块,输出左地址 解题思路:一开始全用线段树去做,写的乱七八糟,其实只要用线段树维护可用内存.然后用户一个vector记录所有的内存块. #include <cstdio> #include <cstring> #include <vector>

HDU 2871 Memory Control (线段树,区间合并)

http://acm.hdu.edu.cn/showproblem.php?pid=2871 Memory Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4418    Accepted Submission(s): 1056 Problem Description Memory units are numbered

HDU 2871 Memory Control

一共4种操作 其中用线段树 区间合并,来维护连续空的长度,和找出那个位置.其他用vector维护即可 #include<cstring> #include<cstdio> #include<algorithm> #include<cmath> #include <iostream> #include<vector> #define lson i<<1 #define rson i<<1|1 #define N

HDU 2871 Memory Control(线段树&#183;区间合并&#183;Vector)

题意  模拟内存申请  有n个内存单元  有以下4种操作 Reset  将n个内存单元全部清空 New x  申请一个长度为x的连续内存块  申请成功就输出左端 Free x  将x所在的内存块空间释放  释放成功输出释放的内存始末位置 Get x  输出第x个内存块的起始位置 Reset 和 New 都是基本的区间合并知识  比较简单  Free和Get需要知道内层块的位置  所以我们在New的时候要将申请的内存块的始末位置都存起来  两个内层块不会相交  这样就能通过二分找到需要的内层块了

hdu 2871 Memory Control(成段更新,区间合并)

这道题在网上搜了一下题解,别人说是比hdu hotel还要变态的一题. 既然是变态题,因为它综合了线段树的几乎所有操作. 这道题的题意是: 有如下几个操作: 1:首先是Reset操作,这个操作代表的是把所有的内存空间全部都清空. 2:New x:代表的是分配一个x个内存空间,如果有内存空间的话,则输出那个内存空间的最左边的那个端点.否则,则输出Reject New 3:Free x:代表的是释放含有x这个数的内存空间.如果这个内存空间已经被释放了,那么就输出Reject Free 4:Get x

HDU 2871&quot;Memory Control&quot;(线段树区间和并+set&lt;pii &gt;.lower_bound())

传送门 •题意 有 n 个内存单元(编号从1开始): 给出 4 种操作: (1)Reset :表示把所有的内存清空,然后输出 "Reset Now". (2)New x :表示申请一块长度为 x 的内存块(满足起始地址尽可能小): 如果找到,输出 "New at A",A表示该内存块的起点,找不到,输出 "Reject New". (3)Free x :表示把包含第 x 块单位内存的内存块清除: 如果 x 在某内存块中,输出 "Free

hdu 2795 Billboard(线段树)

Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10890    Accepted Submission(s): 4827 Problem Description At the entrance to the university, there is a huge rectangular billboard of

多校训练hdu --Nice boat(线段树,都是泪)

Nice boat Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 47 Accepted Submission(s): 10 Problem Description There is an old country and the king fell in love with a devil. The devil always ask

hdu 5700区间交(线段树)

区间交 Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 849    Accepted Submission(s): 377 Problem Description 小A有一个含有n个非负整数的数列与m个区间.每个区间可以表示为li,ri. 它想选择其中k个区间, 使得这些区间的交的那些位置所对应的数的和最大. 例如样例中,选择[2,5]