把堆顶和堆顶接起来,然后用树状数组模拟即可
不得不说一开始我竟然把100000*100000当成不超出longint 而忘开int64,无药可救……
1 var a,b,c:array[0..100010] of longint; 2 now,n1,n2,n,mid,i:longint; 3 ans:int64; 4 5 procedure swap(var a,b:longint); 6 var c:longint; 7 begin 8 c:=a; 9 a:=b; 10 b:=c; 11 end; 12 13 function lowbit(x:longint):longint; 14 begin 15 exit(x and (-x)); 16 end; 17 18 procedure add(x:longint); 19 begin 20 while x<=n do 21 begin 22 dec(c[x]); 23 x:=x+lowbit(x); 24 end; 25 end; 26 27 function ask(x:longint):longint; 28 begin 29 ask:=0; 30 while x>0 do 31 begin 32 ask:=ask+c[x]; 33 x:=x-lowbit(x); 34 end; 35 end; 36 37 procedure sort(l,r: longint); 38 var i,j,x,y: longint; 39 begin 40 i:=l; 41 j:=r; 42 x:=a[(l+r) div 2]; 43 repeat 44 while a[i]<x do inc(i); 45 while x<a[j] do dec(j); 46 if not(i>j) then 47 begin 48 swap(a[i],a[j]); 49 swap(b[i],b[j]); 50 inc(i); 51 j:=j-1; 52 end; 53 until i>j; 54 if l<j then sort(l,j); 55 if i<r then sort(i,r); 56 end; 57 58 begin 59 readln(n1,n2); 60 n:=n1+n2; 61 for i:=n1 downto 1 do 62 readln(a[i]); 63 for i:=1 to n2 do 64 readln(a[i+n1]); 65 now:=n1; 66 for i:=1 to n do 67 begin 68 b[i]:=i; 69 c[i]:=lowbit(i); 70 end; 71 sort(1,n); 72 for i:=n downto 2 do 73 begin 74 add(b[i]); 75 if b[i]>now then 76 ans:=ans+ask(b[i]-1)-ask(now) 77 else 78 ans:=ans+ask(now)-ask(b[i]); 79 now:=b[i]-1; 80 end; 81 writeln(ans); 82 end.