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小技巧
1.js获取服务器年月日 var date= '<?php echo date("Y-m-d",time())?>';//获取服务器年月
- java模拟登陆功能
package test; import java.util.Scanner; public class Login { static Scanner sc=new Scanner(System.in ...
- mac电脑安装apache,不能启动
因为mac系统是自带apach的 如果安装正确还是不能启动,有可能是 mac电脑自带apache功能,与安装的apache冲突. 这样关闭Mac自带apach即可. mac电脑apache命令:重启a ...
- C#操纵Excel,此工作薄包含嵌入对象,Office 2007的设定方法
C#操纵Excel,插入OLE对象时报“此工作薄包含嵌入对象,EXCEL可能无法从这些对象中删除个人信息.”, 如网上所述,Office 2003可以通过“菜单>>工具>>选项 ...
- C#实现设置完整虚拟路径
){ mHttpUrl.Append(":"); mHttpUrl.Append(port);}string mServerName = "~/AppModu ...
- .net core 依赖注入扩展,实现随处控制反转
在使用.net core时,依赖注入,主要使用通过构造函数注入.小编将通过扩展方式,实现在类中各个地方可以控制反转,获取实例. 1.首先自定义扩展类 using Microsoft.AspNetCor ...
- SpringMV---params and headers
配置文件承接一二章 params params:请求的参数 params=value 表示请求过来的参数必须等于value params!=value 表示请求过来的参数必须不等 ...
- java中处理json各种各样转换方法
JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,非常适合于服务器与 JavaScript 的交互.本文将快速讲解 JSON 格式,并通过代码示例演示如 ...
- Ansible(一) 配置安装
puppet ruby开发 salt python开发,有客户端,使用Rabbitmq消息队列,支持并发,在机器数量很多时效果比ansible好. ansible python开发, 没有客户端,基于 ...
- ideal中如何添加几个不同的项目在同一个idea页面显示(同一个窗口显示多个工程)
今天,我遇到了一个问题,就是同事给了我一些项目,我下载了之后,项目有点多,然后想把这些项目都放到一个里面,所以我就采取了添加module的方式进行添加,首先先看一下我们的四个项目, 我们就想实现在一个 ...