(题外话:这题这是ACMer的福利啊……)
我非常不擅长做矩形类的数据结构
一般来说,二维的问题我们要转化为一维来考虑
感觉一般的手法是对一维排序,并且线性扫描这一维,然后用各种数据结构维护另一维上的最优值
这道题我们首先对x排序,然后扫描x坐标
这时候我们要维护2条扫描线,一左一右(应该就是two pointer)
扫描线之间的长度就是要求小于等于窗口宽度
随着右扫描线的移动,我们把这条扫描线上的点加到某个数据结构中
随着左扫描线的移动,我们把这条扫描线的点删除
显然每个点添加一次且删除一次,要做O(n)次操作
下面我们要做的就是在给定的横坐标区域内,找出星星亮度和最大的窗口
显然这里横坐标区域中的每一个点我们都可以当作压缩在一条线上考虑
考虑求数列区间长度小于等于h的和的最大值,我们对于每一个位置x上的元素a[x]
在x+h的位置上加上-a[x],然后我们可以用线段树维护区间最大和
这个区间最大和长度一定是小于等于h的(想想为什么)
这里的线段树是单点修改,直接查询(难得不用lazy tag,但是要用lmax,rmax)
所以操作的复杂度为O(logn),查询O(1),总的复杂度为O(nlogn)
注意y的坐标很大,要离散化

 var tree,lmax,rmax,maxx,c,rank:array[..] of longint;
a:array[..] of int64;
x,y,s:array[..] of longint;
w,h,n,m,t,p,l,i,ans:longint; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;
a:=b;
b:=c;
end; procedure sorty(l,r: longint);
var i,j: longint;
x,y:int64;
begin
i:=l;
j:=r;
x:=a[(l+r) shr ];
repeat
while a[i]<x do inc(i);
while x<a[j] do dec(j);
if not(i>j) then
begin
y:=a[i];
a[i]:=a[j];
a[j]:=y; swap(c[i],c[j]);
inc(i);
j:=j-;
end;
until i>j;
if l<j then sorty(l,j);
if i<r then sorty(i,r);
end; procedure sortx(l,r: longint);
var i,j,z: longint;
begin
i:=l;
j:=r;
z:=x[(l+r) shr ];
repeat
while x[i]<z do inc(i);
while z<x[j] do dec(j);
if not(i>j) then
begin
swap(x[i],x[j]);
swap(y[i],y[j]);
swap(s[i],s[j]);
inc(i);
j:=j-;
end;
until i>j;
if l<j then sortx(l,j);
if i<r then sortx(i,r);
end; procedure work(i,l,r,x,z:longint);
var m:longint;
begin
if l=r then
begin
inc(tree[i],z);
lmax[i]:=tree[i];
rmax[i]:=tree[i];
maxx[i]:=tree[i];
end
else begin
m:=(l+r) shr ;
if x<=m then work(i*,l,m,x,z)
else work(i*+,m+,r,x,z);
inc(tree[i],z);
lmax[i]:=max(lmax[i*],tree[i*]+lmax[i*+]); //不多说
rmax[i]:=max(rmax[i*+],tree[i*+]+rmax[i*]);
maxx[i]:=max(lmax[i*+]+rmax[i*],max(maxx[i*],maxx[i*+]));
end;
end; begin
while not eof do
begin
readln(n,w,h);
for i:= to n do
readln(x[i],y[i],s[i]);
sortx(,n);
//先对x排序,后面离散化y坐标对应就轻松一点
for i:= to n do
begin
a[i]:=y[i];
a[i+n]:=int64(y[i])+int64(h);//当然y+h这个位置也要离散化
c[i]:=i;
c[i+n]:=i+n;
end;
t:=n*;
sorty(,t);
p:=;
rank[c[]]:=;
for i:= to t do
begin
if (a[i]<>a[i-]) then inc(p);
rank[c[i]]:=p;
end;
m:=p;
fillchar(tree,sizeof(tree),);
fillchar(lmax,sizeof(lmax),);
fillchar(rmax,sizeof(rmax),);
fillchar(maxx,sizeof(maxx),);
l:=;
ans:=;
for i:= to n do
begin
while (l<i) and (int64(x[l])+int64(w)<=x[i]) do
begin
work(,,m,rank[l],-s[l]); //删除左扫描线左边的点
work(,,m,rank[l+n],s[l]);
inc(l);
end;
work(,,m,rank[i+n],-s[i]); //右加入扫描线上的点
work(,,m,rank[i],s[i]);
ans:=max(ans,maxx[]);
end;
writeln(ans);
end;
end.

