結果

問題 No.1234 典型RMQ
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-09-18 21:28:51
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 270 ms / 2,000 ms
コード長 3,856 bytes
コンパイル時間 1,145 ms
コンパイル使用メモリ 107,772 KB
実行使用メモリ 20,544 KB
最終ジャッジ日時 2023-09-04 10:02:32
合計ジャッジ時間 8,641 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 247 ms
11,872 KB
testcase_07 AC 203 ms
6,444 KB
testcase_08 AC 262 ms
11,720 KB
testcase_09 AC 234 ms
8,896 KB
testcase_10 AC 270 ms
12,120 KB
testcase_11 AC 255 ms
12,960 KB
testcase_12 AC 233 ms
9,876 KB
testcase_13 AC 204 ms
6,844 KB
testcase_14 AC 232 ms
8,336 KB
testcase_15 AC 228 ms
8,196 KB
testcase_16 AC 256 ms
12,412 KB
testcase_17 AC 235 ms
8,468 KB
testcase_18 AC 190 ms
12,472 KB
testcase_19 AC 259 ms
12,108 KB
testcase_20 AC 179 ms
20,544 KB
testcase_21 AC 248 ms
11,548 KB
testcase_22 AC 216 ms
11,796 KB
testcase_23 AC 218 ms
12,956 KB
testcase_24 AC 218 ms
12,436 KB
testcase_25 AC 217 ms
13,604 KB
testcase_26 AC 221 ms
12,736 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


// T, S: monoid
// opTT: T * T -> T
// opST: S * T * int -> T
//   (s t_a) ... (s t_{b-1}) = opST(s, t_a ... t_{b-1}, b - a)
// opSS: S * S -> S
// query(a, b, s): t_a <- s t_a, ..., t_{b-1} <- s t_{b-1};
//   returns t_a ... t_{b-1}
class SegmentTree(T, S, alias opTT, alias opST, alias opSS) {
  import std.functional : binaryFun;
  alias opTTFun = binaryFun!opTT;
  alias opSTFun = opST;
  alias opSSFun = binaryFun!opSS;
  const(T) idT;
  const(S) idS;

  int n;
  T[] ts;
  S[] ss;
  this(int n_, const(T) idT, const(S) idS) {
    this.idT = idT;
    this.idS = idS;
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ss = new S[n << 1];
    ts[] = idT;
    ss[] = idS;
  }
  ref T at(int a) {
    return ts[n + a];
  }
  void build() {
    foreach_reverse (a; 1 .. n) ts[a] = opTTFun(ts[a << 1], ts[a << 1 | 1]);
  }
  T query(int a, int b, const(S) s) {
    return query(1, 0, n, a, b, s);
  }

 private:
  T query(int u, int l, int r, int a, int b, const(S) s) {
    if (a < l) a = l;
    if (b > r) b = r;
    if (a >= b) return idT;
    if (a == l && b == r) {
      ts[u] = opSTFun(s, ts[u], r - l);
      ss[u] = opSSFun(s, ss[u]);
      return ts[u];
    }
    const int uL = u << 1, uR = u << 1 | 1;
    const int mid = (l + r) >> 1;
    // speed-up: if (ss[u] != idS)
    {
      ts[uL] = opSTFun(ss[u], ts[uL], mid - l);
      ts[uR] = opSTFun(ss[u], ts[uR], r - mid);
      ss[uL] = opSSFun(ss[u], ss[uL]);
      ss[uR] = opSSFun(ss[u], ss[uR]);
      ss[u] = idS;
    }
    const T resL = query(uL, l, mid, a, b, s);
    const T resR = query(uR, mid, r, a, b, s);
    ts[u] = opTTFun(ts[uL], ts[uR]);
    return opTTFun(resL, resR);
  }
}


enum INF = 10L^^18;

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      auto A = new long[N];
      foreach (i; 0 .. N) {
        A[i] = readLong();
      }
      const Q = readInt();
      auto K = new int[Q];
      auto L = new int[Q];
      auto R = new int[Q];
      auto C = new long[Q];
      foreach (q; 0 .. Q) {
        K[q] = readInt();
        L[q] = readInt() - 1;
        R[q] = readInt();
        C[q] = readLong();
      }
      
      auto seg = new SegmentTree!(long, long, min, (s, t, sz) => t + s,
                                  "a + b")(N, INF, 0);
      foreach (i; 0 .. N) {
        seg.at(i) = A[i];
      }
      seg.build;
      foreach (q; 0 .. Q) {
        switch (K[q]) {
          case 1: {
            seg.query(L[q], R[q], C[q]);
          } break;
          case 2: {
            const res = seg.query(L[q], R[q], 0);
            writeln(res);
          } break;
          default: assert(false);
        }
      }
    }
  } catch (EOFException e) {
  }
}
0