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

题目大意:平面上有N个星星。问一个W∗H的矩形最多能括进多少个星星。

解题思路:扫描线的变形。仅仅要以每一个点为左上角。建立矩形,这个矩形即为框框左下角放的位置能够括到该点,那么N个星星就有N个矩形,扫描线处理哪些位置覆盖次数最多。

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm> using namespace std; typedef long long ll;
const int maxn = 40000; #define lson(x) ((x)<<1)
#define rson(x) (((x)<<1)|1) int lc[maxn << 2], rc[maxn << 2];
ll v[maxn << 2], s[maxn << 2]; inline void pushup (int u) {
s[u] = max(s[lson(u)], s[rson(u)]) + v[u];
} inline void maintain (int u, int d) {
v[u] += d;
pushup(u);
} void build (int u, int l, int r) {
lc[u] = l;
rc[u] = r;
v[u] = s[u] = 0; if (l == r)
return; int mid = (l + r) / 2;
build(lson(u), l, mid);
build(rson(u), mid + 1, r);
pushup(u);
} void modify (int u, int l, int r, int d) {
if (l <= lc[u] && rc[u] <= r) {
maintain(u, d);
return;
} int mid = (lc[u] + rc[u]) / 2;
if (l <= mid)
modify(lson(u), l, r, d);
if (r > mid)
modify(rson(u), l, r, d);
pushup(u);
} struct Seg {
ll x, l, r, d;
Seg (ll x = 0, ll l = 0, ll r = 0, ll d = 0) {
this->x = x;
this->l = l;
this->r = r;
this->d = d;
}
friend bool operator < (const Seg& a, const Seg& b) {
return a.x < b.x;
}
}; int N, W, H;
vector<ll> pos;
vector<Seg> vec; inline int find (ll k) {
return lower_bound(pos.begin(), pos.end(), k) - pos.begin();
} void init () {
ll x, y, d; pos.clear();
vec.clear();
for (int i = 0; i < N; i++) {
scanf("%lld%lld%lld", &x, &y, &d);
pos.push_back(y - H);
pos.push_back(y);
vec.push_back(Seg(x - W, y - H, y, d));
vec.push_back(Seg(x, y - H, y, -d));
}
sort(pos.begin(), pos.end());
sort(vec.begin(), vec.end());
} ll solve () {
ll ret = 0;
build (1, 0, pos.size()); for (int i = 0; i < vec.size(); i++) {
modify(1, find(vec[i].l), find(vec[i].r) - 1, vec[i].d);
ret = max(ret, s[1]);
}
return ret;
} int main () {
while (scanf("%d%d%d", &N, &W, &H) == 3) {
init();
printf("%lld\n", solve());
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

poj 2482 Stars in Your Window(扫描线)的更多相关文章

  1. POJ 2482 Stars in Your Window(线段树)

    POJ 2482 Stars in Your Window 题目链接 题意:给定一些星星,每一个星星都有一个亮度.如今要用w * h的矩形去框星星,问最大能框的亮度是多少 思路:转化为扫描线的问题,每 ...

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

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

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

  4. (中等) POJ 2482 Stars in Your Window,静态二叉树。

    Description Here comes the problem: Assume the sky is a flat plane. All the stars lie on it with a l ...

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

    [题目链接] http://poj.org/problem?id=2482 [题目大意] 给出一些点的二维坐标和权值,求用一个长H,宽W的矩形能框住的最大权值之和, 在矩形边缘的点不计算在内 [题解] ...

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

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

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

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

  8. poj 2482 Stars in Your Window (线段树扫描线)

    题目大意: 求一个窗体覆盖最多的星星的权值. 思路分析: 每个星星看成 左下点为x y 右上点为x+w-1 y+h-1 的矩形. 然后求出最大覆盖的和. #include <cstdio> ...

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

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

随机推荐

  1. iOS随机颜色

    #import <UIKit/UIKit.h> @interface UIColor (RandomColor) +(UIColor *) randomColor; @end #impor ...

  2. 数据验证validator 与 DWZ

    在进行系统经常使用的数据验证.数据验证可以编写自己的,它也可以用来作为现在.现在,记录这两个库的使用, validator <!DOCTYPE HTML PUBLIC "-//W3C/ ...

  3. JS多语种方式

    方案: 在不同的移动平台(IOS.Android)上,并建立了HTML页面通信框架.主要业务逻辑HTML发展:我要支持多语言开发. 动机: 通过积极主动的信息方式,前一页完成初始化,获取当前语言选项. ...

  4. HTTP2.0协议

    HTTP2.0协议 http2协议的草案已经出来了,阅读了一下网上的中文版,http2尽可能的兼容http1.1.改进了http1.1协议的不足. http1.0和http1.1的缺点: 1.http ...

  5. URAL 1934 Black Spot --- 最短的简单修改

    右侧是1.维护的同时保持最短路p值至少,我有直接存款(1-p).该概率不满足,为了使这个值极大. #include <iostream> #include <cstdlib> ...

  6. HDU1698_Just a Hook(线段树/成段更新)

    解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...

  7. sql server基本流程语句

  8. 【原创】纯OO:从设计到编码写一个FlappyBird (二)

    第一部分请点这里. 续结前文,本文将实现Game类. 首先是实例变量.由上次的类图可以看出,Game类首先得具有如下实例变量: 0.Judge judge;1.SimpleDraw draw; // ...

  9. 7、Cocos2dx 3.0游戏开发找小三之3.0版本号的代码风格

    重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27691337 Cocos2d-x代码风格 前面我们已 ...

  10. ehcache.xml设置(转)

    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLoc ...