結果

問題 No.631 Noelちゃんと電車旅行
ユーザー te-shte-sh
提出日時 2018-07-24 16:40:44
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 311 ms / 2,000 ms
コード長 2,759 bytes
コンパイル時間 1,164 ms
コンパイル使用メモリ 91,724 KB
実行使用メモリ 11,796 KB
最終ジャッジ日時 2023-09-03 20:48:31
合計ジャッジ時間 9,337 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 183 ms
4,380 KB
testcase_01 AC 309 ms
10,528 KB
testcase_02 AC 311 ms
11,592 KB
testcase_03 AC 305 ms
10,332 KB
testcase_04 AC 306 ms
11,340 KB
testcase_05 AC 309 ms
10,292 KB
testcase_06 AC 121 ms
6,712 KB
testcase_07 AC 74 ms
6,948 KB
testcase_08 AC 236 ms
10,240 KB
testcase_09 AC 261 ms
7,028 KB
testcase_10 AC 197 ms
11,796 KB
testcase_11 AC 173 ms
7,068 KB
testcase_12 AC 218 ms
10,248 KB
testcase_13 AC 185 ms
5,764 KB
testcase_14 AC 72 ms
4,376 KB
testcase_15 AC 164 ms
5,372 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}

void main()
{
  int n; readV(n);
  int[] t; readA(n-1, t);

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

  int m; readV(m);
  foreach (_; 0..m) {
    int l, r, d; readV(l, r, d); --l;
    s[l..r] += d;
    writeln(s[0..$] + (n-1)*3);
  }
}

class SegmentTreeLazy(T, alias pred = max)
{
  import core.bitop, std.conv, std.functional, std.range;
  alias predFun = binaryFun!pred;
  enum Op { none, fill, 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.fill:
      buf[k] = val;
      laz[k] = val;
      op[k] = nop;
      break;
    case Op.add:
      buf[k] += val;
      laz[k] = op[k] == Op.none ? val : laz[k] + val;
      op[k] = op[k] == Op.fill ? Op.fill : 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 opSliceAssign(T val, size_t l, size_t r)
  {
    addOpe(Op.fill, val, l, r, 1, 0, an);
  }

  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