记录编号 39364 评测结果 AAAAAAAAAA
题目名称 数列 最终得分 100
用户昵称 GravatarIMSL77 是否通过 通过
代码语言 Pascal 运行时间 0.050 s
提交时间 2012-07-09 16:17:43 内存使用 1.39 MiB
显示代码纯文本
program queueb;
type
  integer=longint;
const
  maxn=51000;
  maxv=32768;
var
  n:integer;
  a:array[1..maxn] of integer;
  sum:array[0..maxv] of int64;
  prev,next:array[1..maxn] of int64;

  procedure Fopen;
  begin
    assign(input,'queueb.in'); reset(input);
    assign(output,'queueb.out'); rewrite(output);
  end;

  procedure Fclose;
  begin
    close(input); close(output);
  end;

  procedure Init;
  var
    i:integer;
  begin
    readln(n);
    for i:=1 to n do
    begin
      read(a[i]); inc(a[i]);
    end;
  end;

  function lowbit(x:integer):integer;
  begin
    exit(x and (-x));
  end;

  function query(k:integer):int64;
  var
    res:int64;
  begin
    res:=0;
    while k>0 do
    begin
      res:=res+sum[k];
      dec(k,lowbit(k));
    end;
    exit(res);
  end;

  procedure update(k:integer);
  begin
    while k<=maxv do
    begin
      inc(sum[k]);
      inc(k,lowbit(k));
    end;
  end;

  procedure Solve;
  var
    i:integer;
    ans:int64;
  begin
    fillchar(sum,sizeof(sum),0);
    for i:=1 to n do
    begin
      prev[i]:=query(a[i]-1);
      update(a[i]);
    end;
    fillchar(sum,sizeof(sum),0);
    for i:=n downto 1 do
    begin
      next[i]:=query(a[i]-1);
      update(a[i]);
    end;
    ans:=0;
    for i:=1 to n do ans:=ans+prev[i]*next[i];
    writeln(ans);
  end;

begin
  Fopen;
  Init;
  Solve;
  Fclose;
end.