【题目链接】 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. innodb log file与binlog的区别在哪里?

    Q: innodb log file与binlog的区别在哪里?有人说1.mysql的innodb引擎实际上是包装了inno base存储引擎.而innodb log file是由 inno base ...

  2. SICAU-OJ:要我唱几首歌才能够将你捕捉

    要我唱几首歌才能够将你捕捉 题意: 有N种颜色的牛,现在可以执行以下两种操作: 1.抓捕一只牛,代价为ai: 2.花费x的代价使用魔法,让所有颜色加1,N会变为1. 求得到N种颜色的牛最少花费的代价. ...

  3. 买卖股票的最佳时机 [ leetcode ]

    原题地址:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/description/ 给定一个数组,它的第 i 个 ...

  4. Struts2 内建的验证规则

    Struts2 内建的验证规则 conversion validator:转换验证器 date validator:日期验证器 double validator:浮点验证器 email validat ...

  5. java基础学习(一)hashcode

    hashcode的作用 hashCode()方法是从Object类继承过来的,Object类中的hashCode()方法返回的是对象在内存中地址转换成的int值,如果对象没有重写hashCode()方 ...

  6. 【sgu390】数位dp

    这题其实就是不断地合并子树,跟前面例一的思想是一样的. 这个打法我觉得非常优美啊(学别人的),为什么要搞lim1和lim2呢? 是因为在区间lim1~lim2之外的都是没有用的,但是我们f[h][su ...

  7. centos python2.6升级到2.7出现的问题

    centos自带的python版本为2.6,在使用tornado时会出现如下报错: NameError: global name 'memoryview' is not defined 因此需要升级到 ...

  8. linux中的阻塞机制及等待队列【转】

    转自:http://www.cnblogs.com/gdk-0078/p/5172941.html 阻塞与非阻塞是设备访问的两种方式.驱动程序需要提供阻塞(等待队列,中断)和非阻塞方式(轮询,异步通知 ...

  9. Make recursive

    folder structure: Makefile t1/Makefile t1/t1.c t2/Makefile t2/t2.c Makefile SUBDIRS = t1 t2 all: for ...

  10. 使用Tslib在触摸屏上显示汉字【转】

    转自:http://www.latelee.org/embedded-linux/use-tslib-to-display-chinese-character.html 终于到了在触摸屏上显示汉字了, ...