题面

Poj

题解

下面内容引用自"李煜东 《算法竞赛进阶指南》"(对原文略有缩减,侵删):

因为矩形的大小固定,所以矩形可以由它的任意一个顶点唯一确定。我们可以考虑把矩形的右上角顶点放在什么位置,圈住的星星亮度总和最大。

所以,对于一颗星星,能够覆盖住这颗星星的右上角的位置在区间$[x,y]-[x+w,y+h]$之间,但是由于边界上的星星不算,所以不妨将星星变为$[x-0.5,y-0.5]$,于是右上角变为$[x+w-1,y+h-1]$。

问题就转化成了:平面上有若干个区域,每个区域都带有一个权值,求在哪个坐标上重叠的区域权值和最大。可以用扫描线算法来解决。

具体来说,将一颗星星抽象成一个矩形,取出左右边界(四元组):$(x,y,y+h-1,c)$和$(x+w,y,y+h-1,-c)$,然后按照横坐标排序。关于纵坐标建立一颗线段树,维护区间最大值$dat$,初始全为$0$,线段树上的一个值$y$表示区间$[y,y+1]$,注意扫描每个四元组$(x,y_1,y_2,c)$,执行区间修改,把$[y1,y2]$中的每一个数都加$c$,用根节点$dat$更新就好了。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using std::min; using std::max;
using std::swap; using std::sort;
using std::unique; using std::lower_bound;
typedef long long ll;
#define int ll template<typename T>
void read(T &x) {
int flag = 1; x = 0; char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') flag = -flag; ch = getchar(); }
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag;
} const int N = 2e4 + 10;
ll n, W, H;
ll cf[N << 1], tot;
struct star { ll x, y, h; } s[N];
struct raw { ll x, y1, y2, d; } cy[N << 1]; int cnt;
inline bool cmp (const raw &a ,const raw &b) { return a.x < b.x || (a.x == b.x && a.d < b.d); }
ll val[N << 4], add[N << 4]; void pushdown(int o, int lc, int rc) {
if(add[o]) {
add[lc] += add[o], add[rc] += add[o];
val[lc] += add[o], val[rc] += add[o], add[o] = 0;
}
} void modify (int ml, int mr, ll k, int o = 1, int l = 1, int r = tot) {
if(l >= ml && r <= mr) { val[o] += k, add[o] += k; return ; }
int mid = (l + r) >> 1, lc = o << 1, rc = lc | 1;
pushdown(o, lc, rc);
if(ml <= mid) modify(ml, mr, k, lc, l, mid);
if(mr > mid) modify(ml, mr, k, rc, mid + 1, r);
val[o] = max(val[lc], val[rc]);
} signed main () {
while(scanf("%lld%lld%lld", &n, &W, &H) != EOF) {
cnt = tot = 0; ll ans = 0;
for(int i = 1; i <= n; ++i) {
read(s[i].x), read(s[i].y), read(s[i].h);
cy[++cnt] = (raw){s[i].x, s[i].y, s[i].y + H - 1, s[i].h};
cy[++cnt] = (raw){s[i].x + W, s[i].y, s[i].y + H - 1, -s[i].h};
cf[++tot] = s[i].y, cf[++tot] = s[i].y + H - 1;
} sort(&cf[1], &cf[tot + 1]), tot = unique(&cf[1], &cf[tot + 1]) - cf - 1;
sort(&cy[1], &cy[cnt + 1], cmp);
memset(val, 0, sizeof val), memset(add, 0, sizeof add);
for(int i = 1; i <= cnt; ++i) {
cy[i].y1 = lower_bound(&cf[1], &cf[tot + 1], cy[i].y1) - cf;
cy[i].y2 = lower_bound(&cf[1], &cf[tot + 1], cy[i].y2) - cf;
modify(cy[i].y1, cy[i].y2, cy[i].d);
ans = max(ans, val[1]);
}
printf("%lld\n", ans);
}
return 0;
}

