码迷,mamicode.com
首页 > Windows程序 > 详细

POJ 2482 Stars in Your Window(线段树)

时间:2014-10-16 21:09:33      阅读:342      评论:0      收藏:0      [点我收藏+]

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

POJ 2482 Stars in Your Window

题目链接

题意:给定一些星星,每个星星都有一个亮度,现在要用w * h的矩形去框星星,问最大能框的亮度是多少

思路:转化为扫描线的问题,每个星星转化为一个矩形,那么等于求矩形相交区域值最大的区域,利用线段树去维护即可

代码:

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

typedef long long ll;
const int N = 10005;

int n, w, h;

struct Line {
	ll l, r, y, c;
	Line() {}
	Line(ll l, ll r, ll y, ll c) {
		this->l = l; this->r = r;
		this->y = y; this->c = c;
	}
} line[N * 2];

bool cmp(Line a, Line b) {
	if (a.y == b.y) return a.c < b.c;
	return a.y < b.y;
}

ll hash[N * 2];
int hn;

int find(ll x) {
	return lower_bound(hash, hash + hn, x) - hash;
}

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

struct Node {
	int l, r;
	ll add, sum;
} node[N * 8];

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

void pushup(int x) {
	if (node[x].l == node[x].r) node[x].sum = node[x].add;
	else node[x].sum = max(node[lson(x)].sum, node[rson(x)].sum) + node[x].add;
}

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

int main() {
	while (~scanf("%d%d%d", &n, &w, &h)) {
		ll x1, y1, x2, y2, c;
		for (int i = 0; i < n; i++) {
			scanf("%lld%lld%lld", &x1, &y1, &c);
			x2 = x1 + w; y2 = y1 + h;
			hash[i * 2] = x1; hash[i * 2 + 1] = x2;
			line[i * 2] = Line(x1, x2, y1, c);
			line[i * 2 + 1] = Line(x1, x2, y2, -c);
		}
		n *= 2;
		hn = 1;
		sort(hash, hash + n);
		for (int i = 1; i < n; i++)
			if (hash[i] != hash[i - 1])
				hash[hn++] = hash[i];
		build(0, hn - 2);
		sort(line, line + n, cmp);
		ll ans = 0;
		for (int i = 0; i < n; i++) {
			add(find(line[i].l), find(line[i].r) - 1, line[i].c);
			ans = max(ans, node[0].sum);
		}
		printf("%lld\n", ans);
	}
	return 0;
}


POJ 2482 Stars in Your Window(线段树)

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

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

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