POJ 2482 Stars in Your Window(扫描线+线段树)
【题目链接】 http://poj.org/problem?id=2482
【题目大意】
给出一些点的二维坐标和权值,求用一个长H,宽W的矩形能框住的最大权值之和,
在矩形边缘的点不计算在内
【题解】
我们计算能扫到这个点的区间范围,将其拆分为两条平行于y轴的左闭右开的直线,
为方便边界处理,我们将坐标扩大两倍,之后我们按照x轴对这些线段进行扫描
统计出现的最大值即可。
【代码】
#include <cstdio>
#include <algorithm>
#include <utility>
using namespace std;
typedef long long LL;
const int N=10010;
LL xs[N],ys[N],X[N<<1],Y[N<<1];
int cs[N],tag[N<<3],T[N<<3];
pair<pair<int,int>,pair<int,int> >event[N<<1];
void update(int L,int R,int v,int x,int l,int r){
if(L<=l&&r<=R){T[x]+=v;tag[x]+=v;return;}
int mid=(l+r)>>1;
if(L<=mid)update(L,R,v,x<<1,l,mid);
if(mid<R)update(L,R,v,x<<1|1,mid+1,r);
T[x]=max(T[x<<1],T[x<<1|1])+tag[x];
}
int n,W,H;
void solve(){
for(int i=0;i<n;i++){
scanf("%lld%lld%d",xs+i,ys+i,cs+i);
xs[i]<<=1; ys[i]<<=1;
}
for(int i=0;i<n;i++){
X[i<<1]=xs[i]-W; X[i<<1|1]=xs[i]+W;
Y[i<<1]=ys[i]-H; Y[i<<1|1]=ys[i]-1+H;
}sort(X,X+n*2);sort(Y,Y+n*2);
for(int i=0;i<n;i++){
event[i<<1]=make_pair(make_pair(lower_bound(X,X+n*2,xs[i]-W)-X,cs[i]),make_pair(lower_bound(Y,Y+n*2,ys[i]-H)-Y,lower_bound(Y,Y+n*2,ys[i]+H-1)-Y));
event[i<<1|1]=make_pair(make_pair(lower_bound(X,X+n*2,xs[i]+W)-X,-cs[i]),make_pair(lower_bound(Y,Y+n*2,ys[i]-H)-Y,lower_bound(Y,Y+n*2,ys[i]+H-1)-Y));
}sort(event,event+n*2);
int ans=0;
for(int i=0;i<n*2;i++){
update(event[i].second.first,event[i].second.second,event[i].first.second,1,0,n*2);
ans=max(ans,T[1]);
}printf("%d\n",ans);
}
int main(){
while(~scanf("%d%d%d",&n,&W,&H))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 (线段树+扫描线+区间最值,思路太妙了)
该题和 黑书 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 (线段树:区间更新)
题目链接:http://poj.org/problem?id=2482 读完题干不免有些心酸(
- poj 2482 Stars in Your Window(扫描线)
id=2482" target="_blank" style="">题目链接:poj 2482 Stars in Your Window 题目大 ...
- 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)
[POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- 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 (线段树区间合并+扫描线)
这题开始一直被矩形框束缚了,想法一直都是枚举线,但是这样枚举都需要O(n^2)...但是看了别人的思路,感觉这题思想真心很好(PS:开头好浪漫的描述啊,可惜并没有什么用) 题意就是在平面上给你一些星 ...
- POJ 2482 Stars in Your Window 离散化+扫描法 线段树应用
遇见poj上最浪漫的题目..题目里图片以上几百词为一篇模板级英文情书.这情感和细腻的文笔深深地打动了我..不会写情书的童鞋速度进来学习.传送门 题意:坐标系内有n个星星,每个星星都有一个亮度c (1& ...
随机推荐
- springMvc--请求的跳转和传值
springMvc--请求的跳转和传值 目录 forword跳转页面的三种方式 1.使用serlvet 2.使用Model对象 3.使用ModelAndView redirect跳转到页面 使用ser ...
- codeforces 1060 D
https://codeforces.com/contest/1060/problem/D 题意:你可以用1个及以上的圆桌,给n个人排座位,每个人左边需要有Li个空凳子,右边需要有Ri个空凳子,问你最 ...
- es6+最佳入门实践(7)
7.set和map数据结构 7.1.什么是set? Set就是集合,集合是由一组无序且唯一的项组成,在es6中新增了set这种数据结构,有点类似于数组,但是它的元素是唯一的,没有重复 let st = ...
- 动态规划&字符串:最长公共子串
还是直接上转移方程: 动规只能解决O(n^2)的最长公共子串问题 使用后缀数组或者SAM可以高效地解决这个问题 所以,对于这个问题,动规的代码就不给出了 直接给出SAM的实现,也为以后学习SAM打下一 ...
- 从一段字符串中去除数字的shell方法
- MDIO/MDC(SMI)接口-leonwang202
ChinaUnix博客 http://blog.chinaunix.net/uid-24148050-id-132863.html
- 【BZOJ】1691: [Usaco2007 Dec]挑剔的美食家
[算法]扫描线+平衡树(set) [题解]很明显的二维偏序数点,排序后扫描线,现加点后查询答案. 则问题转化为一维偏序,显然贪心找第一个比当前大的最优,所以用平衡树维护. 记得开multiset!!! ...
- 联系人数据存储Demo源代码
源码下载地址:07-联系人数据存储.zip35.8 KB // MJPerson.h // // MJPerson.h // 07-联系人数据存储 // // Created by apple ...
- 不能使用联机NuGet 程序包
打开菜单栏>>工具>>选项>>NuGet 程序包管理器>>选择程序包源 勾选上nuget.org,即可使用联机的Nuget包
- Linux 格式化磁盘命令mkfs
linux格式化磁盘命令 mkfs 指令:mkfs 使用权限 : 超级使用者 使用方式 : mkfs [-V] [-t fstype] [fs-options] f ...