Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define FASTIO                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using ll = long long;

const int MOD = 998244353;
const ll INF = 1e16;

int main() {
  FASTIO

  int n, k;
  cin >> n >> k;
  int b[k][2];
  for (int i = 0; i < k; i++) {
    cin >> b[i][0] >> b[i][1];
  }

  vector<int> dp(n + 1, 0);
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      dp[i] += dp[max(i - b[j][0], 0)] - dp[max(i - b[j][1] - 1, 0)];
      if (dp[i] < 0)
        dp[i] += MOD;
      else
        dp[i] %= MOD;
    }
    dp[i] += dp[i - 1];
    if (dp[i] < 0)
      dp[i] += MOD;
    else
      dp[i] %= MOD;
  }
  int ways = dp[n] - dp[n - 1];
  if (ways < 0)
    cout << ways + MOD << "\\n";
  else
    cout << ways % MOD << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(index, num) for (int index = 0; index < num; index++)
#define rep1(index, num) for (int index = 1; index <= num; index++)
#define brep(index, num) for (int index = num - 1; index >= 0; index--)
#define brep1(index, num) for (int index = num; index > 0; index--)
#define prin(a) cout << a << "\\n"
#define kaigyo cout << "\\n"
#define mp(a1, a2) make_pair(a1, a2)
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
#define SORT(V) sort(ALL(V))
#define REV(V) reverse(ALL(V))
#define RSORT(V)                                                               \\
  SORT(V);                                                                     \\
  REV(V)
#define mem(x) memset(x, 0, sizeof(x))
#define mem1(x) memset(x, -1, sizeof(x))
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<string> vst;
typedef vector<char> vc;
typedef vector<long double> vd;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
typedef vector<vector<int>> vint2;
ll mod = 1e9 + 7;
ll smod = 998244353;
#define int1(x)                                                                \\
  int x;                                                                       \\
  cin >> x;
#define int2(x, y)                                                             \\
  int x, y;                                                                    \\
  cin >> x >> y;
#define int3(x, y, z)                                                          \\
  int x, y, z;                                                                 \\
  cin >> x >> y >> z;
#define int4(x, y, z, w)                                                       \\
  int x, y, z, w;                                                              \\
  cin >> x >> y >> z >> w;
#define ll1(x)                                                                 \\
  ll x;                                                                        \\
  cin >> x;
#define ll2(x, y)                                                              \\
  ll x, y;                                                                     \\
  cin >> x >> y;
#define vint1(a, n)                                                            \\
  vint a(n);                                                                   \\
  rep(i, n) cin >> a[i];
#define vint2(a, b, n)                                                         \\
  vint a(n), b(n);                                                             \\
  rep(i, n) cin >> a[i] >> b[i];
#define vll1(a, n)                                                             \\
  vll a(n);                                                                    \\
  rep(i, n) cin >> a[i];
struct aaa {
  aaa() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
} aaaaaaa;

const double pi = acos(-1.0);
const ll inf = 1LL << 60;

bool isPrime(ll n) {
  if (n < 2)
    return false;
  for (ll x = 2; x * x <= n; x++) {
    if (n % x == 0)
      return false;
  }
  return true;
}
ll kaizyo(ll k) {
  ll sum = 1;
  rep1(i, k) {
    sum *= i;
    sum %= mod;
  }
  return sum;
}
ll ncr(ll n, ll r) { return kaizyo(n) / (kaizyo(r) * kaizyo(n - r)); }
unsigned keta(unsigned num) { return log10(num) + 1; }
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {

  ll2(n, k);
  vll l(k), r(k);
  rep(i, k) cin >> l[i] >> r[i];
  vll s(n + 1, 0), cnt(n + 1, 0);
  cnt[1] = 1;
  rep1(i, n + 1) {
    rep(j, k) {
      if (l[j] <= i && i <= r[j])
        s[i]++;
    }
  }

  rep1(i, n) {
    rep1(j, i - 1) {
      if (s[i - j] > 0) { // cout<<i<<' '<<j<<' '<<cnt[j]<<endl;
        cnt[i] += cnt[j];
      }
    }
    cnt[i] %= smod;
  }
  prin(cnt[n]);
}