結果

問題 No.1234 典型RMQ
ユーザー ninoinuininoinui
提出日時 2020-09-23 14:54:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 4,306 bytes
コンパイル時間 2,292 ms
コンパイル使用メモリ 200,820 KB
実行使用メモリ 7,168 KB
最終ジャッジ日時 2024-04-26 11:33:45
合計ジャッジ時間 5,895 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 84 ms
6,912 KB
testcase_07 AC 65 ms
5,376 KB
testcase_08 AC 83 ms
7,040 KB
testcase_09 AC 79 ms
5,376 KB
testcase_10 AC 85 ms
6,912 KB
testcase_11 AC 84 ms
6,784 KB
testcase_12 AC 76 ms
5,376 KB
testcase_13 AC 68 ms
5,376 KB
testcase_14 AC 76 ms
5,376 KB
testcase_15 AC 74 ms
5,376 KB
testcase_16 AC 86 ms
6,912 KB
testcase_17 AC 75 ms
5,376 KB
testcase_18 AC 61 ms
5,376 KB
testcase_19 AC 88 ms
7,040 KB
testcase_20 AC 69 ms
7,168 KB
testcase_21 AC 82 ms
7,040 KB
testcase_22 AC 81 ms
7,040 KB
testcase_23 AC 80 ms
7,168 KB
testcase_24 AC 80 ms
7,168 KB
testcase_25 AC 82 ms
7,040 KB
testcase_26 AC 81 ms
7,040 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template <class S, S (*op)(S, S), S (*el)(), class F, S (*mapp)(F, S), F (*comp)(F, F), F (*id)()> class LazySegmentTree {
private:
  int N, size, log;
  vector<S> D;
  vector<F> L;
  void apply(int k) { D.at(k) = op(D.at(2 * k), D.at(2 * k + 1)); }
  void update_all(int k, F f) {
    D.at(k) = mapp(f, D.at(k));
    if (k < size) L.at(k) = comp(f, L.at(k));
  }
  void push(int k) {
    update_all(2 * k, L.at(k));
    update_all(2 * k + 1, L.at(k));
    L.at(k) = id();
  }
  int ceil_pow(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
  }

public:
  LazySegmentTree() : LazySegmentTree(0) {}
  LazySegmentTree(int n) : LazySegmentTree(vector<S>(n, el())) {}
  LazySegmentTree(const vector<S>& v) : N(int(v.size())) {
    log = ceil_pow(N);
    size = 1 << log;
    D = vector<S>(2 * size, el());
    L = vector<F>(size, id());
    for (int i = 0; i < N; i++) D.at(size + i) = v.at(i);
    for (int i = size - 1; i >= 1; i--) apply(i);
  }
  void set(int p, S x) {
    p += size;
    for (int i = log; i >= 1; i--) push(p >> i);
    D.at(p) = x;
    for (int i = 1; i <= log; i++) apply(p >> i);
  }
  S get(int p) {
    p += size;
    for (int i = log; i >= 1; i--) push(p >> i);
    return D.at(p);
  }
  S query(int l, int r) {
    r++;
    if (l == r) return el();
    l += size, r += size;
    for (int i = log; i >= 1; i--) {
      if (((l >> i) << i) != l) push(l >> i);
      if (((r >> i) << i) != r) push(r >> i);
    }
    S sml = el(), smr = el();
    while (l < r) {
      if (l & 1) sml = op(sml, D.at(l++));
      if (r & 1) smr = op(D.at(--r), smr);
      l >>= 1, r >>= 1;
    }
    return op(sml, smr);
  }
  S query_all() { return D.at(1); }
  void update(int p, F f) {
    p += size;
    for (int i = log; i >= 1; i--) push(p >> i);
    D.at(p) = mapp(f, D.at(p));
    for (int i = 1; i <= log; i++) apply(p >> i);
  }
  void update(int l, int r, F f) {
    r++;
    if (l == r) return;
    l += size, r += size;
    for (int i = log; i >= 1; i--) {
      if (((l >> i) << i) != l) push(l >> i);
      if (((r >> i) << i) != r) push((r - 1) >> i);
    }
    {
      int l2 = l, r2 = r;
      while (l < r) {
        if (l & 1) update_all(l++, f);
        if (r & 1) update_all(--r, f);
        l >>= 1, r >>= 1;
      }
      l = l2, r = r2;
    }
    for (int i = 1; i <= log; i++) {
      if (((l >> i) << i) != l) apply(l >> i);
      if (((r >> i) << i) != r) apply((r - 1) >> i);
    }
  }
  template <bool (*g)(S)> int max_right(int l) {
    return max_right(l, [](S x) { return g(x); });
  }
  template <class G> int max_right(int l, G g) {
    if (l == N) return N;
    l += size;
    for (int i = log; i >= 1; i--) push(l >> i);
    S sm = el();
    do {
      while (l % 2 == 0) l >>= 1;
      if (!g(op(sm, D.at(l)))) {
        while (l < size) {
          push(l);
          l = (2 * l);
          if (g(op(sm, D.at(l)))) sm = op(sm, D.at(l++));
        }
        return l - size;
      }
      sm = op(sm, D.at(l++));
    } while ((l & -l) != l);
    return N;
  }
  template <bool (*g)(S)> int min_left(int r) {
    return min_left(r, [](S x) { return g(x); });
  }
  template <class G> int min_left(int r, G g) {
    if (r == 0) return 0;
    r += size;
    for (int i = log; i >= 1; i--) push((r - 1) >> i);
    S sm = el();
    do {
      r--;
      while (r > 1 && (r % 2)) r >>= 1;
      if (!g(op(D.at(r), sm))) {
        while (r < size) {
          push(r);
          r = (2 * r + 1);
          if (g(op(D.at(r), sm))) sm = op(D.at(r--), sm);
        }
        return r + 1 - size;
      }
      sm = op(D.at(r), sm);
    } while ((r & -r) != r);
    return 0;
  }
};

using S = long;
using F = long;
S op(S a, S b) { return min(a, b); }
S el() { return LONG_MAX; }
S mapp(F f, S s) { return f + s; }
F comp(F a, F b) { return a + b; }
F id() { return 0; }

signed main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<long> A(N);
  for (int i = 0; i < N; i++) cin >> A.at(i);
  LazySegmentTree<S, op, el, F, mapp, comp, id> ST(A);
  int Q;
  cin >> Q;
  while (Q--) {
    int k, l, r, c;
    cin >> k >> l >> r >> c, l--, r--;
    if (k == 1) {
      ST.update(l, r, c);
    } else {
      cout << ST.query(l, r) << "\n";
    }
  }
}
0