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

#include <bits/stdc++.h>
using ll = long long;
#define int ll
#define FOR(i, a, b) for (int i = (a); i < int(b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= int(a); --i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, int(n) + 1)
#define rrep(i, n) RFOR(i, 0, n)
#define rrep1(i, n) RFOR(i, 1, int(n) + 1)
#define all(c) begin(c), end(c)
// const int MOD = 1000000007;

template <typename T>
void __print__(std::ostream &os, const char *, const char *tail, const T &fst) {
  os << fst << tail;
}
template <typename Fst, typename... Rst>
void __print__(std::ostream &os, const char *del, const char *tail,
               const Fst &fst, const Rst &...rst) {
  os << fst << del;
  __print__(os, del, tail, rst...);
}

#ifdef LOCAL
#define dump(...)                                                              \\
  do {                                                                         \\
    std::ostringstream os;                                                     \\
    os << __LINE__ << ":\\t" << #__VA_ARGS__ << " = ";                          \\
    __print__(os, ", ", "\\n", __VA_ARGS__);                                    \\
    std::cerr << os.str();                                                     \\
  } while (0)
#else
#define dump(...)
#endif

template <typename Fst, typename... Rst>
void println(const Fst &fst, const Rst &...rst) {
  __print__(std::cout, "\\n", "\\n", fst, rst...);
}
template <typename Fst, typename... Rst>
void print(const Fst &fst, const Rst &...rst) {
  __print__(std::cout, " ", "\\n", fst, rst...);
}

template <typename iter> void println_(iter bgn, iter end) {
  while (bgn != end)
    println(*bgn++);
}

template <typename iter> void print_(iter bgn, iter end) {
  while (bgn != end) {
    std::cout << *bgn++;
    std::cout << (bgn == end ? "\\n" : " ");
  }
}

int _ = (std::cout.precision(10), std::cout.setf(std::ios::fixed),
         std::cin.tie(0), std::ios::sync_with_stdio(0), 0);

template <typename T> std::vector<T> ndarray(int n, T v) {
  return std::vector<T>(n, v);
}
template <typename... Args> auto ndarray(int n, Args... args) {
  auto val = ndarray(args...);
  return std::vector<decltype(val)>(n, move(val));
}

template <typename T> bool umax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

template <typename T> bool umin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

using namespace std;

template <typename monoid> struct segment_tree {
  using M = monoid;
  using T = typename M::value_type;

  std::size_t sz;
  std::vector<T> x;

  segment_tree(std::size_t n = 0) {
    sz = 1;
    while (sz < n)
      sz *= 2;
    x.assign(sz * 2, M::id());
    initialize();
  }

  template <typename iterator> segment_tree(iterator first, iterator last) {
    sz = 1;
    std::size_t n = std::distance(first, last);
    while (sz < n)
      sz *= 2;
    x.assign(sz * 2, M::id());
    std::copy(first, last, x.begin() + sz);
    initialize();
  }

  void fill(const T &val) {
    std::fill(x.begin() + sz, x.end(), val);
    initialize();
  }

  void initialize() {
    for (int i = (int)sz - 1; i >= 1; --i) {
      x[i] = M::op(x[i * 2 + 0], x[i * 2 + 1]);
    }
  }

  T accumulate(std::size_t l, std::size_t r) const {
    T al = M::id(), ar = M::id();
    for (l += sz, r += sz; l < r; l /= 2, r /= 2) {
      if (l & 1)
        al = M::op(al, x[l++]);
      if (r & 1)
        ar = M::op(x[--r], ar);
    }
    return M::op(al, ar);
  }

  void update(std::size_t i, const T &val) {
    x[i += sz] = val;
    while (i > 1) {
      x[i / 2] = M::op(x[i], x[i ^ 1]);
      i /= 2;
    }
  }

  T operator[](std::size_t i) const { return x[sz + i]; }
};

template <typename T> struct min_monoid {
  using value_type = T;
  static constexpr T id() { return 1e18; }
  static T op(const T &a, const T &b) { return std::min(a, b); }
};

template <typename T> struct max_monoid {
  using value_type = T;
  static constexpr value_type id() {
    return std::numeric_limits<value_type>::min();
  }
  static value_type op(const value_type &a, const value_type &b) {
    return std::max(a, b);
  }
};

template <typename T> struct sum_monoid {
  using value_type = T;
  static constexpr value_type id() { return 0; }
  static value_type op(const value_type &a, const value_type &b) {
    return a + b;
  }
};

template <typename value_type> using rmq = segment_tree<min_monoid<value_type>>;

template <typename value_type> using rsq = segment_tree<sum_monoid<value_type>>;

using pii = pair<int, int>;
int N, M, s, t;
int d[100010];
vector<pii> es;

