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. WinForm 子窗体在父窗体范围内移动,不能出父窗体 摘自于网络

    详细解释:1, 主窗体Form1属性IsMdiContainer设为True,并添加ToolStrip控件, Toolstrip中添加一个按钮toolStripButton1.         2,添 ...

  2. servlet容器开发要点

    v1 是一个http服务器. v2 是一个servlet容器, 可以提供servlet的服务.   =>  动态load servlet字节码,并运行它( 按生命周期). servlet容器它来 ...

  3. sqlserver内存释放

    由于Sql Server对于系统内存的管理策略是有多少占多少,除非系统内存不够用了(大约到剩余内存为4M左右),  Sql Server才会释放一点点内存.所以很多时候,我们会发现运行Sql Serv ...

  4. java transient修饰符

    1)一旦变量被transient修饰,变量将不再是对象持久化的一部分,该变量内容在序列化后无法获得访问. 2)transient关键字只能修饰变量,而不能修饰方法和类.注意,本地变量是不能被trans ...

  5. codeforces 689B Mike and Shortcuts 最短路

    题目大意:给出n个点,两点间的常规路为双向路,路长为两点之间的差的绝对值,第二行为捷径,捷径为单向路(第i个点到ai点),距离为1.问1到各个点之间的最短距离. 题目思路:SPFA求最短路 #incl ...

  6. 转:HTML错误编号大全

    HTML错误编号大全 状态行包含HTTP版本.状态代码.与状态代码对应的简短说明信息.在大多数情况下,除了Content-Type之外的所有应答头都是可选的.但Content-Type是必需的,它描述 ...

  7. Hibernate 乐观锁(Optimistic Locking)

    1.hibernate基于数据版本(Version)记录机制实现.为数据增加一个版本标识,一般是通过为数据库表增加一个"version"字段来实现. 读取出数据时,将此版本号一同读 ...

  8. MVC 使用Jquery的$.post传递参数

    MVC中,如果要使用 $.post 给 COntroller 传递参数,需要类实现 属性 get set,这样才行

  9. dfs和bfs的简单总结

    首先是dfs,又名深度优先搜索.看名字就知道,它的核心思想就是一直搜索,先在一条路上面一路撸到底,如果到底没有办法前进了,那么判断是否到达终点,如果没有到达,那么就回溯到之前的点再撸. dfs的要点: ...

  10. android:分享 一个很强大的LOG开关---Log.isLoggable

    标签:android分享 一个很强大的log开 1.API亮点: 此API可以实现不更换APK,在出问题的手机上就直接能抓到有效log,能提升不少工作效率. 2.API介绍 最近在解决短信问题时,看到 ...