結果

問題 No.631 Noelちゃんと電車旅行
ユーザー te-shte-sh
提出日時 2018-01-09 14:01:07
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 303 ms / 2,000 ms
コード長 2,449 bytes
コンパイル時間 2,845 ms
コンパイル使用メモリ 89,044 KB
実行使用メモリ 13,388 KB
最終ジャッジ日時 2023-09-03 17:56:23
合計ジャッジ時間 6,906 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 171 ms
4,380 KB
testcase_01 AC 303 ms
12,004 KB
testcase_02 AC 291 ms
12,828 KB
testcase_03 AC 298 ms
12,584 KB
testcase_04 AC 302 ms
11,964 KB
testcase_05 AC 297 ms
13,388 KB
testcase_06 AC 121 ms
7,360 KB
testcase_07 AC 75 ms
7,784 KB
testcase_08 AC 230 ms
12,104 KB
testcase_09 AC 258 ms
11,196 KB
testcase_10 AC 195 ms
11,480 KB
testcase_11 AC 173 ms
9,436 KB
testcase_12 AC 207 ms
11,828 KB
testcase_13 AC 179 ms
6,952 KB
testcase_14 AC 69 ms
4,376 KB
testcase_15 AC 161 ms
6,524 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

void main()
{
  auto n = readln.chomp.to!int;
  auto t = readln.split.to!(int[]);

  auto st = SegmentTreeLazy!(long, max)(n);
  foreach (i; 0..n-1) st[i..i+1] += t[i] - i*3;

  auto m = readln.chomp.to!int;
  foreach (_; 0..m) {
    auto rd2 = readln.splitter;
    auto l = rd2.front.to!int-1; rd2.popFront();
    auto r = rd2.front.to!int;   rd2.popFront();
    auto d = rd2.front.to!long;
    st[l..r] += d;
    writeln(st[0..$] + (n-1)*3);
  }
}

struct SegmentTreeLazy(T, alias pred = "a + b")
{
  import core.bitop, std.conv, std.functional, std.range;
  alias predFun = binaryFun!pred;
  enum Op { none, add };

  const size_t n, an;
  T[] buf, laz;
  Op[] op;
  T unit;

  this(size_t n, T unit = T.init)
  {
    this.n = n;
    this.unit = unit;
    an = (1 << ((n - 1).bsr + 1));
    buf = new T[](an * 2);
    laz = new T[](an * 2);
    op = new Op[](an * 2);
    if (T.init != unit) {
      buf[] = unit;
    }
  }

  void propagate(size_t k, size_t nl, size_t nr)
  {
    if (op[k] == Op.none) return;

    size_t nm = (nl + nr) / 2;
    setLazy(op[k], laz[k], k*2,   nl, nm);
    setLazy(op[k], laz[k], k*2+1, nm, nr);

    op[k] = Op.none;
  }

  void setLazy(Op nop, T val, size_t k, size_t nl, size_t nr)
  {
    switch (nop) {
    case Op.add:
      buf[k] += val;
      laz[k] = op[k] == Op.none ? val : laz[k] + val;
      op[k] = Op.add;
      break;
    default:
      assert(0);
    }
  }

  void addOpe(Op op, T val, size_t l, size_t r, size_t k, size_t nl, size_t nr)
  {
    if (nr <= l || r <= nl) return;

    if (l <= nl && nr <= r) {
      setLazy(op, val, k, nl, nr);
      return;
    }

    propagate(k, nl, nr);

    auto nm = (nl + nr) / 2;
    addOpe(op, val, l, r, k*2,   nl, nm);
    addOpe(op, val, l, r, k*2+1, nm, nr);

    buf[k] = predFun(buf[k*2], buf[k*2+1]);
  }

  void opSliceOpAssign(string op: "+")(T val, size_t l, size_t r)
  {
    addOpe(Op.add, val, l, r, 1, 0, an);
  }

  T summary(size_t l, size_t r, size_t k, size_t nl, size_t nr)
  {
    if (nr <= l || r <= nl) return unit;

    if (l <= nl && nr <= r) return buf[k];

    propagate(k, nl, nr);

    auto nm = (nl + nr) / 2;
    auto vl = summary(l, r, k*2,   nl, nm);
    auto vr = summary(l, r, k*2+1, nm, nr);

    return predFun(vl, vr);
  }

  T opSlice(size_t l, size_t r)
  {
    return summary(l, r, 1, 0, an);
  }

  pure size_t opDollar() const { return n; }
}
0