signed main() {
  while (cin >> N >> M >> s >> t && N) {
    --s, --t;
    rep(i, N) cin >> d[i];
    es.resize(M);
    for (auto &e : es) {
      int a, b;
      cin >> a >> b;
      --a;
      --b;
      e = {a, b};
    }

    sort(all(es), [](pii a, pii b) { return a.second < b.second; });

    rmq<ll> dp(N);
    rep(i, s + 1) { dp.update(i, 0); }
    for (auto &e : es) {
      int l, r;
      tie(l, r) = e;
      int x = dp.accumulate(l, r) + d[r];
      if (dp.accumulate(r, r + 1) > x) {
        dp.update(r, x);
      }
      // rep(i, N) {
      //     cout << dp.accumulate(i, i + 1) << ' ';
      // }
      // cout << endl;
    }
    int ans = dp.accumulate(t, N);
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using ll = long long;
#define int ll
#define FOR(i, a, b) for (int i = (a); i < int(b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= int(a); --i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, int(n) + 1)
#define rrep(i, n) RFOR(i, 0, n)
#define rrep1(i, n) RFOR(i, 1, int(n) + 1)
#define all(c) begin(c), end(c)
// const int MOD = 1000000007;

template <typename T>
void __print__(std::ostream &os, const char *, const char *tail, const T &fst) {
  os << fst << tail;
}
template <typename Fst, typename... Rst>
void __print__(std::ostream &os, const char *del, const char *tail,
               const Fst &fst, const Rst &...rst) {
  os << fst << del;
  __print__(os, del, tail, rst...);
}

#ifdef LOCAL
#define dump(...)                                                              \\
  do {                                                                         \\
    std::ostringstream os;                                                     \\
    os << __LINE__ << ":\\t" << #__VA_ARGS__ << " = ";                          \\
    __print__(os, ", ", "\\n", __VA_ARGS__);                                    \\
    std::cerr << os.str();                                                     \\
  } while (0)
#else
#define dump(...)
#endif

template <typename Fst, typename... Rst>
void println(const Fst &fst, const Rst &...rst) {
  __print__(std::cout, "\\n", "\\n", fst, rst...);
}
template <typename Fst, typename... Rst>
void print(const Fst &fst, const Rst &...rst) {
  __print__(std::cout, " ", "\\n", fst, rst...);
}

template <typename iter> void println_(iter bgn, iter end) {
  while (bgn != end)
    println(*bgn++);
}

template <typename iter> void print_(iter bgn, iter end) {
  while (bgn != end) {
    std::cout << *bgn++;
    std::cout << (bgn == end ? "\\n" : " ");
  }
}

int _ = (std::cout.precision(10), std::cout.setf(std::ios::fixed),
         std::cin.tie(0), std::ios::sync_with_stdio(0), 0);

template <typename T> std::vector<T> ndarray(int n, T v) {
  return std::vector<T>(n, v);
}
template <typename... Args> auto ndarray(int n, Args... args) {
  auto val = ndarray(args...);
  return std::vector<decltype(val)>(n, move(val));
}

template <typename T> bool umax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

template <typename T> bool umin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

using namespace std;

template <typename monoid> struct segment_tree {
  using M = monoid;
  using T = typename M::value_type;

  std::size_t sz;
  std::vector<T> x;

  segment_tree(std::size_t n = 0) {
    sz = 1;
    while (sz < n)
      sz *= 2;
    x.assign(sz * 2, M::id());
    initialize();
  }

  template <typename iterator> segment_tree(iterator first, iterator last) {
    sz = 1;
    std::size_t n = std::distance(first, last);
    while (sz < n)
      sz *= 2;
    x.assign(sz * 2, M::id());
    std::copy(first, last, x.begin() + sz);
    initialize();
  }

  void fill(const T &val) {
    std::fill(x.begin() + sz, x.end(), val);
    initialize();
  }

  void initialize() {
    for (int i = (int)sz - 1; i >= 1; --i) {
      x[i] = M::op(x[i * 2 + 0], x[i * 2 + 1]);
    }
  }

  T accumulate(std::size_t l, std::size_t r) const {
    T al = M::id(), ar = M::id();
    for (l += sz, r += sz; l < r; l /= 2, r /= 2) {
      if (l & 1)
        al = M::op(al, x[l++]);
      if (r & 1)
        ar = M::op(x[--r], ar);
    }
    return M::op(al, ar);
  }

  void update(std::size_t i, const T &val) {
    x[i += sz] = val;
    while (i > 1) {
      x[i / 2] = M::op(x[i], x[i ^ 1]);
      i /= 2;
    }
  }

  T operator[](std::size_t i) const { return x[sz + i]; }
};

template <typename T> struct min_monoid {
  using value_type = T;
  static constexpr T id() { return 1e18; }
  static T op(const T &a, const T &b) { return std::min(a, b); }
};

template <typename T> struct max_monoid {
  using value_type = T;
  static constexpr value_type id() {
    return std::numeric_limits<value_type>::min();
  }
  static value_type op(const value_type &a, const value_type &b) {
    return std::max(a, b);
  }
};

template <typename T> struct sum_monoid {
  using value_type = T;
  static constexpr value_type id() { return 0; }
  static value_type op(const value_type &a, const value_type &b) {
    return a + b;
  }
};

template <typename value_type> using rmq = segment_tree<min_monoid<value_type>>;

template <typename value_type> using rsq = segment_tree<sum_monoid<value_type>>;

using pii = pair<int, int>;
int N, M, s, t;
int d[100010];
vector<pii> es;

signed main() {
  while (cin >> N >> M >> s >> t) {
    --s, --t;
    rep(i, N) cin >> d[i];
    es.resize(M);
    for (auto &e : es) {
      int a, b;
      cin >> a >> b;
      --a;
      --b;
      e = {a, b};
    }
    sort(all(es), [](pii a, pii b) { return a.second < b.second; });
    int ans;
    if (t < s) {
      ans = 0;
      assert(false);
    } else {
      rmq<ll> dp(N + 1);
      rep(i, s + 1) { dp.update(i, 0); }
      for (auto &e : es) {
        int l, r;
        tie(l, r) = e;
        int x = dp.accumulate(l, r) + d[r];
        dp.update(r, x);
        // rep(i, N) {
        //     cout << dp.accumulate(i, i + 1) << ' ';
        // }
        // cout << endl;
      }
      ans = dp.accumulate(t, N + 1);
    }
    cout << ans << endl;
  }
}