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. The window is a rectangle whose edges are parallel to the x-axis or y-axis. Your task is to tell where I should put the window in order to maximize the sum of the brightness of the stars within the window. Note, the stars which are right on the edge of the window does not count. The window can be translated but rotation is not allowed.
 
  题意就是给平面上的一些星星,然后用一个矩形去覆盖,能覆盖到的最大值。
 
  被这个题卡了一天,好坑,要注意数据范围,y坐标加上H就会超int了,然后就是用静态二叉树做的话注意建的树不一定是完全二叉树,所以要注意越界问题和内存空间问题,还有要把数组都清零。
 
  算法的话就是先以每个y和y+H建一个二叉树,然后从左到右,以每个点为最左边,然后把在带状范围内的加进提前建好的静态二叉树,其中y点加上c,y+H点减去c,然后二叉树维护子节点的和自己的值的和,以及最大的前缀和。
 
代码如下:
// ━━━━━━神兽出没━━━━━━
//    ┏┓ ┏┓
//   ┏┛┻━━━━━━━┛┻┓
//   ┃ ┃
//   ┃ ━ ┃
// ████━████ ┃
//   ┃ ┃
//   ┃ ┻ ┃
//   ┃ ┃
//   ┗━┓ ┏━┛
//    ┃ ┃
//    ┃ ┃
//    ┃ ┗━━━┓
//    ┃ ┣┓
//    ┃ ┏┛
//    ┗┓┓┏━━━━━┳┓┏┛
//     ┃┫┫ ┃┫┫
//     ┗┻┛ ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━ // Author : WhyWhy
// Created Time : 2015年07月16日 星期四 16时33分23秒
// File Name : 2482_1.cpp // 注意:建的不是完全二叉树,所以空间要开大,然后就是注意lc和rc的越界问题(不知如何判,所以直接初始化为0)。 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=; #define lc (po<<1)
#define rc ((po<<1)|1) struct DE
{
int a,b,c; bool operator < (const DE &b) const
{
return c<b.c;
}
}de[MaxN]; int decou=; struct BIT
{
int N;
long long key[MaxN],val[MaxN];
long long *numP; long long SUM[MaxN],MAXN[MaxN]; void _build(int L,int R,int po)
{
if(L>R)
return; de[decou].a=L;
de[decou].b=R;
de[decou].c=po;
++decou; int M=(L+R+)>>; _build(L,M-,lc); key[po]=numP[M];
val[po]=;
SUM[po]=MAXN[po]=; _build(M+,R,rc);
} void build(long long *num,int cou)
{
N=cou;
numP=num;
memset(SUM,,sizeof(SUM));
memset(MAXN,,sizeof(MAXN));
_build(,N,);
} void pushUP(int po)
{
/* long long Lnum,Rnum,Rmaxn,Lmaxn; if(lc>N)
Lnum=Lmaxn=0;
else
{
Lnum=SUM[lc];
Lmaxn=MAXN[lc];
} if(rc>N)
Rnum=Rmaxn=0;
else
{
Rnum=SUM[rc];
Rmaxn=MAXN[rc];
} SUM[po]=Lnum+Rnum+val[po];
MAXN[po]=max(Lmaxn,max(Lnum+val[po]+Rmaxn,Lnum+val[po]));*/ SUM[po]=SUM[lc]+SUM[rc]+val[po];
MAXN[po]=max(MAXN[lc],SUM[lc]+val[po]+max(0LL,MAXN[rc]));
} void _add(int po,long long k,long long num)
{
// if(po>N)
// return; if(k==key[po])
val[po]+=num;
else if(k<key[po])
_add(lc,k,num);
else
_add(rc,k,num); pushUP(po);
} void add(long long k,long long num)
{
_add(,k,num);
} long long query()
{
return MAXN[];
} void display()
{
for(int i=;i<=N;++i)
cout<<key[i]<<' '<<val[i]<<' '<<SUM[i]<<' '<<MAXN[i]<<endl;
cout<<endl;
}
}; BIT tree; struct Point
{
long long x,y,val; bool operator < (const Point &b) const
{
if(x==b.x)
return y<b.y; return x<b.x;
}
}; Point P[MaxN];
int N;
long long W,H;
int cou;
long long num[MaxN];
long long ans; long long getans()
{
if(W== || H==)
return ; int Lp,Rp; Lp=Rp=; for(int i=;i<=N;++i)
{
while(Lp<=N && P[Lp].x<P[i].x)
{
tree.add(P[Lp].y,-P[Lp].val);
tree.add(P[Lp].y+H,P[Lp].val);
++Lp;
} while(Rp<=N && P[Rp].x<P[i].x+W)
{
tree.add(P[Rp].y,P[Rp].val);
tree.add(P[Rp].y+H,-P[Rp].val);
++Rp;
} ans=max(ans,tree.query());
} return ans;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); while(~scanf("%d %lld %lld",&N,&W,&H))
{
ans=;
cou=; for(int i=;i<=N;++i)
{
scanf("%lld %lld %lld",&P[i].x,&P[i].y,&P[i].val);
num[++cou]=P[i].y;
num[++cou]=P[i].y+H;
} sort(P+,P+N+);
sort(num+,num+cou+);
cou=unique(num+,num+cou+)-num-; tree.build(num,cou); printf("%lld\n",getans());
} return ;
}

