POJ 2482 解题报告

三个月以来的第一题。。。

这道题是二维的线段树,需要转化为一维。

首先只考虑x轴,将每个点看成两条线,入线和出线。入线的x坐标是点的x坐标,亮度是点的亮度;出线x坐标是x+W, 亮度是点的亮度的负值,即-c。这样做的原因是如果我们按照x的大小从左往右处理所有的点(想象一个以每个点再左下那么一点点为左下角的矩形扫描,左下那么一点点是为了包括点),入线表示我们到了点的作用范围,出线会取消掉点的作用。为了能这样从左往右处理,我们需要把所有的入线出线按照x坐标从小到大排序。x值相同的时候,亮度为负值的点需要先处理,这样保证我们不会多算亮度和(即避免有个点已经不起作用了,仍然暂时算进去了)。

然后考虑y轴,由于上述处理方式,我们已经能够保证每个在线段树里面的点x是在作用范围内的,所以我们只用在处理每个点的时候把点加入到其y轴作用范围内就好了,即[y, y + H - 1]。这个范围是由于我们上述描述的矩形的位置决定的。

由于y值范围可能很大,为了减少线段树的空间消耗,我们可以将y离散化,即把所有y, y + H - 1排序,给个编号。编号仍然能表示两个y值的大小关系,但大大减少了数值范围。


我的线段树实现一如既往的慢。

thestoryofsnow2482Accepted3216K547MSC++3282B

/* 
ID: thestor1 
LANG: C++ 
TASK: poj2482 
*/
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <limits>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cassert>

using namespace std;

typedef unsigned long long ULL;

const int MAXN = 10000;

class Star
{
public:
	ULL x, y1, y2;
	int c;
	Star() {}
	Star(ULL x, ULL y1, ULL y2, int c) : x(x), y1(y1), y2(y2), c(c) {}

	// We insert stars from left to right (with x increasing)
	// But we should insert the leaving stars (c < 0) first
	// to make SegmentTree maxc correct 
	// (as those leaving stars shouldn't count)
	inline bool operator<(const Star &rhs) const 
	{
		return x < rhs.x || (x == rhs.x && c < rhs.c);
	}
};

class SegmentTree {
public:
	int sumc, maxc;
	SegmentTree() : sumc(0), maxc(0) {}
};

void insert(SegmentTree segmentTree[], int left, int right, int index, Star &star) {
	if (left > star.y2 || right < star.y1) {
		return;
	}

	segmentTree[index].sumc += star.c;
	if (left == right) {
		segmentTree[index].maxc = segmentTree[index].sumc;
		return;
	}

	int mid = left + (right - left) / 2;
	insert(segmentTree, left, mid, 2 * index + 1, star);
	insert(segmentTree, mid + 1, right, 2 * index + 2, star);
	segmentTree[index].maxc = max(segmentTree[2 * index + 1].maxc, segmentTree[2 * index + 2].maxc);
}

int main() {
	int n, W, H;
	Star stars[2 * MAXN];
	SegmentTree segmentTree[2 * MAXN * 4];
	ULL ys[2 * MAXN];
	map<ULL, int> yIndexMap;
	while (scanf("%d%d%d", &n, &W, &H) > 0) {
		// printf("n:%d, W:%d, H:%d\n", n, W, H);
		// reset the segmentTree
		for (int i = 0; i < 4 * n; ++i) {
			segmentTree[i].sumc = 0;
		}

		for (int i = 0; i < n; ++i) {
			// the "entrancing star"
			// which will add 'c' when inserted
			scanf("%lld%lld%d", &stars[i].x, &stars[i].y1, &stars[i].c);
			// the y range for a star is [y, y + H - 1]
			// when assuming a rectangle whose bottom left corner is slightly off [x, y]
			stars[i].y2 = stars[i].y1 + H - 1;

			// add the corresponding "leaving star"
			// which when inserted will counteract the "entrancing star"
			stars[i + n].x = stars[i].x + W;
			stars[i + n].y1 = stars[i].y1;
			stars[i + n].y2 = stars[i].y2;
			stars[i + n].c = -stars[i].c;
		}

		// map stars' y to index which can reflect their relative difference (but ignore the absolute value)
		for (int i = 0; i < n; ++i) {
			ys[i] = stars[i].y1;
			ys[i + n] = stars[i].y2;
		}
		sort(ys, ys + 2 * n);
		int yIndex = 0;
		for (int i = 0; i < 2 * n; ++i) {
			if (i > 0 && ys[i] != ys[i - 1]) {
				yIndex++;
			}
			yIndexMap[ys[i]] = yIndex;
		}
		for (int i = 0; i < 2 * n; ++i) {
			stars[i].y1 = yIndexMap[stars[i].y1];
			stars[i].y2 = yIndexMap[stars[i].y2];
		}

		// sort the stars with their x value (if equal, negative c first)
		sort(stars, stars + 2 * n);
		// printf("stars:\n");
		// for (int i = 0; i < 2 * n; ++i) {
		// 	printf("(x: %lld, y1: %lld, y2: %lld, c: %d)\t", stars[i].x, stars[i].y1, stars[i].y2, stars[i].c); 
		// }
		// printf("\n");

		int maxc = 0;
		for (int i = 0; i < 2 * n; ++i) {
			insert(segmentTree, 0, 2 * n - 1, 0, stars[i]);
			maxc = max(maxc, segmentTree[0].maxc);
		}

		printf("%d\n", maxc);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值