Problem p02345 (C++) — buggy submission, verdict: Runtime Error


#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;

//??????????????°??¨(min????§?????????§???????????°?????????????????§??¨???)
// less???greater???Comp?????\\?????????
//???????§??????????operator()???????????????????????§???OK
//??????RMQ<int,less<int>>tree(N,INT_MAX);
template <typename T, typename Comp> class RMQ {
private:
  const T DEF;   //??????????????????(min??§???INF???sum??§???0??????)
  vector<T> seg; //????????????
  int width;     //?????°??¨??????(???????????§????????§????????????)
  T query_raw(int a, int b, int k, int l, int r) const {
    if (a >= r || b <= l)
      return DEF;
    if (a <= l && r <= b)
      return seg[k];
    return min(query_raw(a, b, 2 * k + 1, l, (l + r) / 2),
               query_raw(a, b, 2 * k + 2, (l + r) / 2, r), Comp());
  }

public:
  //?????????n?????????????????????def????????°??¨?????????
  RMQ(int n, T def) : DEF(def) {
    width = 1;
    while (width < n)
      width *= 2;
    seg.assign(width * 2 - 1, DEF);
  }
  //??????[a,b)????????¨???????????????????????????
  T query(int a, int b) const { return query_raw(a, b, 0, 0, width); }
  // x?????????????´????k?????´??°??????
  void update(int k, T x) {
    k += width - 1;
    seg[k] = x;
    while (k > 0) {
      k = (k - 1) / 2;
      seg[k] = min(seg[2 * k + 1], seg[2 * k + 2], Comp());
    }
  }
  //???????????¨???????????????
  void init(const vector<T> &vec) {
    assert(vec.size() >= width);
    for (int i = 0; i < width; ++i)
      seg[i + width - 1] = vec[i];
    for (int k = width = 2; k >= 0; --k)
      seg[k] = min(seg[2 * k + 1], seg[2 * k + 2], Comp());
  }
};

int main() {
  int N, Q;
  cin >> N >> Q;
  RMQ<int, less<int>> tree(N, INT_MAX);
  for (int i = 0; i < Q; ++i) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com) {
      cout << tree.query(x, y + 1) << endl;
    } else {
      tree.update(x, y);
    }
  }
  return 0;
}

Error: WA

Fixed submission:


#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;

//??????????????°??¨(min????§?????????§???????????°?????????????????§??¨???)
// less???greater???Comp?????\\?????????
//???????§??????????operator()???????????????????????§???OK
//??????RMQ<int,less<int>>tree(N,INT_MAX);
template <typename T, typename Comp> class RMQ {
private:
  const T DEF;   //??????????????????(min??§???INF???sum??§???0??????)
  vector<T> seg; //????????????
  int width;     //?????°??¨??????(???????????§????????§????????????)
  T query_raw(int a, int b, int k, int l, int r) const {
    if (a >= r || b <= l)
      return DEF;
    if (a <= l && r <= b)
      return seg[k];
    return min(query_raw(a, b, 2 * k + 1, l, (l + r) / 2),
               query_raw(a, b, 2 * k + 2, (l + r) / 2, r), Comp());
  }

public:
  //?????????n?????????????????????def????????°??¨?????????
  RMQ(int n, T def) : DEF(def) {
    width = 1;
    while (width < n)
      width *= 2;
    seg.assign(width * 2 - 1, DEF);
  }
  //??????[a,b)????????¨???????????????????????????
  T query(int a, int b) const { return query_raw(a, b, 0, 0, width); }
  // x?????????????´????k?????´??°??????
  void update(int k, T x) {
    k += width - 1;
    seg[k] = x;
    while (k > 0) {
      k = (k - 1) / 2;
      seg[k] = min(seg[2 * k + 1], seg[2 * k + 2], Comp());
    }
  }
  //???????????¨???????????????
  void init(const vector<T> &vec) {
    assert(vec.size() >= width);
    for (int i = 0; i < width; ++i)
      seg[i + width - 1] = vec[i];
    for (int k = width = 2; k >= 0; --k)
      seg[k] = min(seg[2 * k + 1], seg[2 * k + 2], Comp());
  }
};

int main() {
  int N, Q;
  cin >> N >> Q;
  RMQ<int, less<int>> tree(N, INT_MAX);
  for (int i = 0; i < Q; ++i) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com) {
      cout << tree.query(x - 1, y) << endl;
    } else {
      tree.update(x - 1, y);
    }
  }
  return 0;
}