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

#include <bits/stdc++.h>

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define dump_1(x1) cerr << #x1 << ": " << x1 << endl
#define dump_2(x1, x2)                                                         \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define dump_3(x1, x2, x3)                                                     \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << endl
#define dump_4(x1, x2, x3, x4)                                                 \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << endl
#define dump_5(x1, x2, x3, x4, x5)                                             \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#define dump(...)                                                              \\
  CHOOSE((__VA_ARGS__, dump_5, dump_4, dump_3, dump_2, dump_1, ~))(__VA_ARGS__)
#define check(s) cerr << s << endl

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

using namespace std;

using ll = long long;

const long long MOD = 998244353;

vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

const ll LINF = 2e18;
const int INF = 1e9;

ll N;
ll K;
std::vector<ll> L;
std::vector<ll> R;

vector<ll> dp;

vector<pair<ll, ll>> f(ll i) {
  vector<pair<ll, ll>> ret;
  rep(k, K) {
    ll l = L.at(k);
    ll r = R.at(k);
    if (i - l > 0) {
      pair<ll, ll> p = {i - l, max(1LL, i - r)};
      ret.push_back(p);
    }
  }
  return ret;
}

void solve() {
  dp.resize(N, 0);
  dp.at(0) = 1;

  vector<ll> acc(N + 1, 0);
  acc.at(0) = 0;
  acc.at(1) = 1;

  rep(i, N - 1) {
    for (auto p : f(i + 2)) {
      ll l = p.second - 1;
      ll r = p.first;
      dp.at(i + 1) += (acc.at(r) - acc.at(l) + MOD) % MOD;
      dp.at(i + 1) %= MOD;
    }
    acc.at(i + 2) = (acc.at(i + 1) + dp.at(i + 1)) % MOD;
  }

  cout << dp.at(N - 1) % MOD << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  scanf("%lld", &N);
  scanf("%lld", &K);
  L.resize(K);
  R.resize(K);
  for (int i = 0; i < K; i++) {
    scanf("%lld", &L[i]);
    scanf("%lld", &R[i]);
  }
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define dump_1(x1) cerr << #x1 << ": " << x1 << endl
#define dump_2(x1, x2)                                                         \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define dump_3(x1, x2, x3)                                                     \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << endl
#define dump_4(x1, x2, x3, x4)                                                 \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << endl
#define dump_5(x1, x2, x3, x4, x5)                                             \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#define dump(...)                                                              \\
  CHOOSE((__VA_ARGS__, dump_5, dump_4, dump_3, dump_2, dump_1, ~))(__VA_ARGS__)
#define check(s) cerr << s << endl

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

using namespace std;

using ll = long long;

const long long MOD = 998244353;

vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

const ll LINF = 2e18;
const int INF = 1e9;

ll N;
ll K;
std::vector<ll> L;
std::vector<ll> R;

vector<ll> dp;

vector<pair<ll, ll>> f(ll i) {
  vector<pair<ll, ll>> ret;
  rep(k, K) {
    ll l = L.at(k);
    ll r = R.at(k);
    if (i - l > 0) {
      pair<ll, ll> p = {i - l, max(1LL, i - r)};
      ret.push_back(p);
    }
  }
  return ret;
}

void solve() {
  dp.resize(N + 1, 0);
  dp.at(0) = 1;

  rep(i, N) {
    for (auto p : f(i + 2)) {
      ll l = p.second;
      ll r = p.first;
      for (int j = l; j <= r; j++) {
        dp.at(i + 1) += dp.at(j - 1) % MOD;
      }
    }
  }

  cout << dp.at(N - 1) % MOD << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  scanf("%lld", &N);
  scanf("%lld", &K);
  L.resize(K);
  R.resize(K);
  for (int i = 0; i < K; i++) {
    scanf("%lld", &L[i]);
    scanf("%lld", &R[i]);
  }
  solve();
  return 0;
}