POJ 2482 Stars in Your Window(线段树)
POJ 2482 Stars in Your Window
题意:给定一些星星,每一个星星都有一个亮度。如今要用w * h的矩形去框星星,问最大能框的亮度是多少
思路:转化为扫描线的问题,每一个星星转化为一个矩形,那么等于求矩形相交区域值最大的区域,利用线段树去维护就可以
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; typedef long long ll;
const int N = 10005; int n, w, h; struct Line {
ll l, r, y, c;
Line() {}
Line(ll l, ll r, ll y, ll c) {
this->l = l; this->r = r;
this->y = y; this->c = c;
}
} line[N * 2]; bool cmp(Line a, Line b) {
if (a.y == b.y) return a.c < b.c;
return a.y < b.y;
} ll hash[N * 2];
int hn; int find(ll x) {
return lower_bound(hash, hash + hn, x) - hash;
} #define lson(x) ((x<<1)+1)
#define rson(x) ((x<<1)+2) struct Node {
int l, r;
ll add, sum;
} node[N * 8]; void build(int l, int r, int x = 0) {
node[x].l = l; node[x].r = r;
node[x].add = node[x].sum = 0;
if (l == r) return;
int mid = (l + r) / 2;
build(l, mid, lson(x));
build(mid + 1, r, rson(x));
} void pushup(int x) {
if (node[x].l == node[x].r) node[x].sum = node[x].add;
else node[x].sum = max(node[lson(x)].sum, node[rson(x)].sum) + node[x].add;
} void add(int l, int r, ll c, int x = 0) {
if (node[x].l >= l && node[x].r <= r) {
node[x].add += c;
pushup(x);
return;
}
int mid = (node[x].l + node[x].r) / 2;
if (l <= mid) add(l, r, c, lson(x));
if (r > mid) add(l, r, c, rson(x));
pushup(x);
} int main() {
while (~scanf("%d%d%d", &n, &w, &h)) {
ll x1, y1, x2, y2, c;
for (int i = 0; i < n; i++) {
scanf("%lld%lld%lld", &x1, &y1, &c);
x2 = x1 + w; y2 = y1 + h;
hash[i * 2] = x1; hash[i * 2 + 1] = x2;
line[i * 2] = Line(x1, x2, y1, c);
line[i * 2 + 1] = Line(x1, x2, y2, -c);
}
n *= 2;
hn = 1;
sort(hash, hash + n);
for (int i = 1; i < n; i++)
if (hash[i] != hash[i - 1])
hash[hn++] = hash[i];
build(0, hn - 2);
sort(line, line + n, cmp);
ll ans = 0;
for (int i = 0; i < n; i++) {
add(find(line[i].l), find(line[i].r) - 1, line[i].c);
ans = max(ans, node[0].sum);
}
printf("%lld\n", ans);
}
return 0;
}
POJ 2482 Stars in Your Window(线段树)的更多相关文章
- 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 ...
- POJ 2482 Stars in Your Window 线段树
如果按一般的思路来想,去求窗户能框住的星星,就很难想出来. 如果换一个思路,找出每颗星星能被哪些窗户框住,这题就变得非常简单了. 不妨以每个窗户的中心代表每个窗户,那么每颗星星所对应的窗户的范围即以其 ...
- POJ 2482 Stars in Your Window (线段树区间合并+扫描线)
这题开始一直被矩形框束缚了,想法一直都是枚举线,但是这样枚举都需要O(n^2)...但是看了别人的思路,感觉这题思想真心很好(PS:开头好浪漫的描述啊,可惜并没有什么用) 题意就是在平面上给你一些星 ...
- POJ 2482 Stars in Your Window(线段树+扫描线)
题目链接 非常不容易的一道题,把每个点向右上构造一个矩形,将问题转化为重合矩形那个亮度最大,注意LL,注意排序. #include <cstdio> #include <cstrin ...
- poj 2482 Stars in Your Window(扫描线)
id=2482" target="_blank" style="">题目链接:poj 2482 Stars in Your Window 题目大 ...
- 【POJ-2482】Stars in your window 线段树 + 扫描线
Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11706 Accepted: ...
- poj 2482 Stars in Your Window + 51Nod1208(扫描线+离散化+线段树)
Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13196 Accepted: ...
- POJ 2482 Stars in Your Window 离散化+扫描法 线段树应用
遇见poj上最浪漫的题目..题目里图片以上几百词为一篇模板级英文情书.这情感和细腻的文笔深深地打动了我..不会写情书的童鞋速度进来学习.传送门 题意:坐标系内有n个星星,每个星星都有一个亮度c (1& ...
- POJ 2482 Stars in Your Window (线段树+扫描线+区间最值,思路太妙了)
该题和 黑书 P102 采矿 类似 参考链接:http://blog.csdn.net/shiqi_614/article/details/7819232http://blog.csdn.net/ts ...
随机推荐
- PHP基础入门(三)【PHP中的数组】
PHP数组的分类 按照下标的不同,PHP数组分为关联数组与索引数组: 索引数组:下标从0开始,依次增长: 关联数组: 下标为字符串格式,每个下标字符串与数组的值一一关联对应.(有点像对象的键值对) 关 ...
- 零基础如何迅速学习HTML5?新手小白学习web前端H5自白!
很多的人在毕业之后才发现原来学的专业不是自己想做的工作,或者专业对口的工作待遇让人觉得并不满意,于是很多人选择培训机构学新的一门技能转换行业.IT行业的web前端H5受到很多学员的青睐.那么学习web ...
- 关于帧动画steps属性的理解
CSS3的Animation有八个属性 animation-name animation-duration animation-delay animation-iteration-count anim ...
- 《Linux命令行与shell脚本编程大全》 第七章理解Linux文件权限
Linux沿用了Unix文件权限的方法,允许用户和组根据每个文件和目录的安全性设置来访问文件. 用户权限通过创建用户时分配的用户ID(UID)来跟踪的.每个用户有唯一的ID,但是登录时用的不是UID, ...
- Kotlin(二) 函数定义
1.不带参数,不返回值的函数 fun sum(){} 2.带参数,不带返回值的函数 fun sum(a:Int){} 3.带参数,带返回值的函数 fun sum(a:Int,b:Int) : Int{ ...
- Linux的chattr与lsattr命令详解
Linux的chattr与lsattr命令详解 这两个命令是用来查看和改变文件.目录属性的,与chmod这个命令相比,chmod只是改变文件的读写.执行权限,更底层的属性控制是由chattr来改变的. ...
- django作业2
管理后台 1.登陆Form 2.Session (用装饰器实现) 3.装饰器 4.主机,主机组 添加(主机,主机组) 删除 修改 查询
- JavaWeb面试(六)
51.说一说Servlet的生命周期? Servlet有良好的生存期的定义,包括加载和实例化.初始化.处理请求以及服务结束.这个生存期由javax.servlet.Servlet接口的init(),s ...
- 《Metasploit魔鬼训练营》第四章(下)
p163 XSSF 默认kali 2.0中没有xssf,先下载:https://code.google.com/archive/p/xssf/downloads 将下载下来的zip文件解压,将其中的d ...
- 实战Excel Add-in的三种玩法
作者:陈希章 发表于 2017年11月26日 前言 这个系列文章应该有一阵子没有更新了,原因是一如既往的多,但是根本所在是我对于某些章节其实还没有完全想好怎么写,尤其是对于Office Add-in这 ...