把堆顶和堆顶接起来,然后用树状数组模拟即可
不得不说一开始我竟然把100000*100000当成不超出longint 而忘开int64,无药可救……
var a,b,c:array[..] of longint;
now,n1,n2,n,mid,i:longint;
ans:int64; procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;
a:=b;
b:=c;
end; function lowbit(x:longint):longint;
begin
exit(x and (-x));
end; procedure add(x:longint);
begin
while x<=n do
begin
dec(c[x]);
x:=x+lowbit(x);
end;
end; function ask(x:longint):longint;
begin
ask:=;
while x> do
begin
ask:=ask+c[x];
x:=x-lowbit(x);
end;
end; procedure sort(l,r: longint);
var i,j,x,y: longint;
begin
i:=l;
j:=r;
x:=a[(l+r) div ];
repeat
while a[i]<x do inc(i);
while x<a[j] do dec(j);
if not(i>j) then
begin
swap(a[i],a[j]);
swap(b[i],b[j]);
inc(i);
j:=j-;
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end; begin
readln(n1,n2);
n:=n1+n2;
for i:=n1 downto do
readln(a[i]);
for i:= to n2 do
readln(a[i+n1]);
now:=n1;
for i:= to n do
begin
b[i]:=i;
c[i]:=lowbit(i);
end;
sort(,n);
for i:=n downto do
begin
add(b[i]);
if b[i]>now then
ans:=ans+ask(b[i]-)-ask(now)
else
ans:=ans+ask(now)-ask(b[i]);
now:=b[i]-;
end;
writeln(ans);
end.