题意:在二维坐标系中有一些带权值的点,要求用一个长宽指定不能互换的框套住其中的一些,使得它们的权值和最大。
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.