【POJ2482】Stars in Your Window(线段树,扫描线)
题意:在二维坐标系中有一些带权值的点,要求用一个长宽指定不能互换的框套住其中的一些,使得它们的权值和最大。
n<=10000 x,y<=2^31
思路:首先按X排序,将Y坐标离散化,X坐标用扫描线框定,每个点(x,y)在x中只对y有a[i]的贡献,y+h有-a[i]的贡献,线段树(树状数组更好写)维护最大子段和即可。
var t:array[..]of record
l,r,s,m:int64;
end;
x,y,c,a,h:array[..]of int64;
n,m,i,j,tt,ww,up,w1,h1:longint;
ans:int64; procedure swap(var x,y:int64);
var t:int64;
begin
t:=x; x:=y; y:=t;
end; procedure qsort(l,r:longint);
var i,j,mid1,mid2:longint;
begin
i:=l; j:=r; mid1:=x[(l+r)>>]; mid2:=y[(l+r)>>];
repeat
while (mid1>x[i])or((mid1=x[i])and(mid2>y[i])) do inc(i);
while (mid1<x[j])or((mid1=x[j])and(mid2<y[j])) do dec(j);
if i<=j then
begin
swap(x[i],x[j]);
swap(y[i],y[j]);
swap(a[i],a[j]);
inc(i); dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end; procedure qsort2(l,r:longint);
var i,j:longint;
mid:int64;
begin
i:=l; j:=r; mid:=c[(l+r)>>];
repeat
while mid>c[i] do inc(i);
while mid<c[j] do dec(j);
if i<=j then
begin
swap(c[i],c[j]);
inc(i); dec(j);
end;
until i>j;
if l<j then qsort2(l,j);
if i<r then qsort2(i,r);
end; function max(x,y:int64):int64;
begin
if x>y then exit(x);
exit(y);
end; procedure pushup(p:longint);
var ls,rs:longint;
begin
ls:=p<<; rs:=ls+;
t[p].l:=max(t[ls].l,t[ls].s+t[rs].l);
t[p].r:=max(t[rs].r,t[rs].s+t[ls].r);
t[p].m:=max(t[ls].r+t[rs].l,max(t[ls].m,t[rs].m));
end; procedure update(l,r,x,v,p:longint);
var mid:longint;
begin
if l=r then
begin
t[p].s:=t[p].s+v;
t[p].l:=t[p].s; t[p].r:=t[p].s; t[p].m:=t[p].s;
exit;
end;
mid:=(l+r)>>;
if x<=mid then update(l,mid,x,v,p<<)
else update(mid+,r,x,v,p<<+);
t[p].s:=t[p].s+v;
pushup(p);
end; function hash(x:int64):longint;
var l,r,mid,last:longint;
begin
l:=; r:=up; last:=;
while l<=r do
begin
mid:=(l+r)>>;
if x=h[mid] then begin last:=mid; r:=mid-; end;
if x<h[mid] then r:=mid-;
if x>h[mid] then l:=mid+;
end;
exit(last);
end; begin
assign(input,'poj2482.in'); reset(input);
assign(output,'poj2482.out'); rewrite(output);
while not eof do
begin
readln(n,w1,h1);
if n= then break;
for i:= to n do read(x[i],y[i],a[i]);
qsort(,n);
m:=;
for i:= to n do
begin
inc(m); c[m]:=y[i];
inc(m); c[m]:=y[i]+h1;
end;
qsort2(,m);
up:=; h[]:=c[];
for i:= to m do
if c[i]>c[i-] then
begin
if c[i]=c[i-]+ then begin inc(up); h[up]:=c[i]; end
else
begin
inc(up); h[up]:=c[i-];
inc(up); h[up]:=c[i];
end;
end;
ans:=-maxlongint;
tt:=; ww:=;
while ww<n do
begin
inc(ww);
while (tt<=n)and(x[tt]+w1-<x[ww]) do
begin
update(,up,hash(y[tt]),-a[tt],);
update(,up,hash(y[tt]+h1),a[tt],);
inc(tt);
end;
update(,up,hash(y[ww]),a[ww],);
update(,up,hash(y[ww]+h1),-a[ww],);
ans:=max(ans,t[].m);
end;
writeln(ans);
for i:= to up<< do
begin
t[i].s:=; t[i].l:=; t[i].r:=; t[i].m:=;
end;
end;
close(input);
close(output);
end.
【POJ2482】Stars in Your Window(线段树,扫描线)的更多相关文章
- 【POJ-2482】Stars in your window 线段树 + 扫描线
Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11706 Accepted: ...
- 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 ...
- POJ 2482 Stars in Your Window(线段树+扫描线)
题目链接 非常不容易的一道题,把每个点向右上构造一个矩形,将问题转化为重合矩形那个亮度最大,注意LL,注意排序. #include <cstdio> #include <cstrin ...
- POJ 2482 Stars in Your Window (线段树区间合并+扫描线)
这题开始一直被矩形框束缚了,想法一直都是枚举线,但是这样枚举都需要O(n^2)...但是看了别人的思路,感觉这题思想真心很好(PS:开头好浪漫的描述啊,可惜并没有什么用) 题意就是在平面上给你一些星 ...
- POJ 2482 Stars in Your Window 线段树
如果按一般的思路来想,去求窗户能框住的星星,就很难想出来. 如果换一个思路,找出每颗星星能被哪些窗户框住,这题就变得非常简单了. 不妨以每个窗户的中心代表每个窗户,那么每颗星星所对应的窗户的范围即以其 ...
- 【学习笔记】线段树—扫描线补充 (IC_QQQ)
[学习笔记]线段树-扫描线补充 (IC_QQQ) (感谢 \(IC\)_\(QQQ\) 大佬授以本内容的著作权.此人超然于世外,仅有 \(Luogu\) 账号 尚可膜拜) [学习笔记]线段树详解(全) ...
- 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)
D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)
题目链接:http://codeforces.com/contest/522/problem/D 题目大意: 给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...
- HDU 4419 Colourful Rectangle --离散化+线段树扫描线
题意: 有三种颜色的矩形n个,不同颜色的矩形重叠会生成不同的颜色,总共有R,G,B,RG,RB,GB,RGB 7种颜色,问7种颜色每种颜色的面积. 解法: 很容易想到线段树扫描线求矩形面积并,但是如何 ...
- BZOJ-3228 棋盘控制 线段树+扫描线+鬼畜毒瘤
3228: [Sdoi2008]棋盘控制 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 23 Solved: 9 [Submit][Status][D ...
随机推荐
- [python实现设计模式]-3.简单工厂模式-触宝开放平台
预备知识: 开放封闭原则(Open-Closed Principle OCP) Software entities(classes,modules,functions etc) should open ...
- sqlldr
1.字符集 sqlldr可以指定读取的文件的字符集,如果数据库为gbk,读取的文件为utf-8,这个时候就需要指定字符集 load data CHARACTERSET 'UTF8' 2.sqlldr导 ...
- ios 使用Core Image实现高斯模糊
在iOS和OS X平台上,Core Image都提供了大量的滤镜(Filter),这也是Core Image库中比较核心的东西之一.按照官方文档记载,在OS X上有120多种Filter,而在iOS上 ...
- C# List结果集排序
public class Student { public string Name { get; set; } public int Age { get; set; } public Student( ...
- Delphi 用文件流读取文本文件字符串的方法
procedure TForm23.Button4Click(Sender: TObject); var pstr:Pchar; mestr,Str1:string; FS:TFilestream; ...
- Unity已经学会的
1.以MonoBehaviour为脚本的开发模式. 2.MonoBehaviour的大多数API. 3.动画系统大多数了解. 4.UI了解一些,能写UI. 5.Editor了解一些,能写Editor. ...
- 通过innobackupex实现对MySQL的增量备份与还原
备份 增量备份是基于完整备份的,所以我们需要先做一次完整备份: innobackupex --password=test /backup/ 备注:test是我的MySQL服务的root用户的密码,/b ...
- Redis的持久化的两种方式drbd以及aof日志方式
redis的持久化配置: 主要包括两种方式:1.快照 2 日志 来看一下redis的rdb的配置选项和它的工作原理: save 900 1 // 表示的是900s内,有1条写入,则产生快照 save ...
- [Altera]PLL仿真
EDA Tools: 1.Quartus II 13.1(64-bit) 2.Modelsim SE-64 10.1c Time: 2016.05.05 ----------------------- ...
- 9.8 js进阶总结3
DOM文档对象模型 DOM(document object model)文档对象模型,它定义了操作文档对象的接口. DOM 把一份html文档表示为一棵家谱树,使用parent(父),child(子) ...