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

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <fstream>
#include <iostream>

#include "bits/stdc++.h"
#include <iostream>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef int integer;
#define int long long
typedef pair<int, int> pii;
#define vii vector<pair<int, int>>
#define vi vector<int>
#define vvi vector<vector<int>>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define s(x) in >> x
#define s2(x, y) in >> x >> y
#define s3(x, y, z) in >> x >> y >> z
#define s4(x, y, z, w) in >> x >> y >> z >> w
#define p(x) out << (x) << endl
#define p2(x, y) out << (x) << ' ' << (y) << endl
#define p3(x, y, z) out << (x) << ' ' << (y) << ' ' << (z) << endl
#define p4(x, y, z, w)                                                         \\
  out << (x) << ' ' << (y) << ' ' << (z) << ' ' << (w) << endl
#define p1d(a, n)                                                              \\
  for (int ix = 0; ix < n - 1; ix++)                                           \\
    out << a[ix] << ' ';                                                       \\
  out << a[n - 1] << '\\n';
#define p2d(a, n, m)                                                           \\
  for (int ix = 0; ix < n; ix++) {                                             \\
    for (int jx = 0; jx < m; jx++)                                             \\
      out << a[ix][jx] << " ";                                                 \\
    out << endl;                                                               \\
  }
#define pl(x) out << (x)
#define pll(x) out << (x) << ' '
#define range(v) v.begin(), v.end()
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define rst(x) memset(x, -1, sizeof(x))
#define UNVISITED -1
#define VISITED 0
#define EXPLORED 2

#define EACH(x, a) for (auto &x : a)
template <class T> void read_v(T &x, std::istream &in) { in >> (x); }
template <class A> void read_vec(vector<A> &x, std::istream &in) {
  EACH(a, x)
  read_v(a, in);
}

ll iceildiv(const ll a, const ll b) {
  if (a == 0) {
    return 0;
  }
  return (1 + ((a - 1) / b));
}
bool sortpairbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}
template <typename T> T factmod(T a, T modulus) {
  int res = 1, i;
  for (i = 2; i <= a; i++) {
    res *= i;
    res %= modulus;
  }
  return res;
}
template <typename T> int powmod(T a, T b, const int m) {
  int ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans *= a;
      ans %= m;
    }
    a *= a;
    a %= m;
    b >>= 1;
  }
  return ans;
}
template <typename T> int power(T a, T b) {
  int ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans *= a;
    }
    a *= a;
    b >>= 1;
  }
  return ans;
}

const int CMOD = 998244353;

int dp_solve(const int n, const vector<pair<int, int>> &sv) {
  int k = sz(sv);
  vi dp(n, 0);
  dp[0] = 1;
  dp[1] = -1;
  //    for(auto x: sv){
  //        dp[x.first] = 1;
  //        dp[x.second] = -1;
  //    }

  for (int i = 0; i < n; i++) {
    if (i > 0) {
      dp[i] += dp[i - 1]; // integrate
      dp[i] %= CMOD;
    }
    for (int j = 0; j < k; j++) {
      int l, r;
      tie(l, r) = sv[j];
      if (i + l < n) {
        dp[i + l] += dp[i]; // marks
        dp[i + l] %= CMOD;
      }
      if (i + r < n) {
        dp[i + r] -= dp[i]; // marks
        dp[i + r] %= CMOD;
        dp[i + r] = (dp[i + r] < 0 ? dp[i + r] + CMOD : dp[i + r]);
      }
    }
  }
  int ans = dp[n - 1] % CMOD;
  ans = (ans < 0 ? ans + CMOD : ans);
  return ans;
}

void ssolve(std::istream &in, std::ostream &out) {
  int n, k;
  s2(n, k);
  vii sv;
  rep(i, 0, k) {
    int l, r;
    s2(l, r);
    r++;
    sv.emplace_back(make_pair(l, r));
  }
  sort(all(sv));
  int ans = dp_solve(n, sv);
  p(ans);
  return;
}

class DLeapingTak {
public:
  void solve(std::istream &in, std::ostream &out) {
    ios_base::sync_with_stdio(false);
    in.tie(nullptr);
    out.tie(nullptr);
    int t;
    int READ = 0;
    if (READ)
      s(t);
    else
      t = 1;
    while (t--) {
      ssolve(in, out);
    }
  }
};

integer main() {
  DLeapingTak solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
}

