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

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()

template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vpi = vector<Pi>;
using vpl = vector<Pl>;

#define debug(arr) cout << #arr << " = " << arr << '\\n'
#define debug2(a, b)                                                           \\
  cout << "[" << #a << "," << #b << "] = "                                     \\
       << "[" << a << "," << b << "]" << '\\n'
template <class T> ostream &operator<<(ostream &out, const vector<T> &arr) {
  cout << "{";
  for (int i = 0; i < arr.size(); i++)
    cout << (!i ? "" : ", ") << arr[i];
  cout << "}";
  return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &arr) {
  cout << "{\\n";
  for (auto &vec : arr)
    cout << "  " << vec << ",\\n";
  cout << "}";
  return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &p) {
  cout << "{" << p.first << "," << p.second << "}" << '\\n';
  return out;
}
template <class T> istream &operator>>(istream &in, vector<T> &arr) {
  for (auto &i : arr)
    cin >> i;
  return in;
}

/////////////////////////////////////////////////////////////////////////
#define MOD 998244353
template <int mod> struct Modint {
  long long x; // typedef long long ll;
  Modint(long long x = 0) : x((x % mod + mod) % mod) {}
  Modint &operator+=(const Modint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  Modint &operator-=(const Modint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  Modint &operator*=(const Modint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  Modint operator+(const Modint a) const {
    Modint res(*this);
    return res += a;
  }
  Modint operator-(const Modint a) const {
    Modint res(*this);
    return res -= a;
  }
  Modint operator*(const Modint a) const {
    Modint res(*this);
    return res *= a;
  }
  Modint pow(ll t) const {
    if (!t)
      return 1;
    Modint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  // for prime mod
  Modint inv() const { return pow(mod - 2); }
  Modint &operator/=(const Modint a) { return (*this) *= a.inv(); }
};
using mint = Modint<MOD>;

template <class T> class RangeSumQuery {
private:
  vector<T> dat;
  int n;
  T INF;

public:
  RangeSumQuery(int n_) {
    INF = 0;
    n = 1;
    while (n < n_)
      n *= 2;
    dat.assign(2 * n - 1, INF);
  }
  void update(int k, T a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = dat[k * 2 + 1] + dat[k * 2 + 2];
    }
  }
  T query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return INF;
    if (a <= l && r <= b)
      return dat[k];
    else {
      T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return vl + vr;
    }
  }
  T query(int a, int b) { return query(a, b, 0, 0, n); }
};

mint dp[200010];
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(20);

  int n, k;
  cin >> n >> k;
  RangeSumQuery<mint> ST(n + 1);
  vector<Pi> range(k);
  REP(i, k) { cin >> range[i].first >> range[i].second; }
  sort(all(range));

  dp[0] = 1;
  ST.update(0, 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      int l = i - range[j].second;
      int r = i - range[j].first + 1;
      if (r < 0)
        continue;
      dp[i] += ST.query(max(0, l), r);
      ST.update(i, dp[i]);
    }
  }

  cout << dp[n - 1].x << '\\n';

  return 0;
}


Error: 0

Fixed submission:

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()

template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vpi = vector<Pi>;
using vpl = vector<Pl>;

#define debug(arr) cout << #arr << " = " << arr << '\\n'
#define debug2(a, b)                                                           \\
  cout << "[" << #a << "," << #b << "] = "                                     \\
       << "[" << a << "," << b << "]" << '\\n'
template <class T> ostream &operator<<(ostream &out, const vector<T> &arr) {
  cout << "{";
  for (int i = 0; i < arr.size(); i++)
    cout << (!i ? "" : ", ") << arr[i];
  cout << "}";
  return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &arr) {
  cout << "{\\n";
  for (auto &vec : arr)
    cout << "  " << vec << ",\\n";
  cout << "}";
  return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &p) {
  cout << "{" << p.first << "," << p.second << "}" << '\\n';
  return out;
}
template <class T> istream &operator>>(istream &in, vector<T> &arr) {
  for (auto &i : arr)
    cin >> i;
  return in;
}

/////////////////////////////////////////////////////////////////////////
#define MOD 998244353
template <int mod> struct Modint {
  long long x; // typedef long long ll;
  Modint(long long x = 0) : x((x % mod + mod) % mod) {}
  Modint &operator+=(const Modint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  Modint &operator-=(const Modint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  Modint &operator*=(const Modint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  Modint operator+(const Modint a) const {
    Modint res(*this);
    return res += a;
  }
  Modint operator-(const Modint a) const {
    Modint res(*this);
    return res -= a;
  }
  Modint operator*(const Modint a) const {
    Modint res(*this);
    return res *= a;
  }
  Modint pow(ll t) const {
    if (!t)
      return 1;
    Modint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  // for prime mod
  Modint inv() const { return pow(mod - 2); }
  Modint &operator/=(const Modint a) { return (*this) *= a.inv(); }
};
using mint = Modint<MOD>;

template <class T> class RangeSumQuery {
private:
  vector<T> dat;
  int n;
  T INF;

public:
  RangeSumQuery(int n_) {
    INF = 0;
    n = 1;
    while (n < n_)
      n *= 2;
    dat.assign(2 * n - 1, INF);
  }
  void update(int k, T a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = dat[k * 2 + 1] + dat[k * 2 + 2];
    }
  }
  T query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return INF;
    if (a <= l && r <= b)
      return dat[k];
    else {
      T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return vl + vr;
    }
  }
  T query(int a, int b) { return query(a, b, 0, 0, n); }
};

mint dp[100010];
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(20);

  int n, k;
  cin >> n >> k;
  RangeSumQuery<mint> ST(n + 1);
  vector<Pi> range(k);
  REP(i, k) { cin >> range[i].first >> range[i].second; }
  sort(all(range));

  dp[0] = 1;
  ST.update(0, 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      int l = i - range[j].second;
      int r = i - range[j].first + 1;
      if (r < 0)
        continue;
      dp[i] += ST.query(max(0, l), r);
      ST.update(i, dp[i]);
    }
  }

  cout << dp[n - 1].x << '\\n';

  return 0;
}