博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2482 解题报告
阅读量:4204 次
发布时间:2019-05-26

本文共 3546 字,大约阅读时间需要 11 分钟。

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

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

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

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

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

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

Accepted 3216K 547MS 3282B

/* ID: thestor1 LANG: C++ TASK: poj2482 */#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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
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); }}

转载地址:http://hvxli.baihongyu.com/

你可能感兴趣的文章
【unix网络编程第三版】阅读笔记(三):基本套接字编程
查看>>
同步与异步的区别
查看>>
IT行业--简历模板及就业秘籍
查看>>
JNI简介及实例
查看>>
DOM4J使用教程
查看>>
JAVA实现文件树
查看>>
linux -8 Linux磁盘与文件系统的管理
查看>>
linux 9 -文件系统的压缩与打包 -dump
查看>>
PHP在变量前面加&是什么意思?
查看>>
ebay api - GetUserDisputes 函数
查看>>
ebay api GetMyMessages 函数
查看>>
php加速器 - zendopcache
查看>>
手动12 - 安装php加速器 Zend OPcache
查看>>
set theme -yii2
查看>>
yii2 - 模块(modules)的view 映射到theme里面
查看>>
yii2 - controller
查看>>
yii2 - 增加actions
查看>>
网站加载代码
查看>>
php图像处理函数大全(缩放、剪裁、缩放、翻转、旋转、透明、锐化的实例总结)
查看>>
magento url中 uenc 一坨编码 base64
查看>>