結果

問題 No.877 Range ReLU Query
ユーザー ArkArk
提出日時 2020-02-06 21:17:00
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,129 ms / 2,000 ms
コード長 8,415 bytes
コンパイル時間 1,820 ms
コンパイル使用メモリ 194,080 KB
実行使用メモリ 28,340 KB
最終ジャッジ日時 2023-09-04 05:28:03
合計ジャッジ時間 15,249 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 5 ms
4,376 KB
testcase_02 AC 5 ms
4,380 KB
testcase_03 AC 6 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 8 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 1,102 ms
26,768 KB
testcase_12 AC 972 ms
26,260 KB
testcase_13 AC 739 ms
16,632 KB
testcase_14 AC 777 ms
15,120 KB
testcase_15 AC 1,129 ms
26,072 KB
testcase_16 AC 1,085 ms
27,088 KB
testcase_17 AC 1,105 ms
25,752 KB
testcase_18 AC 1,100 ms
25,696 KB
testcase_19 AC 1,045 ms
28,340 KB
testcase_20 AC 1,094 ms
26,084 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.d(166): Deprecation: usage of the `body` keyword is deprecated. Use `do` instead.
Main.d(176): Deprecation: usage of the `body` keyword is deprecated. Use `do` instead.

ソースコード

diff #

import std.stdio;
import std.string;
import std.format;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.concurrency;
import std.traits;
import std.uni;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;

enum long INF = long.max/5;
enum long MOD = 10L^^9+7;

void main() {
  long N, Q;
  scanln(N, Q);
  long[] as = readln.split.to!(long[]);

  struct P {
    long i, l, r, x;
  }
  P[] ps = new P[Q];
  foreach(i; 0..Q) {
    long q;
    scanln(q, ps[i].l, ps[i].r, ps[i].x);
    ps[i].l--;
    ps[i].r--;
    ps[i].i = i;
  }
  ps.sort!"a.x > b.x";

  struct Pair {
    long num, a;
  }
  auto seg = LazySegTree!(
    Pair, long,
    (a, b) => Pair(a.num + b.num, a.a + b.a),
    "a+b",
    (a, b) => Pair(a.num, a.a + b*a.num),
    Pair(0, 0),
    0
  )(Pair(0, 0).repeat(N).array);

  auto bs = DList!(Tuple!(ulong, "index", long, "value"))(as.enumerate.array.sort!"a.value > b.value".array);

  long[] ys = new long[Q];
  foreach(i; 0..Q) {
    while(!bs.empty) {
      auto b = bs.front;
      if (b.value > ps[i].x) {
        bs.removeFront;
        seg.set(b.index, Pair(1, b.value));
      } else {
        break;
      }
    }
    seg.update(ps[i].l, ps[i].r+1, -ps[i].x);
    ys[ps[i].i] = seg.query(ps[i].l, ps[i].r+1).a;
    seg.update(ps[i].l, ps[i].r+1, ps[i].x);
  }
  ys.each!writeln;
}

