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. JavaScript正则表达式知识点

    通过学习imooc课程<JavaScript正则表达式>http://www.imooc.com/video/12539,对视频教学内容做一个知识整理. 一个正则表达式在线工具:http: ...

  2. kickstart 实现批量安装centos7.x系统

    1.1 安装系统的方法 l  光盘(ISO文件,光盘的镜像文件)===>>每一台物理机都得给一个光驱,如果用外置光驱的话,是不是每台机器都需要插一下 l  U盘:ISO镜像刻录到U盘==& ...

  3. pgjdbc源码分析

    一. 源代码目录结构 pgjdbc的源码结构如下图: 那么我们来一一看看各个模块都是做什么的吧. 1 core 该目录是程序的核心模块目录. 这里实现了大部分pgjdbc的基类和接口,例如statem ...

  4. POJ1082食物链

    加权并查集入门习题. 传送门http://poj.org/problem?id=1182 下面来记录一下做法: 并查集的作用是询问两个对象时候在同一集合以及将两个非空不相交集合合并. 本题涉及两点之间 ...

  5. windows下安装和redis主从配置(通过哨兵控制主从切换)

    首先自己先得了解什么是redis,这里就不详做介绍什么是redis了,这篇文章主要讲的是怎么样配置 redis怎样配置主从关系和哨兵控制主从服务器的配置以及应用,就当是给自己记笔记吧! 1.下载red ...

  6. JavaEE中的MVC(四)AOP代理

    咱们来吹牛,JDK的动态代理在AOP(Aspect Oriented Programming,面向切面编程)中被称为AOP代理,而AOP是Spring框架中的重要组成部分. 代理模式 但是什么是代理模 ...

  7. 网络分析法(Analytic Network Process,ANP)

    什么是网络分析法 网络分析法(ANP)是美国匹兹堡大学的T.L.Saaty教授于1996年提出的一种适应非独立的递阶层次结构的决策方法,它是在层次分析法(Analytic Hierarchy Proc ...

  8. gulp 小坑一个

    学习gulp的时候,看到很多人写到 gulp.task('styles', function() { return gulp.src('src/styles/main.scss') .pipe(sas ...

  9. 熟悉Objective—C

    1.oc使用消息结构而非函数调用 使用消息结构的语言,运行时所应执行的代码由运行环境来决定,而是用函数调用的语言,则由编译器决定.函数调用:如果调用的函数是多态的,那么在运行时就要按照“虚方法表”来查 ...

  10. JAVA的向上转型和向下转型怎么理解呢?

    在定义中是子类向父类转型称为向上转型,父类向子类转型是向下转型(必须先向上转型过,才能向下转型), 但是在下面类定义后,我得到的结果却不同.求大佬解惑 class superclass{ public ...