結果

問題 No.1234 典型RMQ
ユーザー tnakao0123tnakao0123
提出日時 2020-09-19 00:01:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 101 ms / 2,000 ms
コード長 2,667 bytes
コンパイル時間 1,099 ms
コンパイル使用メモリ 98,220 KB
実行使用メモリ 7,768 KB
最終ジャッジ日時 2023-08-08 18:52:44
合計ジャッジ時間 5,069 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,532 KB
testcase_01 AC 4 ms
7,540 KB
testcase_02 AC 3 ms
7,532 KB
testcase_03 AC 3 ms
7,688 KB
testcase_04 AC 3 ms
7,552 KB
testcase_05 AC 3 ms
7,536 KB
testcase_06 AC 93 ms
7,552 KB
testcase_07 AC 79 ms
7,504 KB
testcase_08 AC 101 ms
7,688 KB
testcase_09 AC 91 ms
7,548 KB
testcase_10 AC 99 ms
7,500 KB
testcase_11 AC 94 ms
7,552 KB
testcase_12 AC 89 ms
7,552 KB
testcase_13 AC 80 ms
7,536 KB
testcase_14 AC 88 ms
7,536 KB
testcase_15 AC 86 ms
7,552 KB
testcase_16 AC 98 ms
7,612 KB
testcase_17 AC 90 ms
7,736 KB
testcase_18 AC 76 ms
7,604 KB
testcase_19 AC 101 ms
7,508 KB
testcase_20 AC 64 ms
7,768 KB
testcase_21 AC 97 ms
7,472 KB
testcase_22 AC 76 ms
7,540 KB
testcase_23 AC 77 ms
7,608 KB
testcase_24 AC 78 ms
7,500 KB
testcase_25 AC 78 ms
7,732 KB
testcase_26 AC 77 ms
7,552 KB
testcase_27 AC 3 ms
7,676 KB
testcase_28 AC 3 ms
7,672 KB
testcase_29 AC 3 ms
7,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1234.cc:  No.1234 典型RMQ - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_E2 = 1 << 18; // = 262144
const long long LINF = 1LL << 62;

/* typedef */

typedef long long ll;

template <typename T, const int MAX_E2>
struct SegTreeMinDelay {
  int e2;
  T nodes[MAX_E2], inf;
  T dls[MAX_E2];
  SegTreeMinDelay() {}

  void init(int n, T _inf) {
    inf = _inf;
    for (e2 = 1; e2 < n; e2 <<= 1);
    fill(nodes, nodes + MAX_E2, inf);
    //fill(dls, dls + MAX_E2, false);
  }

  T &get(int i) { return nodes[e2 - 1 + i]; }
  void seti(int i, T v) { get(i) = v; }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = min(nodes[j * 2 + 1], nodes[j * 2 + 2]);
  }

  void __update(int k) {
    if (dls[k] != 0) {
      int k0 = k * 2 + 1, k1 = k0 + 1;
      nodes[k0] += dls[k];
      nodes[k1] += dls[k];
      dls[k0] += dls[k];
      dls[k1] += dls[k];
      dls[k] = 0;
    }
  }

  void add_range(int r0, int r1, T v, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return;
    if (r0 <= i0 && i1 <= r1) {
      nodes[k] += v;
      dls[k] += v;
      return;
    }

    __update(k);

    int im = (i0 + i1) / 2;
    int k0 = k * 2 + 1, k1 = k0 + 1;
    add_range(r0, r1, v, k0, i0, im);
    add_range(r0, r1, v, k1, im, i1);
    nodes[k] = min(nodes[k0], nodes[k1]);
  }
  void add_range(int r0, int r1, T v) { add_range(r0, r1, v, 0, 0, e2); }

  T min_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return inf;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    __update(k);

    int im = (i0 + i1) / 2;
    T v0 = min_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = min_range(r0, r1, k * 2 + 2, im, i1);
    return min(v0, v1);
  }
  T min_range(int r0, int r1) { return min_range(r0, r1, 0, 0, e2); }
};

/* global variables */

SegTreeMinDelay<ll,MAX_E2> st;

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);

  st.init(n, LINF);

  for (int i = 0; i < n; i++) {
    ll ai;
    scanf("%lld", &ai);
    st.seti(i, ai);
  }
  st.setall();

  int q;
  scanf("%d", &q);

  while (q--) {
    int k, l, r, c;
    scanf("%d%d%d%d", &k, &l, &r, &c);
    l--;

    if (k == 1)
      st.add_range(l, r, c);
    else {
      ll v = st.min_range(l, r);
      printf("%lld\n", v);
    }
  }
  return 0;
}
0