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 ...
随机推荐
- ionic3 打包安卓平台环境搭建报错解决方案总结
1.jvm虚拟机提供的运行空间小于项目所需的空间是报错.如图: 解决方法:在环境变量中配置jvm的运行内存大小,大于所需的内存即可. 其中:-Xmx512M可根据实际提示情况,进行更改,如1024M, ...
- ⑾bootstrap组件 徽章 大屏 页头 基础案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JavaScript系列----事件机制
1.事件流 1.1.标准事件流 所谓的标准事件流指的的:EMCAScript标准规定事件流包含三个阶段,分别为事件捕获阶段,处于目标阶段,事件冒泡阶段. 下面是一段html代码,根据代码来说明标准事件 ...
- elastic-search单机部署以及中文分词IKAnalyzer安装
前提条件 elasticsearch使用版本5.6.3,需要jdk版本1.8,低于该版本不能使用 下载 https://artifacts.elastic.co/downloads/elasticse ...
- nodeCZBK-笔记1
[TOC] ****************************** day01 node简介 Node.js是一个让JavaScript运行在服务器端的开发平台. node就是一个js的执行环境 ...
- js经典闭包
setTimeout函数之循环和闭包 前言 之前对于setTimeout的一个经典问题的理解总是感到很迷惑,现在好像清晰一点了,所以把我的理解写下来,我对js的理解也不深入,如果有错误,请务必指出.以 ...
- C重定向
- [转载] 谷歌技术"三宝"之谷歌文件系统
转载自http://blog.csdn.net/opennaive/article/details/7483523 题记:初学分布式文件系统,写篇博客加深点印象.GFS的特点是使用一堆廉价的商用计算机 ...
- From missionary to firebrand--Eisle Tu [20160102]
From missionary to firebrand 杜叶锡恩(1913年(癸丑年)-2015年(乙未年),英文名字Elsie Hume Elliot Tu,丈夫是教育家杜学魁.她是香港著名的 ...
- Fedora 23建立wifi热点(Android手机可用)
在ubuntu14.04下使用ap-hotspot,速度还不错.但是在15.04下就用不了了,不知为啥.现在使用fedora23,在学校还是挺需要给手机连wifi的,于是google看看ap-hots ...