結果

問題 No.151 セグメントフィッシング
ユーザー te-sh
提出日時 2017-05-08 14:32:37
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 21 ms / 5,000 ms
コード長 1,623 bytes
コンパイル時間 818 ms
コンパイル使用メモリ 102,116 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-12 19:03:19
合計ジャッジ時間 1,703 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

  auto pondL = BiTree!long(n + q + 2);
  auto pondR = BiTree!long(n + q + 2);

  foreach (t; 0..q) {
    auto pl(size_t i) { return i + t + 2; }
    auto pr(size_t i) { return i - t + q + 1; }

    auto rd2 = readln.split;
    switch (rd2[0]) {
    case "L":
      auto y = rd2[1].to!size_t, z = rd2[2].to!long;
      pondL[pl(y)] += z;
      break;
    case "R":
      auto y = rd2[1].to!size_t, z = rd2[2].to!long;
      pondR[pr(y)] += z;
      break;
    case "C":
      auto y = rd2[1].to!size_t, z = rd2[2].to!size_t;
      auto nl = pondL[pl(y)..pl(z)];
      auto nr = pondR[pr(y)..pr(z)];
      writeln(nl + nr);
      break;
    default:
      assert(0);
    }

    auto vl = pondL[pl(0)];
    auto vr = pondR[pr(n-1)];
    if (vl) {
      pondL[pl(0)] += -vl; pondR[pr(-1)] += vl;
    }
    if (vr) {
      pondR[pr(n-1)] += -vr; pondL[pl(n)] += vr;
    }
  }
}

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);
  }

private:

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