結果

問題 No.742 にゃんにゃんにゃん 猫の挨拶
ユーザー ikdikd
提出日時 2018-10-05 23:06:37
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 17 ms / 2,500 ms
コード長 913 bytes
コンパイル時間 602 ms
コンパイル使用メモリ 84,256 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 21:09:52
合計ジャッジ時間 1,345 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 17 ms
4,376 KB
testcase_12 AC 16 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

void main() {
  import std.stdio, std.string, std.conv, std.algorithm;

  int n;
  rd(n);
  auto a = new int[](n);
  foreach (i; 0 .. n)
    rd(a[i]);
  int cnt = 0;
  auto tree = new FenwickTree(n);
  foreach (i; 0 .. n) {
    cnt += i - tree.sum(a[i]);
    tree.add(a[i], 1);
  }
  writeln(cnt);
}

class FenwickTree {
  int n;
  int[] dat;
  this(int n) {
    this.n = n;
    dat.length = n + 1;
  }

  void add(int i, int x) {
    for (int k = i; k <= n; k += k & (-k))
      dat[k] += x;
  }

  int sum(int i) { // [1, i]
    int s = 0;
    for (int k = i; k > 0; k -= k & (-k))
      s += dat[k];
    return s;
  }

  int sum(int i, int j) { // [i, j]
    return sum(j) - sum(i);
  }
}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

  auto l = readln.split;
  assert(l.length == x.length);
  foreach (i, ref e; x)
    e = l[i].to!(typeof(e));
}
0