結果

問題 No.121 傾向と対策:門松列(その2)
ユーザー te-shte-sh
提出日時 2017-05-08 14:25:36
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,395 ms / 5,000 ms
コード長 1,351 bytes
コンパイル時間 870 ms
コンパイル使用メモリ 95,716 KB
実行使用メモリ 120,880 KB
最終ジャッジ日時 2023-09-03 13:11:05
合計ジャッジ時間 5,865 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
9,664 KB
testcase_01 AC 60 ms
10,696 KB
testcase_02 AC 6 ms
4,380 KB
testcase_03 AC 482 ms
47,820 KB
testcase_04 AC 1,395 ms
120,880 KB
testcase_05 AC 482 ms
47,456 KB
testcase_06 AC 356 ms
47,688 KB
testcase_07 AC 427 ms
48,700 KB
testcase_08 AC 518 ms
51,316 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.d(10): Deprecation: foreach: loop index implicitly converted from `size_t` to `int`

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

void main()
{
  auto n = readln.chomp.to!size_t;
  auto ai = readln.split.to!(int[]);

  auto bi = ai.dup.sort().array.uniq.array;
  int[int] ci;
  foreach (int i, b; bi) ci[b] = i + 1;

  auto di = ai.map!(a => ci[a]).array;
  auto maxD = di.maxElement;

  auto bt1 = BiTree!long(maxD + 1);
  bt1[di[0]] += 1;

  auto bt2 = BiTree!long(maxD + 1);
  foreach (d; di[2..$]) bt2[d] += 1;

  auto ne = bt2[di[0]];
  auto r = 0L;

  foreach (i; 1..n-1) {
    auto d1 = di[i], d2 = di[i+1];

    r += bt1[0..d1] * bt2[0..d1];
    r += bt1[d1+1..$] * bt2[d1+1..$];
    r -= ne - bt1[d1] * bt2[d1];

    bt1[d1] += 1;
    ne += bt2[d1];
    bt2[d2] += -1;
    ne -= bt1[d2];
  }

  writeln(r);
}

struct BiTree(T)
{
  const size_t n;
  T[] buf;

  this(size_t n)
  {
    this.n = n;
    this.buf = new T[](n + 1);
  }

  void opIndexOpAssign(string op)(T val, size_t i)
    if (op == "+")
  {
    ++i;
    while (i <= n) {
      buf[i] += val;
      i += i & -i;
    }
  }

  pure size_t opDollar() { return n; }

  pure T opIndex(size_t i) const { return opSlice(i, i+1); }

  pure T opSlice(size_t r, size_t l) const
  {
    return get(l) - get(r);
  }

  pure T get(size_t i) const
  {
    auto s = T(0);
    while (i > 0) {
      s += buf[i];
      i -= i & -i;
    }
    return s;
  }
}
0