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(线段树)的更多相关文章

  1. 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 ...

  2. POJ 2482 Stars in Your Window 线段树

    如果按一般的思路来想,去求窗户能框住的星星,就很难想出来. 如果换一个思路,找出每颗星星能被哪些窗户框住,这题就变得非常简单了. 不妨以每个窗户的中心代表每个窗户,那么每颗星星所对应的窗户的范围即以其 ...

  3. POJ 2482 Stars in Your Window (线段树区间合并+扫描线)

    这题开始一直被矩形框束缚了,想法一直都是枚举线,但是这样枚举都需要O(n^2)...但是看了别人的思路,感觉这题思想真心很好(PS:开头好浪漫的描述啊,可惜并没有什么用)  题意就是在平面上给你一些星 ...

  4. POJ 2482 Stars in Your Window(线段树+扫描线)

    题目链接 非常不容易的一道题,把每个点向右上构造一个矩形,将问题转化为重合矩形那个亮度最大,注意LL,注意排序. #include <cstdio> #include <cstrin ...

  5. poj 2482 Stars in Your Window(扫描线)

    id=2482" target="_blank" style="">题目链接:poj 2482 Stars in Your Window 题目大 ...

  6. 【POJ-2482】Stars in your window 线段树 + 扫描线

    Stars in Your Window Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11706   Accepted:  ...

  7. poj 2482 Stars in Your Window + 51Nod1208(扫描线+离散化+线段树)

    Stars in Your Window Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13196   Accepted:  ...

  8. POJ 2482 Stars in Your Window 离散化+扫描法 线段树应用

    遇见poj上最浪漫的题目..题目里图片以上几百词为一篇模板级英文情书.这情感和细腻的文笔深深地打动了我..不会写情书的童鞋速度进来学习.传送门 题意:坐标系内有n个星星,每个星星都有一个亮度c (1& ...

  9. POJ 2482 Stars in Your Window (线段树+扫描线+区间最值,思路太妙了)

    该题和 黑书 P102 采矿 类似 参考链接:http://blog.csdn.net/shiqi_614/article/details/7819232http://blog.csdn.net/ts ...

随机推荐

  1. mysql查询锁表及解锁

    SHOW PROCESSLIST; KILL ; 锁表网上解释: 这牵涉到mysql的事务,简单通俗的话,就这样给你解释有一个任务序列控制sql语句的执行,第一次有select的语句查询表a,mysq ...

  2. oracle数据库管理系统常见的错误(一)

    oracle数据库管理系统常见的错误之一如下: Listener refused the connection with the following error:ORA-12519, TNS:no a ...

  3. maven项目导出依赖的Jar包以及项目本身以jar包形式导出详细教程

    一.maven项目已jar包形式导出 1.首先右键项目,选择Export 2.选择好项目,设置导出路径和jar名字即可: 二.导出maven项目所依赖的所有jar包 1.右键项目,选择Export 2 ...

  4. vimgdb安装以及使用

    vimgdb安装 vim-7.3.tar.bz2http://www.vim.org/sources.phpvimgdb-for-vim7.3 (this patch) https://github. ...

  5. Python源码分析

  6. javascript第六章--BOM

    ① window对象 ② location对象 ③ navigator对象 ④ screen对象 ⑤ history对象

  7. java-基础-泛型

    java泛型通配符问题.   java中的泛型基本用法参考<java编程思想>第四版 p.353 java泛型中比较难理解的主要是类型擦除和通配符相关.   1.类型擦除 在编译期间,类型 ...

  8. Maven工程下报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

    Maven工程下,webapp下面新建index.jsp文件,报如下错误. 原因很简单,没有安装如下maven依赖包: <dependencies> <!-- JSP相关 --> ...

  9. Python StringIO与BytesIO、类文件对象

    StringIO与BytesIO StringIO与BytesIO.类文件对象的用途,应用场景,优.缺点. StringIO StringIO 是io 模块中的类,在内存中开辟的一个文本模式的buff ...

  10. _1Python简介 安装及版本检测

    简介 Python是一种面向对象的解释性计算机程序设计语言,由荷兰人Guido von Rossum于1988年的圣诞节发明,第一个公开发行版于1991年. Python崇尚优美.清晰.简单,是一个优 ...