/CHOSO - Chọn số bằng nhau

<Problem>

http://ntucoder.net/Problem/Details/4402
              Uses Crt;
        type    arr=array[0..1000000] of int64;
        Var     n,i:longint; kq,da,db,dc:int64;
                a,vt:arr;
        Procedure Sort(var a:arr; l,r:longint);
        var i,j,x,tg:longint;
        begin
            i:=l; j:=r; x:=a[(l+r) div 2];
            repeat
                while x>a[i] do inc(i); while x<a[j] do dec(j);
                if i<=j then begin
                tg:=a[i]; a[i]:=a[j]; a[j]:=tg;
                tg:=vt[i]; vt[i]:=vt[j]; vt[j]:=tg;
                inc(i); dec(j);
                end;
            until i>j;
            if j>l then sort(a,l,j); if i<r then sort(a,i,r);
        end;
        Begin
             readln(n);
             for i:=1 to n do begin read(a[i]); vt[i]:=1; end;
             for i:=n+1 to 2*n do begin read(a[i]); vt[i]:=2; end;
             for i:=n*2+1 to 3*n do begin read(a[i]); vt[i]:=3; end;
             n:=n*3; sort(a,1,n);
             for i:=1 to n do
                if a[i]=a[i+1] then
                begin
                        if vt[i]=1 then inc(da);
                        if vt[i]=2 then inc(db);
                        if vt[i]=3 then inc(dc);
                end
                else
                begin
                        if vt[i]=1 then inc(da);
                        if vt[i]=2 then inc(db);
                        if vt[i]=3 then inc(dc);
                        kq:=kq+da*db*dc;
                        da:=0; db:=0; dc:=0;
                end;
                writeln(kq);
                readln;
        End.