题意:n个音阶,每个音阶敲bi次,从0时刻开始。即[0,b1-1]敲第一个音阶,[b1,b1+b2-1]敲第二个,以此类推。给定m个询问,查询t时刻敲的音阶是第几个
显然要求O(mlogn)的复杂度,那么应该就是二分
对于所敲音阶i的时间,维护一个前缀和sum[i]
对于每个询问,二分找出所敲的音阶
var
m,n,t,ans :longint;
b,sum :array[0..50010] of longint;
i :longint;
procedure work(x:longint);
var
l,r,mid:longint;
begin
l:=0; r:=n;
while (l<=r) do
begin
mid:=(l+r)>>1;
if sum[mid]>=x then
begin
ans:=mid; r:=mid-1;
end else l:=mid+1;
end;
end;
begin
read(n,m);
for i:=1 to n do read(b[i]);
sum[0]:=-1;
for i:=1 to n do sum[i]:=sum[i-1]+b[i];
for i:=1 to m do
begin
read(t);
work(t);
writeln(ans);
end;
end.
——by Eirlys