【题目链接】 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(扫描线+线段树)的更多相关文章

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

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

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

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

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

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

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

    题目链接:http://poj.org/problem?id=2482 读完题干不免有些心酸(

  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(线段树+离散化+扫描线)

    [POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  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 线段树扫描线

    Stars in Your Window   Description Fleeting time does not blur my memory of you. Can it really be 4 ...

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

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

  10. POJ 2482 Stars in Your Window 离散化+扫描法 线段树应用

    遇见poj上最浪漫的题目..题目里图片以上几百词为一篇模板级英文情书.这情感和细腻的文笔深深地打动了我..不会写情书的童鞋速度进来学习.传送门 题意:坐标系内有n个星星,每个星星都有一个亮度c (1& ...

随机推荐

  1. CodeIgniter自带的数据库类使用介绍

    在 CodeIgniter 中,使用数据库是非常频繁的事情.你可以使用框架自带的数据库类,就能便捷地进行数据库操作. 初始化数据库类 依据你的数据库配置载入并初始化数据库类: view source ...

  2. 获取html元素内容

    html: <!DOCTYPE ><html> <head> <meta http-equiv="Content-Type" conten ...

  3. JavaScript中Array(数组)的属性和方法(转)

    数组有四种定义的方式 使用构造函数:var a = new Array();var b = new Array(8);var c = new Array("first", &quo ...

  4. 链接oracle数据库 生成表对应的javabean

    package com.databi.utils; import java.io.File; import java.io.FileOutputStream; import java.io.IOExc ...

  5. IE9,IE10 CSS因Mime类型不匹配而被忽略问题 (转)

    写页面的时候在chrome,fireforks等页面上显示正常,但是换成IE9,IE10之后就完全没有样式了,报错信息是CSS 因 Mime 类型不匹配而被忽略,下面与大家分享下这个问题的相关的回答 ...

  6. cdp协议通信并发编程基础之进程

    一 . 基于UDP的套接字 udp是无链接的所以先启动哪一段都不会报错 udp服务端 import socket server=socket.socket(socket.AF_INET,socket. ...

  7. Mac-配置SecureCRT

    1. Tools - Create Public Key - 2. Select Key Type 3. Create Passphrase 4.Input  Key Length 5. Genera ...

  8. 学习struts2

    有部分内容转载牛人的博客: http://blog.csdn.net/hudie1234567/article/details/6730481 http://blog.csdn.net/lishuan ...

  9. 编译opencv2.4.11时出现错误:error: ‘NppiGraphcutState’ has not been declared

    安装cuda之后再安装opencv时出现错误: /data/opencv-2.4.11/modules/gpu/src/graphcuts.cpp:120:54: error: ‘NppiGraphc ...

  10. Selenium2+python自动化34-获取百度输入联想词【转载】

    前言 最近有小伙伴问百度输入后,输入框下方的联想词如何定位到,这个其实难度不大,用前面所讲的元素定位完全可以定位到的. 本篇以百度输入框输入关键字匹配后,打印出联想词汇. 一.定位输入框联想词 1.首 ...