Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <map>
#include <set>
#include <vector>
#define MOD1 1000000007
#define MOD2 998244353
#define LIMIT1 200010
#define LIMIT2 500010
#define LIMIT3 1000010
#define INF ((1 << 30) - 1)
#define LLINF (1LL << 60)
#define EPS (1e-10)
typedef long long ll;
typedef long double ld;
typedef const void cv;
typedef pair<ll, ll> P;
#define rep(i, n) for ((i) = 0; (i) < (n); (i)++)
#define per(i, n) for ((i) = (n)-1; (i) >= 0; (i)--)
template <class T, class C> T max(T a, C b) {
  return ((a) > (T)(b) ? (a) : (T)(b));
}
template <class T, class C> T min(T a, C b) {
  return ((a) < (T)(b) ? (a) : (T)(b));
}
#define zt(a, b) (max((a), (b)) - min((a), (b)))
#define setpre(n) fixed << setprecision(n)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}, dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int upint(cv *a, cv *b) {
  return *(int *)a < *(int *)b ? -1 : *(int *)a > *(int *)b ? 1 : 0;
}
int downint(cv *a, cv *b) {
  return *(int *)a < *(int *)b ? 1 : *(int *)a > *(int *)b ? -1 : 0;
}
int upchar(cv *left, cv *right) { return strcmp((char *)left, (char *)right); }
int downchar(cv *left, cv *right) {
  return strcmp((char *)right, (char *)left);
}
ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1) {
      res *= a;
      if (mod >= 1)
        res %= mod;
    }
    a *= a;
    if (mod >= 1)
      a %= mod;
    n >>= 1;
  }
  return res;
}
void initialize() {}
const int MAX_N = LIMIT2;
int bit[MAX_N + 1] = {0}, n;
// 1-indexed BIT
ll sum(int i) {
  ll s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & (-i);
  }
  return s;
}
void add(int i, ll x) {
  while (i <= n) {
    bit[i] += x;
    i += i & (-i);
  }
}
ll rangesum(int l, int r) {
  if (l > r)
    return 0;
  return sum(r) - sum(l - 1);
}
int main(void) {
  initialize();
  ll m, i, j, k, result = 0;
  ll l[LIMIT1] = {0}, r[LIMIT1] = {0};
  ll dp[LIMIT1] = {0};
  string s;

  cin >> n >> k;
  rep(i, k) { cin >> l[i] >> r[i]; }

  dp[1] = 1;
  for (i = 1; i <= n; i++) {
    dp[i] += dp[i - 1];
    rep(j, k) {
      if (i + l[j] <= n) {
        dp[i + l[j]] += dp[i];
        dp[i + l[j]] %= MOD2;
        if (i + r[j] <= n)
          dp[i + r[j] + 1] -= dp[i];
        if (dp[i + r[j] + 1] < 0)
          dp[i + r[j] + 1] += MOD2;
        // cout << i << ":" << i+l[j] << ":" << i+r[j] << endl;
      }
    }
  }

  result = dp[n] - dp[n - 1];

  cout << result << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <string>
using namespace std;
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <map>
#include <set>
#include <vector>
#define MOD1 1000000007
#define MOD2 998244353
#define LIMIT1 200010
#define LIMIT2 500010
#define LIMIT3 1000010
#define INF ((1 << 30) - 1)
#define LLINF (1LL << 60)
#define EPS (1e-10)
typedef long long ll;
typedef long double ld;
typedef const void cv;
typedef pair<ll, ll> P;
#define rep(i, n) for ((i) = 0; (i) < (n); (i)++)
#define per(i, n) for ((i) = (n)-1; (i) >= 0; (i)--)
template <class T, class C> T max(T a, C b) {
  return ((a) > (T)(b) ? (a) : (T)(b));
}
template <class T, class C> T min(T a, C b) {
  return ((a) < (T)(b) ? (a) : (T)(b));
}
#define zt(a, b) (max((a), (b)) - min((a), (b)))
#define setpre(n) fixed << setprecision(n)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}, dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int upint(cv *a, cv *b) {
  return *(int *)a < *(int *)b ? -1 : *(int *)a > *(int *)b ? 1 : 0;
}
int downint(cv *a, cv *b) {
  return *(int *)a < *(int *)b ? 1 : *(int *)a > *(int *)b ? -1 : 0;
}
int upchar(cv *left, cv *right) { return strcmp((char *)left, (char *)right); }
int downchar(cv *left, cv *right) {
  return strcmp((char *)right, (char *)left);
}
ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1) {
      res *= a;
      if (mod >= 1)
        res %= mod;
    }
    a *= a;
    if (mod >= 1)
      a %= mod;
    n >>= 1;
  }
  return res;
}
void initialize() {}
const int MAX_N = LIMIT2;
int bit[MAX_N + 1] = {0}, n;
// 1-indexed BIT
ll sum(int i) {
  ll s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & (-i);
  }
  return s;
}
void add(int i, ll x) {
  while (i <= n) {
    bit[i] += x;
    i += i & (-i);
  }
}
ll rangesum(int l, int r) {
  if (l > r)
    return 0;
  return sum(r) - sum(l - 1);
}
int main(void) {
  initialize();
  ll m, i, j, k, result = 0;
  ll l[LIMIT1] = {0}, r[LIMIT1] = {0};
  ll dp[LIMIT1] = {0};
  string s;

  cin >> n >> k;
  rep(i, k) { cin >> l[i] >> r[i]; }

  dp[1] = 1;
  for (i = 1; i <= n; i++) {
    dp[i] += dp[i - 1];
    rep(j, k) {
      if (i + l[j] <= n) {
        dp[i + l[j]] += dp[i];
        dp[i + l[j]] %= MOD2;
        if (i + r[j] <= n)
          dp[i + r[j] + 1] -= dp[i];
        if (i + r[j] + 1 <= n && dp[i + r[j] + 1] < 0)
          dp[i + r[j] + 1] += MOD2;
        // cout << i << ":" << i+l[j] << ":" << i+r[j] << endl;
      }
    }
  }

  result = dp[n] - dp[n - 1];

  cout << result << endl;
  return 0;
}