// Lazy Segment Tree
//    - with 1-based array
struct LazySegTree(
  M,           // モノイドの型
  X,           // 作用素モノイドの型
  alias funMM, // (M, M) → M : Mの2項演算
  alias funXX, // (X, X) → X : Xの2項演算
  alias funMX, // (M, X) → M : Xの元をMの元に作用させる関数
  M eM,        // Mの単位元
  X eX,        // Xの単位元
  bool structly = true
) if (
  is(typeof(binaryFun!funMM(M.init, M.init)) : M) &&
  is(typeof(binaryFun!funXX(X.init, X.init)) : X) &&
  is(typeof(binaryFun!funMX(M.init, X.init)) : M) &&
  binaryFun!funMM(M.init, M.init) == M.init &&
  binaryFun!funXX(X.init, X.init) == X.init &&
  binaryFun!funMX(M.init, X.init) == M.init
) {

private:
  alias _funMM = binaryFun!funMM;
  alias _funXX = binaryFun!funXX;
  alias _funMX = binaryFun!funMX;
  Pair[] _data;
  X[] _lazy;
  size_t _height;
  size_t _size;
  size_t _l, _r;

public:

  // size: データ数
  this(size_t size) {
    init(size);
  }

  // 配列で指定
  this(M[] ms) {
    init(ms.length);
    build(ms);
  }

  // O(N)
  void init(size_t size){
    _height = 0;
    _size = 1;
    while(_size < size) {
      _size <<= 1;
      _height++;
    }
    _data.length = _size<<1;
    _lazy.length = _size<<1;
    _data[] = Pair(size_t.max, eM);
    _lazy[] = eX;
    _l = 0;
    _r = size;
  }

  // 区間 [a, b) を x ∈ X で作用
  // O(logN)
  void update(size_t a, size_t b, X x) {
    assert(a <= b);
    a += _size;
    b += _size;
    evaluate(a);
    evaluate(b - 1);
    for (size_t l = a, r = b; l < r; l >>= 1, r >>= 1) {
      if (l&1) {
        _lazy[l] = _funXX(_lazy[l], x);
        l++;
      }
      if (r&1) {
        r--;
        _lazy[r] = _funXX(_lazy[r], x);
      }
    }
    recalc(a);
    recalc(b - 1);
  }

  // 区間[a, b)でのクエリ (valueの取得)
  // O(logN)
  M query(size_t a, size_t b) {
    Pair pair = accumulate(a, b);
    return pair.value;
  }

  // 区間[a, b)でのクエリ (indexの取得)
  // O(logN)
  size_t queryIndex(size_t a, size_t b) out(result) {
    // fun == (a, b) => a+b のようなときはindexを聞くとassertion
    if (structly) assert(result != size_t.max);
  } body {
    Pair pair = accumulate(a, b);
    return pair.index;
  }

  // 区間[a, b)でのクエリ ((index, value)の取得)
  // O(logN)
  Pair queryPair(size_t a, size_t b) out(result) {
    // fun == (a, b) => a+b のようなときはindexを聞くとassertion
    if (structly) assert(result.index != size_t.max);
  } body {
    Pair pair = accumulate(a, b);
    return pair;
  }

  // i番目の要素を取得
  // O(logN)
  M get(size_t i) {
    i += _size;
    evaluate(i);
    return reflect(i).value;
  }

  // i番目の要素をmに更新
  // O(logN)
  void set(size_t i, M m) {
    i += _size;
    evaluate(i);
    _data[i].value = m;
    _lazy[i] = eX;
    recalc(i);
  }

private:

  struct Pair {
    size_t index;
    M value;
  }

  Pair select(Pair nl, Pair nr) {
    M v = _funMM(nl.value, nr.value);
    if (nl.value == v) {
      return nl;
    } else if (nr.value == v) {
      return nr;
    } else {
      return Pair(size_t.max, v);
    }
  }

  Pair accumulate(size_t a, size_t b) {
    assert(a <= b);
    if (b<=_l || _r<=a) return Pair(size_t.max, eM);
    a += _size;
    b += _size;
    evaluate(a);
    evaluate(b - 1);
    Pair accL = Pair(size_t.max, eM);
    Pair accR = Pair(size_t.max, eM);
    for (size_t l = a, r = b; l < r; l >>= 1, r >>= 1) {
      if (l&1) accL = select(accL, reflect(l++));
      if (r&1) accR = select(reflect(--r), accR);
    }
    return select(accL, accR);
  }

  Pair reflect(size_t i) {
    if (_lazy[i] == eX) {
      return _data[i];
    } else {
      return Pair(
        _data[i].index,
        _funMX(_data[i].value, _lazy[i])
      );
    }
  }

  void thrust(size_t i) {
    if (_lazy[i] == eX) return;
    _lazy[i<<1|0] = _funXX(_lazy[i<<1|0], _lazy[i]);
    _lazy[i<<1|1] = _funXX(_lazy[i<<1|1], _lazy[i]);
    _data[i] = reflect(i);
    _lazy[i] = eX;
  }

  void evaluate(size_t i) {
    foreach_reverse(k; 0.._height) {
      thrust(i>>(k+1));
    }
  }

  void recalc(size_t i) {
    while(i > 0) {
      i >>= 1;
      _data[i] = select(reflect(i<<1|0), reflect(i<<1|1));
    }
  }

  // O(N)
  void build(M[] ms) {
    foreach(i, e; ms) {
      _data[i+_size] = Pair(i, e);
    }
    foreach_reverse(i; 1.._size) {
      Pair nl = _data[i<<1|0];
      Pair nr = _data[i<<1|1];
      _data[i] = select(nl, nr);
    }
  }
}


// ----------------------------------------------


void times(alias fun)(long n) {
  // n.iota.each!(i => fun());
  foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(long n) {
  // return n.iota.map!(i => fun()).array;
  T[] res = new T[n];
  foreach(ref e; res) e = fun();
  return res;
}

T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
  // `(x+y-1)/y` will only work for positive numbers ...
  T t = x / y;
  if (y > 0 && t * y < x) t++;
  if (y < 0 && t * y > x) t++;
  return t;
}

T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
  T t = x / y;
  if (y > 0 && t * y > x) t--;
  if (y < 0 && t * y < x) t--;
  return t;
}

ref T ch(alias fun, T, S...)(ref T lhs, S rhs) {
  return lhs = fun(lhs, rhs);
}
unittest {
  long x = 1000;
  x.ch!min(2000);
  assert(x == 1000);
  x.ch!min(3, 2, 1);
  assert(x == 1);
  x.ch!max(100).ch!min(1000); // clamp
  assert(x == 100);
  x.ch!max(0).ch!min(10); // clamp
  assert(x == 10);
}

mixin template Constructor() {
  import std.traits : FieldNameTuple;
  this(Args...)(Args args) {
    // static foreach(i, v; args) {
    foreach(i, v; args) {
      mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v;
    }
  }
}

void scanln(Args...)(auto ref Args args) {
  enum sep = " ";
  enum n = Args.length;
  enum fmt = n.rep!(()=>"%s").join(sep);

  string line = readln.chomp;
  static if (__VERSION__ >= 2074) {
    line.formattedRead!fmt(args);
  } else {
    enum argsTemp = n.iota.map!(
      i => "&args[%d]".format(i)
    ).join(", ");
    mixin(
      "line.formattedRead(fmt, " ~ argsTemp ~ ");"
    );
  }
}

// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
  template fold(fun...) if (fun.length >= 1) {
    auto fold(R, S...)(R r, S seed) {
      static if (S.length < 2) {
        return reduce!fun(seed, r);
      } else {
        return reduce!fun(tuple(seed), r);
      }
    }
  }
}

// popcnt with ulongs was added in D 2.071.0
static if (__VERSION__ < 2071) {
  ulong popcnt(ulong x) {
    x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L);
    x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L);
    x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL);
    x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL);
    x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL);
    x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL);
    return x;
  }
}
0