(中等) POJ 2482 Stars in Your Window,静态二叉树。的更多相关文章

  1. poj 2482 Stars in Your Window(扫描线)

    id=2482" target="_blank" style="">题目链接:poj 2482 Stars in Your Window 题目大 ...

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

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

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

    Stars in Your Window Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13196   Accepted:  ...

  4. 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 ...

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

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

  6. POJ 2482 Stars in Your Window(扫描线+线段树)

    [题目链接] http://poj.org/problem?id=2482 [题目大意] 给出一些点的二维坐标和权值,求用一个长H,宽W的矩形能框住的最大权值之和, 在矩形边缘的点不计算在内 [题解] ...

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

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

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

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

  9. POJ 2482 Stars in Your Window

    线段树+离散化+扫描线 AC之后,又认真读了一遍题目,好文章. #include<cstdio> #include<map> #include<algorithm> ...

随机推荐

  1. js的各种错误类型

    1.SyntaxError(语法错误) 解析代码时发生的语法错误 eg:var 1a; Uncaught SyntaxError: Unexpected number 2.ReferenceError ...

  2. Java中的设计模式

    1 单例模式和多例模式 一.单例模式和多例模式说明:1.         单例模式和多例模式属于对象模式.2.         单例模式的对象在整个系统中只有一份,多例模式可以有多个实例.(单例只会创 ...

  3. slf4j(simple logging facade for java)

    http://www.tuicool.com/articles/IfeUfq   slf4j(simple logging facade for java)是Java的简单的日志门面,它 不是具体的日 ...

  4. 【prim + kruscal 】 最小生成树模板

    来源:dlut oj 1105: Zhuo’s Dream Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 40 Solved: 14[Submit][St ...

  5. Firebase 相关

    谷歌在 2016年 I/O 大会上推出了 Firebase 的新版本.Firebase 平台提供了为移动端(iOS和Android)和 Web 端创建后端架构的完整解决方案. 从一开始的移动后端即服务 ...

  6. if __name__ == '__main__'在python中的应用

    当你打开一个.py文件时,经常会在代码的最下面看到if __name__ == '__main__':,现在就来介 绍一下它的作用. 模块是对象,并且所有的模块都有一个内置属性 __name__.一个 ...

  7. db2导入表结构和表数据

    http://www.cnblogs.com/kfarvid/archive/2010/12/15/1906776.html   db2的博文 -bash-3.2$ db2 connect to ca ...

  8. elasticsearch高级配置之(一)----分片分布规则设置

    cluster.routing.allocation.allow_rebalance 设置根据集群中机器的状态来重新分配分片,可以设置为always, indices_primaries_active ...

  9. 更方便的函数回调——Lambda

    auto callbackFunc = [&](){ backHome(); }; []符号,表示要开始一个lambda函数: ()符号,里面填写函数的参数: 当想在lambda函数里使用外部 ...

  10. haxe 配置

    安装所有类库: 命令提示符 haxelib install 类库名 如:haxelib install openfl 配置安卓: 命令提示符 haxelib run openfl setup andr ...