http://www.lydsy.com/JudgeOnline/problem.php?id=3524
http://www.lydsy.com/JudgeOnline/problem.php?id=2223
给定序列,询问区间内出现次数大于(r-l+1)/2的数
主席树
我发现我主席树的MDbug
const
maxn=500005;
var
root:array[0..maxn]of longint;
w:array[0..20*maxn,1..3]of longint;
i,j,k:longint;
n,m,len,a,b,ans:longint;
procedure build(pre:longint;var now:longint; val,l,r:longint);
var mid:longint;
begin
w[now,3]:=w[pre,3]+1; mid:=(l+r)>>1;
if l=r then exit;
if val<=mid
then begin w[now,2]:=w[pre,2]; inc(len); w[now,1]:=len; build(w[pre,1],w[now,1],val,l,mid); end
else begin w[now,1]:=w[pre,1]; inc(len); w[now,2]:=len; build(w[pre,2],w[now,2],val,mid+1,r) end;
end;
function query(pre,now,val,l,r:longint):longint;
var mid,t1,t2:longint;
begin
if l=r then exit(l);
mid:=(l+r)>>1;
t1:=w[w[now,1],3]-w[w[pre,1],3];
t2:=w[w[now,2],3]-w[w[pre,2],3];
if t1>val then exit(query(w[pre,1],w[now,1],val,l,mid));
if t2>val then exit(query(w[pre,2],w[now,2],val,mid+1,r));
exit(0);
end;
begin
fillchar(root,sizeof(root),0);
fillchar(w,sizeof(w),0);
readln(n,m); root[0]:=0; len:=0;
for i:=1 to n do
begin
read(a); inc(len); root[i]:=len;
build(root[i-1],root[i],a,1,n);
end;
for i:=1 to m do
begin
readln(a,b);
ans:=query(root[a-1],root[b],(b-a+1)div 2,1,n);
writeln(ans);
end;
end.