码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2871 Memory Control(线段树)

时间:2014-10-18 22:23:21      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   sp   

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;
}


HDU 2871 Memory Control(线段树)

标签:style   blog   http   color   io   os   ar   for   sp   

原文地址:http://blog.csdn.net/accelerator_/article/details/40219549

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!