Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;

// Definitions
#define MAXN 210
// #define MAXN 200010
#define MOD 998244353
#define elif else if
typedef long long int64;
#define File(s) freopen(#s ".in", "r", stdin), freopen(#s ".out", "w", stdout)

// Variables
int n, k, l[MAXN], r[MAXN];

// Pynamic Programming
int dp[MAXN], pfxsum[MAXN];
inline void DP() {
  dp[1] = 1;
  for (int cur = 1, begin, end; cur <= n; cur++) {
    for (int idx = 1; idx <= k; idx++) {
      begin = max(1, cur - r[idx]);
      end = max(0, cur - l[idx]);
      // for (int last = begin; last <= end; last++) {
      //   dp[cur] = (dp[cur] + dp[last]) % MOD;
      // }
      if (begin <= end) {
        dp[cur] = dp[cur] + (pfxsum[end] + MOD - pfxsum[begin - 1]) % MOD;
        dp[cur] %= MOD;
      }
    }
    pfxsum[cur] = (pfxsum[cur - 1] + dp[cur]) % MOD;
  }
}

// Functions
inline void Monitor() {
  for (int i = 1; i <= n; i++) {
    printf("%d ", dp[i]);
  }
  puts("");
}
inline void Process() { DP(); }
inline void ReadIn() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= k; i++) {
    scanf("%d %d", l + i, r + i);
  }
}
inline void PrintAns() {
  // Monitor();
  printf("%d\n", dp[n]);
}

int main() {
#ifndef ONLINE_JUDGE
  File(data);
#endif
  ReadIn();
  Process();
  PrintAns();
  return 0;
}
/*

==== O(n^2) {Diffuse} ====
> dp[1] = 1;
> for (int cur = 1; cur <= n; cur++) {
>   for (int idx = 1; idx <= k; idx++) {
>     for (int add = l[idx]; add <= r[idx]; add++) {
>       dp[cur + add] = (dp[cur + add] + dp[cur]) % MOD;
>     }
>   }
> }
==== O(n^2) {Collect} ===
> dp[1] = 1;
> for (int cur = 1, begin, end; cur <= n; cur++) {
>   for (int idx = 1; idx <= k; idx++) {
>     begin = max(1, cur - r[idx]);
>     end = max(0, cur - l[idx]);
>     for (int last = begin; last <= end; last++) {
>       dp[cur] = (dp[cur] + dp[last]) % MOD;
>     }
>   }
> }

*/

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;

// Definitions
// #define MAXN 210
#define MAXN 200010
#define MOD 998244353
#define elif else if
typedef long long int64;
#define File(s) freopen(#s ".in", "r", stdin), freopen(#s ".out", "w", stdout)

// Variables
int n, k, l[MAXN], r[MAXN];

// Pynamic Programming
int dp[MAXN], pfxsum[MAXN];
inline void DP() {
  dp[1] = 1;
  for (int cur = 1, begin, end; cur <= n; cur++) {
    for (int idx = 1; idx <= k; idx++) {
      begin = max(1, cur - r[idx]);
      end = max(0, cur - l[idx]);
      // for (int last = begin; last <= end; last++) {
      //   dp[cur] = (dp[cur] + dp[last]) % MOD;
      // }
      if (begin <= end) {
        dp[cur] = dp[cur] + (pfxsum[end] + MOD - pfxsum[begin - 1]) % MOD;
        dp[cur] %= MOD;
      }
    }
    pfxsum[cur] = (pfxsum[cur - 1] + dp[cur]) % MOD;
  }
}

// Functions
inline void Monitor() {
  for (int i = 1; i <= n; i++) {
    printf("%d ", dp[i]);
  }
  puts("");
}
inline void Process() { DP(); }
inline void ReadIn() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= k; i++) {
    scanf("%d %d", l + i, r + i);
  }
}
inline void PrintAns() {
  // Monitor();
  printf("%d\n", dp[n]);
}

int main() {
#ifndef ONLINE_JUDGE
  File(data);
#endif
  ReadIn();
  Process();
  PrintAns();
  return 0;
}
/*

==== O(n^2) {Diffuse} ====
> dp[1] = 1;
> for (int cur = 1; cur <= n; cur++) {
>   for (int idx = 1; idx <= k; idx++) {
>     for (int add = l[idx]; add <= r[idx]; add++) {
>       dp[cur + add] = (dp[cur + add] + dp[cur]) % MOD;
>     }
>   }
> }
==== O(n^2) {Collect} ===
> dp[1] = 1;
> for (int cur = 1, begin, end; cur <= n; cur++) {
>   for (int idx = 1; idx <= k; idx++) {
>     begin = max(1, cur - r[idx]);
>     end = max(0, cur - l[idx]);
>     for (int last = begin; last <= end; last++) {
>       dp[cur] = (dp[cur] + dp[last]) % MOD;
>     }
>   }
> }

*/