poj2482的更多相关文章

  1. 【POJ2482】Stars in Your Window(线段树,扫描线)

    题意:在二维坐标系中有一些带权值的点,要求用一个长宽指定不能互换的框套住其中的一些,使得它们的权值和最大. n<=10000 x,y<=2^31 思路:首先按X排序,将Y坐标离散化,X坐标 ...

  2. 【POJ2482】Stars in Your Window

    [POJ2482]Stars in Your Window 题面 vjudge 题解 第一眼还真没发现这题居然™是个扫描线 令点的坐标为\((x,y)\)权值为\(c\),则 若这个点能对结果有\(c ...

  3. 【POJ-2482】Stars in your window 线段树 + 扫描线

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

  4. poj2482 Stars in Your Window

    此题可用线段树或静态二叉树来做. 考虑用线段树: 很容易想到先限定矩形横轴范围再考虑在此纵轴上矩形内物品总价值的最大值. 那么枚举矩形横轴的复杂度是O(n)的,考虑如何快速获取纵轴上的最大值. 我们不 ...

  5. 【POJ2482】【线段树】Stars in Your Window

    Description Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw ...

  6. 【杂】poj2482 Stars in Your Windows 题面的翻译

    原地址:http://poj.org/problem?id=2482 神题,被誉为最浪漫的题目,一位acmer以自己独特的方式写下的殷殷情语 你窗前的星星 纵时光飞逝如梭,也我对你的回忆也永不黯然.从 ...

  7. Poj2482 Stars in Your Window(扫描线)

    题面 Poj 题解 下面内容引用自"李煜东 <算法竞赛进阶指南>"(对原文略有缩减,侵删): 因为矩形的大小固定,所以矩形可以由它的任意一个顶点唯一确定.我们可以考虑把 ...

  8. POJ2482 Stars in Your Window(扫描线+区间最大+区间更新)

    Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I stil ...

  9. POJ2482 Stars in Your Window 和 test20180919 区间最大值

    Stars in Your Window Language:Default Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K T ...

随机推荐

  1. Spring AOP体系学习总结:

    二.Spring AOP体系学习总结: 要理解AOP整体的逻辑需要理解一下Advice,Pointcut,Advisor的概念以及他们的关系. Advice是为Spring Bean提供增强逻辑的接口 ...

  2. jquery easyui easyui-treegrid 使用异步加载数据

    jquery easyui easyui-treegrid 使用异步加载数据 jquery easyui easyui-treegrid 异步请求 >>>>>>&g ...

  3. 第一篇:Mysql操作初级

    Mysql操作初级   Mysql操作初级 本节内容 数据库概述 数据库安装 数据库操作 数据表操作 表内容操作 1.数据库概述 数据库管理系统叫做DBMS 1.什么是数据库 ? 答:数据的仓库,如: ...

  4. C#中有关字符串去重的解决方案

    今天在群里看到一个同学的面试题 题目中有一个这样的要求 //本地有个文档文件a.txt里面包含的内容分为一段字符串"abacbacde"请编写一个程序,获取文件得到对应的内容,并对 ...

  5. [XML] C# XmlHelper操作Xml文档的帮助类 (转载)

    点击下载 XmlHelper.rar 主要功能如下所示 /// <summary> /// 类说明:XmlHelper /// 编 码 人:苏飞 /// 联系方式:361983679 // ...

  6. SQL使用存儲過程訪問不同服務器

    用openrowset连接远程SQL或插入数据 --如果只是临时访问,可以直接用openrowset --查询示例 select * from openrowset('SQLOLEDB', 'sql服 ...

  7. Css 梯形图形 并添加文字

    HTML页面的代码: <body> <div style="width:500px;border:solid 1px #ccc;"> <div> ...

  8. linux下安装多个mysql实例(摘自国外:How to create multiple mysql instance in CentOS 6.4 and Red Hat 6.4)

    How to create multiple mysql instance in CentOS 6.4 and Red Hat 6.4 from:http://sharadchhetri.com/20 ...

  9. jquery”ScriptResourceMapping

    要“jquery”ScriptResourceMapping.请添加一个名为 jquery (区分大小写)的 ScriptResourceMapping.”的解决办法. 1.先将aspnet.scri ...

  10. cmake简易教程

    用cmake替代makefile,构建项目还是蛮简单实用的. 工程目录下src放源代码,build保存所有的编译过程和结果. 首先看看src目录下的源代码结构: 最顶层CMakeLists.txt内容 ...