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

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

#define REP(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ALL(c) c.begin(), c.end()
#define SZ(c) (int)c.size()
#define DBG1(x) cout << #x << " = " << x << "\\n"
#if USE_CP_REDIR
#define CP                                                                     \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#endif

using ll = long long;
using pii = pair<int, int>;

const ll MOD = 998244353;
const int MN = 200'000;
int N, K;
ll DP[MN + 2];
ll DPSUM[MN + 2];

ll ADD(ll a, ll b) {
  a %= MOD;
  b %= MOD;
  return (a + b) % MOD;
}

ll cnt2(const vector<pii> &S) {
  DP[N] = 1;
  DPSUM[N] = 1;
  for (int i = N - 1; i > 0; i--) {
    for (const pii &s : S) {
      int l = i + s.first;
      int r = min(N, i + s.second);
      if (l > N)
        continue;
      DP[i] = ADD(DP[i], ADD(DPSUM[l], MOD - DPSUM[r + 1]));
    }
    DPSUM[i] = ADD(DPSUM[i + 1], DP[i]);
  }
  return DP[1];
}

int main() {
  ios::sync_with_stdio(false);
#ifdef CP
  CP;
#endif
  cin >> N >> K;
  vector<pii> S(K);
  REP(i, 0, K) cin >> S[i].first >> S[i].second;
  memset(DP, 0, sizeof DP);
  sort(ALL(S));
  cout << cnt2(S) << endl;
}

Error: TLE

Fixed submission:

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

#define REP(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ALL(c) c.begin(), c.end()
#define SZ(c) (int)c.size()
#define DBG1(x) cout << #x << " = " << x << "\\n"
#if USE_CP_REDIR
#define CP                                                                     \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#endif

using ll = long long;
using pii = pair<int, int>;

const ll MOD = 998244353;
const int MN = 200'000;
int N, K;
ll DP[MN + 100];

ll add(ll a, ll b) {
  a %= MOD;
  b %= MOD;
  return (a + b) % MOD;
}

ll cnt(int i, const vector<pii> &S) {
  if (i == N)
    return 1LL;
  if (i > N)
    return 0LL;

  ll &ans = DP[i];
  if (ans != -1)
    return ans;

  ans = 0LL;
  for (const pii &s : S) {
    for (int d = s.first; d <= s.second; d++) {
      if (i + d > N)
        goto fim;
      ans = add(ans, cnt(i + d, S));
    }
  }
fim:
  return ans;
}

ll cnt2(const vector<pii> &S) {
  DP[N] = 1;
  for (register int i = N - 1; i > 0; i--) {
    for (const pii &s : S) {
      for (int d = s.first; d <= s.second; d++) {
        if (i + d > N)
          goto outer;
        DP[i] = add(DP[i], DP[i + d]);
      }
    }
  outer:;
  }
  return DP[1];
}

int main() {
  ios::sync_with_stdio(false);
#ifdef CP
  CP;
#endif
  cin >> N >> K;
  vector<pii> S(K);
  REP(i, 0, K) cin >> S[i].first >> S[i].second;
  memset(DP, 0, sizeof DP);
  sort(ALL(S));
  cout << cnt2(S) << endl;
}