bzoj2006

时间:2023-03-08 18:43:00

论将区间和转化为前缀和的重要性
这题一旦转化为前缀和就非常明了了
一段区间[l,r]的美妙程度就等于s[r]-s[l-1]
对于这种无法计算出所有方案而取前k大的题目,我们一般分类别然后利用类别内的单调性用堆维护
对于以i开头的区间,显然最美的长度在[l,r]之间的区间和
显然是max(s[i+l-1~i+r-1]),设这个最美的为k
次美的就是max(max(s[i+l-1~k-1]),max(s[k+1~i+r-1]),以此类推
不难想到用ST预处理区间前缀最大值
然后用大根堆维护开头为i,末端点位置在[p,q]上的最大值,
每次取出当前堆最大值,设在k取到最大值,就将这个区间裂位[p,k-1],[k+1,q]两个区间加入堆
一共进行k次,总的复杂度大约是O(klogn+nlogn)
讲的可能不是很清楚,具体见程序吧

 type node=record
tl,tr,st,loc:longint;
num:int64;
end; var heap:array[..] of node;
d:array[..] of longint;
f:array[..,..] of longint;
s:array[..] of longint;
n,k,l,r,i,j,x,y,t,p:longint;
ans:int64; function max(a,b:longint):longint;
begin
if s[a]>s[b] then exit(a) else exit(b);
end; function min(a,b:longint):longint;
begin
if a>b then exit(b) else exit(a);
end; function ask(x,y:longint):longint;
var k:longint;
begin
k:=trunc(ln(y-x+)/ln());
exit(max(f[x,k],f[y-d[k]+,k]));
end; procedure insert(i,x,y:longint);
begin
heap[t].tl:=x;
heap[t].tr:=y;
heap[t].st:=i;
heap[t].loc:=ask(x,y);
heap[t].num:=s[heap[t].loc]-s[i-];
end; procedure swap(var a,b:node);
var c:node;
begin
c:=a;
a:=b;
b:=c;
end; procedure up(i:longint);
var j:longint;
begin
j:=i shr ;
while j> do
begin
if heap[i].num>heap[j].num then
begin
swap(heap[i],heap[j]);
i:=j;
j:=i shr ;
end
else break;
end;
end; procedure sift(i:longint);
var j:longint;
begin
j:=i shl ;
while j<=t do
begin
if (j<t) and (heap[j].num<heap[j+].num) then inc(j);
if heap[i].num<heap[j].num then
begin
swap(heap[i],heap[j]);
i:=j;
j:=i shl ;
end
else break;
end;
end; begin
readln(n,k,l,r);
for i:= to n do
begin
readln(x);
s[i]:=s[i-]+x;
f[i,]:=i;
end;
t:=trunc(ln(n)/ln());
d[]:=;
for i:= to t do
d[i]:=d[i-]*;
for j:= to t do
for i:= to n do
if i+d[j]-<=n then
f[i,j]:=max(f[i,j-],f[i+d[j-],j-])
else break;
t:=;
for i:= to n-l+ do
begin
inc(t);
insert(i,i+l-,min(i+r-,n));
up(i);
end;
for i:= to k do
begin
p:=heap[].loc;
x:=heap[].tl;
y:=heap[].tr;
j:=heap[].st;
ans:=ans+int64(heap[].num);
swap(heap[],heap[t]);
dec(t);
sift();
if p>x then
begin
inc(t);
insert(j,x,p-);
up(t);
end;
if p<y then
begin
inc(t);
insert(j,p+,y);
up(t);
end;
end;
writeln(ans);
end.