結果

問題 No.59 鉄道の旅
ユーザー te-sh
提出日時 2018-01-10 14:57:19
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 28 ms / 5,000 ms
コード長 1,224 bytes
コンパイル時間 631 ms
コンパイル使用メモリ 99,888 KB
実行使用メモリ 8,612 KB
最終ジャッジ日時 2024-06-12 23:28:30
合計ジャッジ時間 1,845 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

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

T read1(T)(){return readln.chomp.to!T;}
void read2(S,T)(ref S a,ref T b){auto r=readln.splitter;a=r.front.to!S;r.popFront;b=r.front.to!T;}
const auto maxW = 1_000_000;

void main()
{
  int n, k; read2(n, k);

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

  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 == "+" || op == "-")
  {
    ++i;
    for (; i <= n; i += i & -i) mixin("buf[i] " ~ op ~ "= val;");
  }

  void opIndexUnary(string op)(size_t i) if (op == "++" || op == "--")
  {
    ++i;
    for (; i <= n; i += i & -i) mixin("buf[i]" ~ op ~ ";");
  }

  pure T opSlice(size_t r, size_t l) { return get(l) - get(r); }
  pure T opIndex(size_t i) { return opSlice(i, i+1); }
  pure size_t opDollar() { return n; }

private:

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