Poj2482 Stars in Your Window(扫描线)的更多相关文章

  1. POJ2482 Stars in Your Window(扫描线+区间最大+区间更新)

    Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I stil ...

  2. poj 2482 Stars in Your Window(扫描线)

    id=2482" target="_blank" style="">题目链接:poj 2482 Stars in Your Window 题目大 ...

  3. POJ2482 Stars in Your Window 和 test20180919 区间最大值

    Stars in Your Window Language:Default Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K T ...

  4. POJ2482 Stars in Your Window 题解

    Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I stil ...

  5. poj2482 Stars in Your Window

    此题可用线段树或静态二叉树来做. 考虑用线段树: 很容易想到先限定矩形横轴范围再考虑在此纵轴上矩形内物品总价值的最大值. 那么枚举矩形横轴的复杂度是O(n)的,考虑如何快速获取纵轴上的最大值. 我们不 ...

  6. 【POJ-2482】Stars in your window 线段树 + 扫描线

    Stars in Your Window Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11706   Accepted:  ...

  7. 【POJ2482】Stars in Your Window

    [POJ2482]Stars in Your Window 题面 vjudge 题解 第一眼还真没发现这题居然™是个扫描线 令点的坐标为\((x,y)\)权值为\(c\),则 若这个点能对结果有\(c ...

  8. POJ 2482 Stars in Your Window 线段树扫描线

    Stars in Your Window   Description Fleeting time does not blur my memory of you. Can it really be 4 ...

  9. 【POJ2482】【线段树】Stars in Your Window

    Description Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw ...

随机推荐

  1. UVA 11440 Help Tomisu

    https://vjudge.net/problem/UVA-11440 题意: 求2——n! 之间有多少个整数x,满足x的所有素因子都大于m 保证m<=n x的所有素因子都大于m 等价于 x和 ...

  2. redis linux下的开机启动

    redis linux下的环境搭建  http://www.cnblogs.com/zsg88/p/8321644.html 安装完redis-4.0.1后设置linux开机自启动.    1.在re ...

  3. webpack自动生成项目的html

    1 自动生成多个html页面 设置webpack.config.js中的plugins属性,多次调用plugin插件(new htmlWebpackPlugin()),同时设置对应数量的.js入口文件 ...

  4. Select 使用不当引发的core,你应该知道的

    排查一个死机问题,搞了好几天时间,最终确定原因:最终确定问题原因,在此分享一下: 第一步:常规根据core文件查看栈信息,gdb –c core xxxx 如下rip不正确,指令地址错乱,栈信息已破坏 ...

  5. perl 复制exe文件的简单方法

    use warnings; use strict; open EXE, "cmd.exe" or die "Can not open cmd.exe:$!\n" ...

  6. 使用makecontext实现用户线程【转】

    转自:http://blog.csdn.net/cyberlabs/article/details/6920138 使用makecontext实现用户线程 现代Unix系统都在ucontext.h中提 ...

  7. python string 对齐文本的几个方法

    用rjust().ljust()和center()方法对齐文本

  8. Educational Codeforces Round 26 F. Prefix Sums 二分,组合数

    题目链接:http://codeforces.com/contest/837/problem/F 题意:如题QAQ 解法:参考题解博客:http://www.cnblogs.com/FxxL/p/72 ...

  9. C json实战引擎 一 , 实现解析部分

    引言 以前可能是去年的去年,写了一个 c json 解析引擎用于一个统计实验数据项目开发中. 基本上能用. 去年在网上 看见了好多开源的c json引擎 .对其中一个比较标准的 cJSON 引擎 深入 ...

  10. TensorFlow计算模型—计算图

    TensorFlow是一个通过计算图的形式来表述计算的编程系统.其中的Tnesor,代表它的数据结构,而Flow代表它的计算模型.TensorFlow中的每一个计算都是计算图上的一个节点,而节点之间的 ...