【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 ...
随机推荐
- 02-C#入门(循环)
记得第一次学编程的时候,就听说过一句话:“新手学习和掌握一门编程语言是很容易的,但是如果你学会了某种编程语言,再去学习新的语言,就很难掌握了”,现在深深地感受到了这方面的阻力. 其实流程控制.循环,包 ...
- linux mutext spinlock 性能分析
http://www.parallellabs.com/2010/01/31/pthreads-programming-spin-lock-vs-mutex-performance-analysis/ ...
- XShell 无法匹配的outgoing encryption算法 ,No matching outgoing encryption algorithm found
在链接的属性(SSH -> 安全性) 的加密算法列表中选择 aes256-ctr, mac加密列表中选择hmac-sha2-256,保存即可 To enable hmac-sha2-256 an ...
- 用gulp建立自动工具,完成软件的编译、测试、打包和发布流程
gulp以task的形式组织任务. 在每一个任务中,从gulp.src()指定文件源头开始,经过一系列pipe管道处理, 最后结果保存到gulp.dest指定的目录中,(或输出到stream) 任务的 ...
- 两系统用asp.net forms 身份验证方式实现跨域登录信息共享
1.两个系统的 web.config 都配置为 forms 验证方式( system.web —> authentication 节点) 2.在两个系统的Web.config里配置相同的 sys ...
- Linux 文件与目录管理
Linux 文件与目录管理 我们知道Linux的目录结构为树状结构,最顶级的目录为根目录 /. 其他目录通过挂载可以将它们添加到树中,通过解除挂载可以移除它们. 在开始本教程前我们需要先知道什么是绝对 ...
- jquery.validate[.unobtrusive]和Bootstrap实现tooltip错误提示
类似的文章园子里已有,请看这里,个人感觉稍显复杂,日前也打算写一个简单的给项目用,一些关键点记录于此.最终效果如下: 后端使用Asp.net mvc5,前端框架有:jquery.validate.jq ...
- 使用cnpm
官方的 npm下载速度太慢 [..................] \ normalizeTree: sill install loadCurrentTree 一直是这个问题 使用淘宝的 cpm ...
- [HTML]JS全屏代码
video全屏参考:https://www.thecssninja.com/javascript/fullscreen <!doctype html> <html> <h ...
- highcharts 柱状图 动态加载
highcharts柱状图动态加载 (1):导入样式 <script type="text/javascript" src="<%=request.getCo ...