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 ...
随机推荐
- [Luogu 2590] ZJOI2008 树的统计
[Luogu 2590] ZJOI2008 树的统计 裸树剖不解释. 比板子还简单. #include <algorithm> #include <cstdio> #inclu ...
- 鸡尾酒排序Cocktail sort
鸡尾酒排序基于冒泡排序,双向循环 还是看例子吧,给定待排数组[2 3 4 5 1] 第一趟过去时的每一步 第一步迭代,2 < 3不换 [2 3 4 5 1] 第二步迭代,3 < 4不换 [ ...
- 【51NOD-0】1134 最长递增子序列
[算法]动态规划 [题解]经典模型:最长上升子序列(n log n) #include<cstdio> #include<algorithm> #include<cstr ...
- 【洛谷 P3187】 [HNOI2007]最小矩形覆盖 (二维凸包,旋转卡壳)
题目链接 嗯,毒瘤题. 首先有一个结论,就是最小矩形一定有条边和凸包重合.脑补一下就好了. 然后枚举凸包的边,用旋转卡壳维护上顶点.左端点.右端点就好了. 上顶点用叉积,叉积越大三角形面积越大,对应的 ...
- 【HNOI】 攻城略池 tree-dp
[题目大意] 给定一棵树,边有边权,每个节点有一些兵,现在叶子节点在0时刻被占领,并且任意节点在x被占领,那么从x+1开始,每单位时间产生一个兵,兵会顺着父亲节点一直走到根(1),其中每经过一个节点, ...
- Python 模块搜索路径 -- (转)
最近在看<Python源码剖析>,对Python内部运行机制比以前了解的更深入了,感觉自己有机会也可以做个小型的动态脚本语言了,呵呵,当然是吹牛了.目的当然不是创造一个动态语言,目的只有一 ...
- Intel call指令
转载:http://blog.ftofficer.com/2010/04/n-forms-of-call-instructions/ 最近有一个需求,给你个地址,看看这个地址前面是不是一个CALL指令 ...
- TCP之listen&backlog
1. listen函数: #include <sys/socket.h> int listen(int sockfd, int backlog); ret-成功返回0 失败返回- list ...
- 【LabVIEW技巧】LabVIEW中的错误2
前言 通过上一个文章的介绍,我们发现LabVIEW自带的错误管理依旧比较基础,如果需要对错误进行很好的管理,则需要进一步的进行程序编写. 用于在程序设计的过程中,为了保证程序的健壮性,我们需要 1.忽 ...
- MySQL的sql_mode解析与设置
https://blog.csdn.net/hhq163/article/details/54140286 https://blog.csdn.net/ccccalculator/article/de ...