結果

問題 No.1234 典型RMQ
ユーザー ninoinuininoinui
提出日時 2020-09-18 22:19:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 118 ms / 2,000 ms
コード長 2,021 bytes
コンパイル時間 2,135 ms
コンパイル使用メモリ 208,512 KB
実行使用メモリ 10,084 KB
最終ジャッジ日時 2023-08-08 18:45:09
合計ジャッジ時間 6,549 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 111 ms
9,896 KB
testcase_07 AC 85 ms
4,380 KB
testcase_08 AC 118 ms
9,816 KB
testcase_09 AC 103 ms
6,400 KB
testcase_10 AC 114 ms
9,764 KB
testcase_11 AC 110 ms
10,064 KB
testcase_12 AC 101 ms
6,376 KB
testcase_13 AC 86 ms
4,376 KB
testcase_14 AC 101 ms
6,708 KB
testcase_15 AC 98 ms
6,396 KB
testcase_16 AC 114 ms
9,820 KB
testcase_17 AC 101 ms
6,456 KB
testcase_18 AC 77 ms
4,376 KB
testcase_19 AC 116 ms
9,760 KB
testcase_20 AC 83 ms
10,084 KB
testcase_21 AC 111 ms
9,840 KB
testcase_22 AC 96 ms
10,008 KB
testcase_23 AC 96 ms
10,024 KB
testcase_24 AC 96 ms
10,024 KB
testcase_25 AC 97 ms
10,008 KB
testcase_26 AC 96 ms
10,028 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename T> class SegmentTree {
private:
  int n, sz, h;
  vector<pair<T, int>> node;
  vector<T> lazy;
  void eval(int k) {
    if (lazy.at(k)) {
      node.at(k).first += lazy.at(k);
      if (k < n) {
        lazy.at(k * 2) += lazy.at(k), lazy.at(k * 2 + 1) += lazy.at(k);
      }
      lazy.at(k) = 0;
    }
  }
public:
  SegmentTree(const vector<T> &v) : sz((int) v.size()), h(0) {
    n = 1;
    while(n < sz) n *= 2, h++;
    node.resize(2 * n, pair<T, int>(numeric_limits<T>::max(), sz));
    lazy.resize(2 * n, 0);
    for (int i = 0; i < sz; i++) {
      node.at(i + n) = make_pair(v.at(i), i);
    }
    for (int i = n - 1; i >= 1; i--) {
      node.at(i) = min(node.at(2 * i), node.at(2 * i + 1));
    }
  }
  void range(int a, int b, T x, int k = 1, int l = 0, int r = -1) {
    if (r < 0) r = n;
    eval(k);
    if (b <= l || r <= a) {
      return;
    }
    if (a <= l && r <= b) {
      lazy.at(k) += x;
      eval(k);
    } else {
      range(a, b, x, 2 * k, l, (l + r) / 2);
      range(a, b, x, 2 * k + 1, (l + r) / 2, r);
      node.at(k) = min(node.at(2 * k), node.at(2 * k + 1));
    }
  }
  pair<T, int> query(int a, int b) {
    a += n, b += n - 1;
    for (int i = h; i > 0; i--) eval(a >> i), eval(b >> i);
    b++;
    pair<T, int> res1 = make_pair(numeric_limits<T>::max(), sz);
    pair<T, int> res2 = make_pair(numeric_limits<T>::max(), sz);
    while(a < b) {
      if (a & 1) eval(a), res1 = min(res1, node.at(a++));
      if (b & 1) eval(--b), res2 = min(res2, node.at(b));
      a >>= 1, b >>= 1;
    }
    return min(res1, res2);
  }
};

int 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);
  SegmentTree<long> ST(A);
  int Q;
  cin >> Q;
  while (Q--) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (a == 1) {
      ST.range(b - 1, c, d);
    } else {
      cout << ST.query(b - 1, c).first << "\n";
    }
  }
}
0