朋友

来源:互联网 发布:三国志9优化 编辑:程序博客网 时间:2024/05/01 15:16

【问题描述】

    mxy 即将前往新世界。

    在前往新世界的过程中,ta 遇见了两种人。一种是只和 lowb 做朋友,即当且仅当自己的能力值大于对方时他们会成为朋友,另一种是大神我们做朋友,即当且仅当自己的能力

值小于对方时他们会成为朋友。

    现在告诉你两种人的能力值。请你计算一共有多少对友好关系。

【输入】

    每组测试数据有三行。

    第一行有两个整数 mn(1 <= mn <= 20000),分别代表前者人数和后者人数。

    第二行 m 个数,第三行 n 个数,代表各自的能力值。

【输出】

   一行一个整数,表示有多少对友好关系。

【输入输出样例】

friend.in

5 3

8 1 7 3 1

3 6 1

friend.out

7



程序:

var
m,n,i,j,q,p:longint;
tj:int64;
a,b:array[0..20000]of longint;
procedure kp1(l,r:longint);
var
i,j,mid:longint;
begin
    if l>=r then exit;
    i:=l;j:=r;mid:=a[(l+r) div 2];
    repeat
         while a[i]>mid do inc(i);
         while a[j]<mid do dec(j);
         if i<=j then
         begin
             a[0]:=a[i];a[i]:=a[j];a[j]:=a[0];
             inc(i);dec(j);
         end;
    until i>j;
    kp1(l,j);
    kp1(i,r);
end;
procedure kp2(l,r:longint);
var
i,j,mid:longint;
begin
    if l>=r then exit;
    i:=l;j:=r;mid:=b[(l+r) div 2];
    repeat
         while b[i]>mid do inc(i);
         while b[j]<mid do dec(j);
         if i<=j then
         begin
             b[0]:=b[i];b[i]:=b[j];b[j]:=b[0];
             inc(i);dec(j);
         end;
    until i>j;
    kp2(l,j);
    kp2(i,r);
end;


begin
    readln(m,n);
    for i:=1 to m do
    read(a[i]);
    readln;
    for i:=1 to n do
    read(b[i]);
    kp1(1,m);
    kp2(1,n);
    tj:=0;
    for i:=1 to m do
    begin
        q:=1;p:=n;
        if b[q]<a[i] then
        begin
            inc(tj,n);
            continue;
        end;
        if b[p]>=a[i] then break;
        while a[i]<=b[q] do inc(q);
        if a[i]>b[q] then
        begin
            tj:=tj+(n-q+1);
            continue;
        end;
        if a[i]>b[p] then
        begin
            tj:=tj+(n-p+1);
            continue;
        end;
    end;
    write(tj);
end.