結果

問題 No.59 鉄道の旅
ユーザー te-sh
提出日時 2017-05-08 14:17:25
言語 D
(dmd 2.109.1)
結果
WA  
実行時間 -
コード長 968 bytes
コンパイル時間 702 ms
コンパイル使用メモリ 101,808 KB
実行使用メモリ 8,616 KB
最終ジャッジ日時 2024-06-12 19:02:56
合計ジャッジ時間 1,751 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 6 WA * 4 RE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const auto maxW = 1_000_000;

void main()
{
  auto rd = readln.split, n = rd[0].to!size_t, k = rd[1].to!int;

  auto bt = BiTree!int(maxW);
  foreach (_; n.iota) {
    auto w = readln.chomp.to!int;
    if (w > 0) {
      if (bt[w..$] < k)
        bt[w] += 1;
    } else {
      w = -w;
      if (bt[w..w+1] > 0)
        bt[w] += -1;
    }
  }

  writeln(bt[0..$]);
}

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 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