poj 2482 Stars in Your Window(扫描线)
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(扫描线)的更多相关文章
- POJ 2482 Stars in Your Window(线段树)
POJ 2482 Stars in Your Window 题目链接 题意:给定一些星星,每一个星星都有一个亮度.如今要用w * h的矩形去框星星,问最大能框的亮度是多少 思路:转化为扫描线的问题,每 ...
- 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 线段树扫描线
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,静态二叉树。
Description Here comes the problem: Assume the sky is a flat plane. All the stars lie on it with a l ...
- POJ 2482 Stars in Your Window(扫描线+线段树)
[题目链接] http://poj.org/problem?id=2482 [题目大意] 给出一些点的二维坐标和权值,求用一个长H,宽W的矩形能框住的最大权值之和, 在矩形边缘的点不计算在内 [题解] ...
- POJ 2482 Stars in Your Window (线段树区间合并+扫描线)
这题开始一直被矩形框束缚了,想法一直都是枚举线,但是这样枚举都需要O(n^2)...但是看了别人的思路,感觉这题思想真心很好(PS:开头好浪漫的描述啊,可惜并没有什么用) 题意就是在平面上给你一些星 ...
- POJ 2482 Stars in Your Window (线段树+扫描线+区间最值,思路太妙了)
该题和 黑书 P102 采矿 类似 参考链接:http://blog.csdn.net/shiqi_614/article/details/7819232http://blog.csdn.net/ts ...
- poj 2482 Stars in Your Window (线段树扫描线)
题目大意: 求一个窗体覆盖最多的星星的权值. 思路分析: 每个星星看成 左下点为x y 右上点为x+w-1 y+h-1 的矩形. 然后求出最大覆盖的和. #include <cstdio> ...
- POJ 2482 Stars in Your Window(线段树+扫描线)
题目链接 非常不容易的一道题,把每个点向右上构造一个矩形,将问题转化为重合矩形那个亮度最大,注意LL,注意排序. #include <cstdio> #include <cstrin ...
随机推荐
- const使用摘要
const在四种方案如以下: int b = 500; const int *a = &b; ①(底层const) int const *a = &b; ②(底层const) int ...
- poj 2369 Permutations 更换水称号
寻找循环节求lcm够了,如果答案是12345应该输出1.这是下一个洞. #include<iostream> #include<cstdio> #include<cstr ...
- bestcoder44#1002
这题采用分治的思想 首先,根据最后一位是否为1,将数分为两个集合, 集合与集合之间的lowbit为1, 然后将每个集合内的元素,倒数第二位是否为1,将数分为两个集合,集合与集合之间的lowbit为2 ...
- The example program of C on point
计划一: #include<stdio.h> #define N_VALUES 5 int main( void ) { float values[N_VALUES]; float *vp ...
- HDU-3502-Huson's Adventure Island(BFS+如压力DP)
Problem Description A few days ago, Tom was tired of all the PC-games, so he went back to some old F ...
- Memcached FAQ
这篇FAQ包含了大家普遍关心的问题.非常值得一看. 原文:http://blog.csdn.net/jarfield/archive/2009/07/05/4322953.aspx 最后更新时间 20 ...
- JDK源代码学习系列07----Stack
JDK源代码学习系列07----Stack 1.Stack源代码很easy ...
- Android 屏幕实现水龙头事件
在android下,事件的发生是在监听器下进行,android系统能够响应按键事件和触摸屏事件.事件说明例如以下: onClick(View v)一个普通的点击button事件 boolean onK ...
- VC版八皇后
一. 功能需求: 1. 可以让玩家摆棋,并让电脑推断是否正确 2. 能让电脑给予帮助(给出全部可能结果) 3. 实现悔棋功能 4. 实现重置功能 5. 加入点按键音效果更佳 二. 整体设计计: 1 ...
- ViewData ViewBag ViewModel
ViewBag 里可以携带dynamic的数据. Model 是从control传过来的模型数据. 我自己感觉ViewBag 可以携带少量的数据,但是我同事喜欢部分页partial 请求,ViewBa ...