結果

問題 No.1234 典型RMQ
ユーザー 👑 emthrmemthrm
提出日時 2020-09-18 21:47:07
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 3,046 bytes
コンパイル時間 2,164 ms
コンパイル使用メモリ 204,200 KB
実行使用メモリ 8,240 KB
最終ジャッジ日時 2023-08-08 18:37:04
合計ジャッジ時間 5,788 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 82 ms
7,712 KB
testcase_07 AC 64 ms
4,376 KB
testcase_08 AC 86 ms
7,832 KB
testcase_09 AC 73 ms
5,628 KB
testcase_10 AC 82 ms
7,760 KB
testcase_11 AC 79 ms
7,920 KB
testcase_12 AC 71 ms
5,464 KB
testcase_13 AC 64 ms
4,376 KB
testcase_14 AC 73 ms
5,584 KB
testcase_15 AC 71 ms
5,484 KB
testcase_16 AC 87 ms
7,760 KB
testcase_17 AC 74 ms
5,456 KB
testcase_18 AC 58 ms
4,380 KB
testcase_19 AC 88 ms
7,820 KB
testcase_20 AC 60 ms
8,028 KB
testcase_21 AC 82 ms
7,796 KB
testcase_22 AC 74 ms
7,964 KB
testcase_23 AC 73 ms
8,020 KB
testcase_24 AC 73 ms
7,976 KB
testcase_25 AC 71 ms
8,240 KB
testcase_26 AC 70 ms
8,028 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr ll LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

template <typename Monoid>
struct StarrySky {
  StarrySky(int sz, const Monoid UNITY, const Monoid DEFAULT = 0) : UNITY(UNITY), DEFAULT(DEFAULT) {
    init(sz);
    dat.assign((n << 1) - 1, DEFAULT);
  }

  StarrySky(const std::vector<Monoid> &a, const Monoid &UNITY, const Monoid DEFAULT = 0) : UNITY(UNITY), DEFAULT(DEFAULT) {
    int a_sz = a.size();
    init(a_sz);
    dat.resize((n << 1) - 1);
    for (int i = 0; i < a_sz; ++i) dat[n - 1 + i] = a[i];
    for (int i = n - 2; i >= 0; --i) dat[i] = std::min(dat[(i << 1) + 1], dat[(i << 1) + 2]);
  }

  void add(int a, int b, Monoid val) { add(a, b, val, 0, 0, n); }

  Monoid query(int a, int b) const { return query(a, b, 0, 0, n); }

  Monoid value(int idx) const {
    idx += n - 1;
    Monoid res = added[idx];
    while (idx > 0) {
      idx = (idx - 1) >> 1;
      res += added[idx];
    }
    return res;
  }

private:
  int n = 1;
  const Monoid UNITY, DEFAULT;
  std::vector<Monoid> dat, added;

  void init(int sz) {
    while (n < sz) n <<= 1;
    added.assign((n << 1) - 1, DEFAULT);
  }

  void add(int a, int b, Monoid val, int node, int left, int right) {
    if (right <= a || b <= left) return;
    if (a <= left && right <= b) {
      added[node] += val;
    } else {
      add(a, b, val, (node << 1) + 1, left, (left + right) >> 1);
      add(a, b, val, (node << 1) + 2, (left + right) >> 1, right);
      dat[node] = std::min(dat[(node << 1) + 1] + added[(node << 1) + 1], dat[(node << 1) + 2] + added[(node << 1) + 2]);
    }
  }

  Monoid query(int a, int b, int node, int left, int right) const {
    if (right <= a || b <= left) return UNITY;
    if (a <= left && right <= b) return dat[node] + added[node];
    return std::min(query(a, b, (node << 1) + 1, left, (left + right) >> 1), query(a, b, (node << 1) + 2, (left + right) >> 1, right)) + added[node];
  }
};

int main() {
  int n; cin >> n;
  vector<ll> a(n); REP(i, n) cin >> a[i];
  StarrySky ss(a, LINF);
  int q; cin >> q;
  while (q--) {
    int k, l, r, c; cin >> k >> l >> r >> c; --l; --r;
    if (k == 1) {
      ss.add(l, r + 1, c);
    } else if (k == 2) {
      cout << ss.query(l, r + 1) << '\n';
    }
  }
  return 0;
}
0