結果

問題 No.151 セグメントフィッシング
ユーザー te-shte-sh
提出日時 2017-04-27 15:23:07
言語 D
(dmd 2.106.1)
結果
TLE  
実行時間 -
コード長 1,466 bytes
コンパイル時間 783 ms
コンパイル使用メモリ 88,892 KB
実行使用メモリ 8,760 KB
最終ジャッジ日時 2023-09-03 13:03:19
合計ジャッジ時間 7,595 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,388 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 7 ms
4,384 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.container; // SList, DList, BinaryHeap

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

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

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

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

    auto vl = pondL.get(pl(0)) - pondL.get(pl(-1));
    auto vr = pondR.get(pr(n-1)) - pondR.get(pr(n-2));
    if (vl) {
      pondL.add(pl(0), -vl); pondR.add(pr(-1), vl);
    }
    if (vr) {
      pondR.add(pr(n-1), -vr); pondL.add(pl(n), vr);
    }
  }
}

struct BiTree(T)
{
  T[] b; // buffer
  const size_t s; // size

  this(size_t t)
  {
    s = t;
    b = new T[](s + 1);
  }

  pure T get(size_t i) const
  {
    return i ? b[i] + get(i - (i & -i)) : 0;
  }

  void add(size_t i, T v)
  {
    if (i > s) return;
    b[i] += v;
    add(i + (i & -i), v);
  }
}
0