Error: 0

Fixed submission:

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <fstream>
#include <iostream>

#include "bits/stdc++.h"
#include <iostream>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef int integer;
#define int long long
typedef pair<int, int> pii;
#define vii vector<pair<int, int>>
#define vi vector<int>
#define vvi vector<vector<int>>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define s(x) in >> x
#define s2(x, y) in >> x >> y
#define s3(x, y, z) in >> x >> y >> z
#define s4(x, y, z, w) in >> x >> y >> z >> w
#define p(x) out << (x) << endl
#define p2(x, y) out << (x) << ' ' << (y) << endl
#define p3(x, y, z) out << (x) << ' ' << (y) << ' ' << (z) << endl
#define p4(x, y, z, w)                                                         \\
  out << (x) << ' ' << (y) << ' ' << (z) << ' ' << (w) << endl
#define p1d(a, n)                                                              \\
  for (int ix = 0; ix < n - 1; ix++)                                           \\
    out << a[ix] << ' ';                                                       \\
  out << a[n - 1] << '\\n';
#define p2d(a, n, m)                                                           \\
  for (int ix = 0; ix < n; ix++) {                                             \\
    for (int jx = 0; jx < m; jx++)                                             \\
      out << a[ix][jx] << " ";                                                 \\
    out << endl;                                                               \\
  }
#define pl(x) out << (x)
#define pll(x) out << (x) << ' '
#define range(v) v.begin(), v.end()
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define rst(x) memset(x, -1, sizeof(x))
#define UNVISITED -1
#define VISITED 0
#define EXPLORED 2

#define EACH(x, a) for (auto &x : a)
template <class T> void read_v(T &x, std::istream &in) { in >> (x); }
template <class A> void read_vec(vector<A> &x, std::istream &in) {
  EACH(a, x)
  read_v(a, in);
}

ll iceildiv(const ll a, const ll b) {
  if (a == 0) {
    return 0;
  }
  return (1 + ((a - 1) / b));
}
bool sortpairbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}
template <typename T> T factmod(T a, T modulus) {
  int res = 1, i;
  for (i = 2; i <= a; i++) {
    res *= i;
    res %= modulus;
  }
  return res;
}
template <typename T> int powmod(T a, T b, const int m) {
  int ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans *= a;
      ans %= m;
    }
    a *= a;
    a %= m;
    b >>= 1;
  }
  return ans;
}
template <typename T> int power(T a, T b) {
  int ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans *= a;
    }
    a *= a;
    b >>= 1;
  }
  return ans;
}

const int CMOD = 998244353;

int dp_solve(const int n, const vector<pair<int, int>> &sv) {
  int k = sz(sv);
  vi dp(n + 1, 0);
  for (auto x : sv) {
    dp[x.first] = 1;
    dp[x.second] = -1;
  }

  for (int i = 1; i <= n; i++) {
    if (i > 1) {
      dp[i] += dp[i - 1]; // integrate
      dp[i] %= CMOD;
    }
    for (int j = 0; j < k; j++) {
      int l, r;
      tie(l, r) = sv[j];
      if (i + l <= n) {
        dp[i + l] += dp[i]; // marks
        dp[i + l] %= CMOD;
      }
      if (i + r <= n) {
        dp[i + r] -= dp[i]; // marks
        dp[i + r] %= CMOD;
        dp[i + r] = (dp[i + r] < 0 ? dp[i + r] + CMOD : dp[i + r]);
      }
    }
  }
  int ans = dp[n - 1] % CMOD;
  ans = (ans < 0 ? ans + CMOD : ans);
  return ans;
}

void ssolve(std::istream &in, std::ostream &out) {
  int n, k;
  s2(n, k);
  vii sv;
  rep(i, 0, k) {
    int l, r;
    s2(l, r);
    r++;
    sv.emplace_back(make_pair(l, r));
  }
  sort(all(sv));
  int ans = dp_solve(n, sv);
  p(ans);
  return;
}

class DLeapingTak {
public:
  void solve(std::istream &in, std::ostream &out) {
    ios_base::sync_with_stdio(false);
    in.tie(nullptr);
    out.tie(nullptr);
    int t;
    int READ = 0;
    if (READ)
      s(t);
    else
      t = 1;
    while (t--) {
      ssolve(in, out);
    }
  }
};

integer main() {
  DLeapingTak solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
}