Poj2482 Stars in Your Window(扫描线)
题面
题解
下面内容引用自"李煜东 《算法竞赛进阶指南》"(对原文略有缩减,侵删):
因为矩形的大小固定,所以矩形可以由它的任意一个顶点唯一确定。我们可以考虑把矩形的右上角顶点放在什么位置,圈住的星星亮度总和最大。
所以,对于一颗星星,能够覆盖住这颗星星的右上角的位置在区间$[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(扫描线)的更多相关文章
- 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 ...
- poj 2482 Stars in Your Window(扫描线)
id=2482" target="_blank" style="">题目链接:poj 2482 Stars in Your Window 题目大 ...
- POJ2482 Stars in Your Window 和 test20180919 区间最大值
Stars in Your Window Language:Default Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K T ...
- 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 ...
- poj2482 Stars in Your Window
此题可用线段树或静态二叉树来做. 考虑用线段树: 很容易想到先限定矩形横轴范围再考虑在此纵轴上矩形内物品总价值的最大值. 那么枚举矩形横轴的复杂度是O(n)的,考虑如何快速获取纵轴上的最大值. 我们不 ...
- 【POJ-2482】Stars in your window 线段树 + 扫描线
Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11706 Accepted: ...
- 【POJ2482】Stars in Your Window
[POJ2482]Stars in Your Window 题面 vjudge 题解 第一眼还真没发现这题居然™是个扫描线 令点的坐标为\((x,y)\)权值为\(c\),则 若这个点能对结果有\(c ...
- 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 ...
- 【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 ...
随机推荐
- [洛谷P2261] [CQOI2007]余数求和
洛谷题目链接:[CQOI2007]余数求和 题目背景 数学题,无背景 题目描述 给出正整数n和k,计算G(n, k)=k mod 1 + k mod 2 + k mod 3 + - + k mod n ...
- struts2常用标签之数据标签
数据标签1 property标签 property标签的主要属性: value:用来获取值的OGNL表达式,如果value属性值没有指定,那么将会被设定为top,也就是返回位于值栈最顶端的对象. ...
- 几分钟内学习 Clojure
1.基本例子 ; 分号作为注释的开始 ; Clojure 用一种把元素用括号括起来的像列表一样的方式来书写,元素之间用空格隔开 ; clojure 解释器会把第一个元素当做是函数或者宏调用,其他的都作 ...
- idea出现:error:java: Target level '1.7' is incompatible with source level '1.8'.解决办法
当我们开始使用idea的时候,编译jsp程序我们有可能出现编译错误,然而我们的代码又没有什么问题. 解决方法一:我们开始的时候可以通过修改java compiler来解决这样的问题,点击file菜单- ...
- 关于SQL注入的五大报错注入函数
~全部都以查user()为例子~ 1.floor()id = 1 and (select 1 from (select count(*),concat(version(),floor(rand(0) ...
- input placeholder 兼容问题
placeholder是html5出的新特性,ie9以下是不兼容的, 那么为了兼容ie9 我们需要对他做处理 //jq的处理方式$(function(){ jQuery('[placeholder] ...
- 6.0docker Dockerfile文件
指令格式 #注释 FROM :基础镜像 MAINTAINER:镜像的作者信息 RUN :指定(构建过程中)当前镜像中运行的命令 EXPOSE :指定运行镜像的容器应用程序所使用的端口 容器但不会打开, ...
- elk + suricata 实验环境详细安装教程
1.安装运行suricata,需要*** sudo add-apt-repository ppa:oisf/suricata-stable sudo apt-get update sudo apt-g ...
- 【Matlab】让Matlab程序发出声音
我有时候运行一段很长的代码,在等待的时候去做别的事,希望程序运行完可以有一个提示音. 这可以用matlab的一个函数sound实现,该函数的输入参量是音频数据向量.采样频率和转换位数. % 响一声 s ...
- centos6.5升级Linux内核步骤
centos6.5升级Linux内核步骤 http://www.jianshu.com/p/c75f00182b4c 使用的操作系统是是centos6.5,按照官方的推荐的配置,把linux内核升级到 ...