Problem p03554 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

template <typename T> struct Segtree {
  int n;
  T e;
  vector<T> dat;
  typedef function<T(T a, T b)> Func;
  Func f;

  Segtree(int n_input, Func f, T e_input) : f(f), e(e_input) {
    n = 1;
    while (n < n_input)
      n <<= 1;
    dat.resize(2 * n - 1, e);
  }

  void update(int k, T a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]);
    }
  }

  T get(int k) { return dat[k + n - 1]; }

  T between(int a, int b) { return query(a, b + 1, 0, 0, n); }

  T query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return e;
    if (a <= l && r <= b)
      return dat[k];
    T vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    T vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return f(vl, vr);
  }
};

int main() {
  const int MAX = 100001;
  int N, Q, B[MAX], num0 = 0;
  vector<int> LR[MAX];
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> B[i];
    if (B[i] == 0)
      num0++;
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    LR[l].push_back(r);
  }

  Segtree<int> st(
      N + 1, [](int a, int b) { return min(a, b); }, 1e9);
  st.update(0, 0);
  for (int i = 1; i <= N; i++) {
    sort(LR[i].rbegin(), LR[i].rend());
    for (auto r : LR[i]) {
      int result = st.between(i - 1, r - 1);
      if (result < st.get(r))
        st.update(r, result);
    }
    int result = st.get(i - 1) + (B[i] ? 1 : -1);
    if (result < st.get(i))
      st.update(i, result);
  }

  int ans = st.get(N) + num0;
  cout << ans << endl;
  return 0;
}

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

template <typename T> struct Segtree {
  int n;
  T e;
  vector<T> dat;
  typedef function<T(T a, T b)> Func;
  Func f;

  Segtree(int n_input, Func f, T e_input) : f(f), e(e_input) {
    n = 1;
    while (n < n_input)
      n <<= 1;
    dat.resize(2 * n - 1, e);
  }

  void update(int k, T a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]);
    }
  }

  T get(int k) { return dat[k + n - 1]; }

  T between(int a, int b) { return query(a, b + 1, 0, 0, n); }

  T query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return e;
    if (a <= l && r <= b)
      return dat[k];
    T vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    T vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return f(vl, vr);
  }
};

int main() {
  const int MAX = 200001;
  int N, Q, B[MAX], num0 = 0;
  vector<int> LR[MAX];
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> B[i];
    if (B[i] == 0)
      num0++;
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    LR[l].push_back(r);
  }

  Segtree<int> st(
      N + 1, [](int a, int b) { return min(a, b); }, 1e9);
  st.update(0, 0);
  for (int i = 1; i <= N; i++) {
    sort(LR[i].rbegin(), LR[i].rend());
    for (auto r : LR[i]) {
      int result = st.between(i - 1, r - 1);
      if (result < st.get(r))
        st.update(r, result);
    }
    int result = st.get(i - 1) + (B[i] ? 1 : -1);
    if (result < st.get(i))
      st.update(i, result);
  }

  int ans = st.get(N) + num0;
  cout << ans << endl;
  return 0;
}