pascal主席树模板
//主席树模板
var
i,n,m,total,ans,x,y,z:longint;
root:array[0..100000]of longint;
t:array[0..4000000,1..3]of longint;
procedure inw(x,y:longint;l,r:int64;z:longint);
var
mid:int64;
begin
if l<>r then
begin
mid:=(l+r)div 2;
if z<=mid then
begin
if t[x,2]>0 then t[y,2]:=t[x,2];
inc(total);
t[y,1]:=total;
if t[x,1]>0 then t[t[y,1]]:=t[t[x,1]];
inw(t[x,1],t[y,1],l,mid,z);
end
else
begin
if t[x,1]>0 then t[y,1]:=t[x,1];
inc(total);
t[y,2]:=total;
if t[x,2]>0 then t[t[y,2]]:=t[t[x,2]];
inw(t[x,2],t[y,2],mid+1,r,z);
end;
t[y,3]:=0;
if t[y,1]>0 then t[y,3]:=t[t[y,1],3];
if t[y,2]>0 then t[y,3]:=t[y,3]+t[t[y,2],3];
end
else inc(t[y,3]);
end;
procedure find(x,y:longint;l,r:int64;tc:longint);
var
mid:int64;
begin
if l<>r then
begin
mid:=(l+r)div 2;
if t[y,1]<>0 then
begin
if tc+t[t[y,1],3]-t[t[x,1],3]>=z then find(t[x,1],t[y,1],l,mid,tc)
else find(t[x,2],t[y,2],mid+1,r,tc+t[t[y,1],3]-t[t[x,1],3]);
end
else find(t[x,2],t[y,2],mid+1,r,tc);
end
else ans:=l;
end;
begin
assign(input,'sf_chairmantree.in');reset(input);
assign(output,'sf_chairmantree.out');rewrite(output);
readln(n,m);
for i:=1 to n do
begin
read(x);
inc(total);
root[i]:=total;
t[root[i]]:=t[root[i-1]];
inw(root[i-1],root[i],1,maxlongint,x);
end;
for i:=1 to m do
begin
readln(x,y,z);
ans:=0;
find(root[x-1],root[y],1,maxlongint,0);
writeln(ans);
end;
close(input);
close(output);
end.