結果

問題 No.259 セグメントフィッシング+
ユーザー te-shte-sh
提出日時 2017-05-30 17:42:25
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 97 ms / 2,000 ms
コード長 1,747 bytes
コンパイル時間 619 ms
コンパイル使用メモリ 86,340 KB
実行使用メモリ 8,188 KB
最終ジャッジ日時 2023-09-03 13:43:08
合計ジャッジ時間 4,853 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 90 ms
7,204 KB
testcase_09 AC 88 ms
7,208 KB
testcase_10 AC 90 ms
7,608 KB
testcase_11 AC 90 ms
6,744 KB
testcase_12 AC 88 ms
7,132 KB
testcase_13 AC 95 ms
6,616 KB
testcase_14 AC 96 ms
7,248 KB
testcase_15 AC 95 ms
6,720 KB
testcase_16 AC 96 ms
6,792 KB
testcase_17 AC 93 ms
7,148 KB
testcase_18 AC 97 ms
6,744 KB
testcase_19 AC 96 ms
6,716 KB
testcase_20 AC 96 ms
8,188 KB
testcase_21 AC 96 ms
6,672 KB
testcase_22 AC 96 ms
7,200 KB
testcase_23 AC 68 ms
4,376 KB
testcase_24 AC 83 ms
7,460 KB
testcase_25 AC 84 ms
6,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

void main()
{
  auto rd1 = readln.split;
  auto n = rd1[0].to!int, n2 = n * 2;
  auto q = rd1[1].to!size_t;

  auto bt = BiTree!long(n2);

  auto summary(int t, int y, int z)
  {
    auto r = 0L;

    auto yl = ((y - t) % n2 + n2) % n2;
    auto zl = ((z - t) % n2 + n2) % n2;
    if (yl < zl) {
      r += bt[yl..zl];
    } else {
      r += bt[yl..$];
      r += bt[0..zl];
    }

    auto yr = ((n2 - z - t) % n2 + n2) % n2;
    auto zr = ((n2 - 1 - y - t) % n2 + n2) % n2 + 1;
    if (yr < zr) {
      r += bt[yr..zr];
    } else {
      r += bt[yr..$];
      r += bt[0..zr];
    }

    return r;
  }

  foreach (_; 0..q) {
    auto rd2 = readln.split;
    auto x = rd2[0], t = rd2[1].to!int;

    switch (x) {
    case "C":
      auto y = rd2[2].to!int, z = rd2[3].to!int;
      writeln(summary(t, y, z));
      break;
    case "R":
      auto y = rd2[2].to!int, z = rd2[3].to!long;
      bt[((y - t) % n2 + n2) % n2] += z;
      break;
    case "L":
      auto y = rd2[2].to!int, z = rd2[3].to!long;
      bt[((n2 - 1 - y - t) % n2 + n2) % n2] += z;
      break;
    default:
      assert(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)
  {
    ++i;
    for (; i <= n; i += i & -i)
      buf[i] += val;
  }

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

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

private:

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