POJ 2482 stars in your window(线段树 , 扫描线)

题目大意:

给你10000以内的星星的坐标和亮度,让你用一个W × H 的矩形去围住一个区域,使得区域内星星的亮度最大,矩形边缘上的星星不算。

解题思路:

对于每一个星星 建立一个(x, y , y + h , c) 的扫描线 和一个(x + w , y , y + h , - c)的扫描线,将问题转化成求区间最大值。几个需要注意的地方:矩形边缘上的需要处理一下,将每个叶节点设为长度为1的线段。另外此题如果用long long wa掉的话可以改为unsigned.

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#define LL unsigned
#define FOR(i,x,y) for(LL i=x;i<=y;i++)
#define rFor(i,x,y) for(LL i=x;i>=y;i--)
#define lson l , m , rt<<1
#define rson m , r , rt<<1|1
using namespace std;
const LL MAXN = 222222;
LL X[MAXN<<2];
int cnt[MAXN<<2] , MAX[MAXN<<2];
struct Seg
{
	LL h , l , r;
	int val;
	Seg() { }
	Seg(LL a , LL b , LL c , int d) : h(a) , l(b) , r(c) , val(d) { }
	bool operator < (const Seg& rhs) const
	{
		return h < rhs.h || (h == rhs.h && val < rhs.val);
	}
}ss[MAXN];
void pushdown(int rt)
{
	if(cnt[rt])
	{
		cnt[rt<<1] += cnt[rt];
		cnt[rt<<1|1] += cnt[rt];
		MAX[rt<<1] += cnt[rt];
		MAX[rt<<1|1] += cnt[rt];
		cnt[rt] = 0;
	}
}
void pushup(int rt)
{
	MAX[rt] = max(MAX[rt<<1] , MAX[rt<<1|1]);
}
void build(int l , int r , int rt)
{
	MAX[rt] = 0 ; cnt[rt] = 0;
	if(l + 1 == r) return ;
	int m = (l + r) >> 1;
	build(lson);
	build(rson);
}
void update(int L , int R , int val , int l , int r , int rt)
{
	if(L <= l && R >= r)
	{
		cnt[rt] += val;
		MAX[rt] += val;
		return ;
	}
	pushdown(rt);
	int m = (l + r) >> 1;
	if(L < m) update(L , R , val , lson);
	if(R > m) update(L , R , val , rson);
	pushup(rt);
}
int N , W , H;
int main()
{
	while(scanf("%d%d%d",&N , &W , &H) != EOF)
	{
		int m = 0;
		while(N--)
		{
			int x , y ,val;
			scanf("%d%d%d",&x,&y,&val);
			X[m] = y;
			ss[m++] = Seg(x , y , (LL)(y + H) , val);
			X[m] = (LL)(y + H);
			ss[m++] = Seg((LL)(x + W) , y , (LL)(y + H) ,-val);
		}
		sort(X , X + m);
		sort(ss , ss + m);
		int k = 1;
		for(LL i = 1 ; i < m ; i ++)
			if(X[i] != X[i - 1]) X[k++] = X[i];
		memset(cnt , 0 , sizeof(cnt));
		memset(MAX , 0 , sizeof(MAX));
		build(0 , k - 1  , 1);
		int ans = 0;
		for(int i = 0 ; i < m; i++)
		{
			int l = lower_bound(X , X + k , ss[i].l) - X;
			int r = lower_bound(X , X + k , ss[i].r) - X ;
			update(l , r , ss[i].val , 0 , k - 1 , 1);
			ans = max(ans , MAX[1]);
		}
		printf("%d\n",ans);
	}
	return 0;
}
时间: 2024-08-02 18:19:03

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 years since I first saw you? I still remember, vividly, on the beautiful Zhuhai Campus, 4 years ago, from the moment I saw you smile, as you were walkin

POJ 2482 Stars in Your Window 线段树+离散化+扫描线

题面据说很美- 每个星星可以根据在窗口的左下角和右上角两个位置建立两条扫描线,之后就是简单的区间增减和求最大值操作了. 注意要处理在边界上的星星是不算的情况,其实只要把左右边界分别增减一个eps即可. #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <

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 lo

poj 2482 Stars in Your Window(扫描线)

题目链接:poj 2482 Stars in Your Window 题目大意:平面上有N个星星,问一个W?H的矩形最多能括进多少个星星. 解题思路:扫描线的变形.只要以每个点为左上角,建立矩形,这个矩形即为框框左下角放的位置可以括到该点,那么N个星星就有N个矩形,扫描线处理哪些位置覆盖次数最多. #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using

poj 2482 Stars in Your Window (线段树:区间更新)

题目链接:http://poj.org/problem?id=2482 读完题干不免有些心酸(??????) 题意:有n个星星(星星i的坐标为xi, yi,亮度为ci),给你一个W*H的矩形,让你求得矩形能覆盖的星星的亮度和最大为多少 思路:矩形大小是固定的,所以可以换个方向思考,把矩形看成一个点(坐标为矩形中心),每个星星的影响区间范围为W*H的矩形(星星原来的坐标为矩形中心),该区间的亮度增加c.该问题就变成了求哪个点的亮度最大.坐标范围太大,直接暴力枚举不可能,所以需要预先进行离散化.在纵

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

Stars in Your Window Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10433   Accepted: 2874 Description Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I still remember, vividly, on the beaut

(中等) 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 location (x, y). for each star, there is a grade ranging from 1 to 100, representing its brightness, where 100 is the brightest and 1 is the weakest. T

POJ 2482 Stars in Your Window

线段树+离散化+扫描线 AC之后,又认真读了一遍题目,好文章. #include<cstdio> #include<map> #include<algorithm> using namespace std; const int maxn=100000+10; int n; long long W,H; long long x[maxn],y[maxn],v[maxn]; struct seg { long long X; long long Y1,Y2; long lo

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

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