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

#define _USE_MATH_DEFINES

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

constexpr ll MOD = 998244353;

#ifndef ONLINE_JUDGE
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
  o << "{";
  for (auto &x : obj)
    o << " (" << x.first << " : " << x.second << ")"
      << ",";
  o << " }";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const set<T> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const multiset<T> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &obj) {
  o << "[";
  for (int i = 0; i < (int)obj.size(); ++i)
    o << (i > 0 ? ", " : "") << obj[i];
  o << "]";
  return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &obj) {
  o << "(" << obj.first << ", " << obj.second << ")";
  return o;
}
template <template <class tmp> class T, class U>
ostream &operator<<(ostream &o, const T<U> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
void print_sim_py(void) { cout << endl; }
template <class Head> void print_sim_py(Head &&head) {
  cout << head;
  print_sim_py();
}
template <class Head, class... Tail>
void print_sim_py(Head &&head, Tail &&...tail) {
  cout << head << " ";
  print_sim_py(forward<Tail>(tail)...);
}

#define print(...) print_sim_py(__VA_ARGS__);
#else
#define print(...) ;
#endif

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll n, k;
  ll ans = 0;

  cin >> n >> k;

  vector<ll> l(k), r(k);
  vector<ll> dp(n, 0);
  vector<ll> sum(n + 1, 0);

  for (ll i = 0; i < k; i++) {
    cin >> l[i] >> r[i];
  }

  dp[0] = 1;
  sum[1] = 1;
  for (ll j = 2; j < n + 1; j++) {
    for (ll i = 0; i < k; i++) {
      ll li = max(j - l[i], 0ll);
      ll ri = max(j - r[i] - 1, 0ll);
      ll s = sum[li] - sum[ri];
      s = s < 0 ? s + MOD : s;
      dp[j - 1] = (dp[j - 1] + s) % MOD;
    }
    sum[j] = (sum[j - 1] + dp[j - 1]) % MOD;
  }
  print(sum) print(dp)

          cout
      << dp[n - 1] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

constexpr ll MOD = 998244353;

#ifndef ONLINE_JUDGE
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
  o << "{";
  for (auto &x : obj)
    o << " (" << x.first << " : " << x.second << ")"
      << ",";
  o << " }";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const set<T> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const multiset<T> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &obj) {
  o << "[";
  for (int i = 0; i < (int)obj.size(); ++i)
    o << (i > 0 ? ", " : "") << obj[i];
  o << "]";
  return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &obj) {
  o << "(" << obj.first << ", " << obj.second << ")";
  return o;
}
template <template <class tmp> class T, class U>
ostream &operator<<(ostream &o, const T<U> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
void print_sim_py(void) { cout << endl; }
template <class Head> void print_sim_py(Head &&head) {
  cout << head;
  print_sim_py();
}
template <class Head, class... Tail>
void print_sim_py(Head &&head, Tail &&...tail) {
  cout << head << " ";
  print_sim_py(forward<Tail>(tail)...);
}

#define print(...) print_sim_py(__VA_ARGS__);
#else
#define print(...) ;
#endif

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll n, k;
  ll ans = 0;

  cin >> n >> k;

  vector<ll> l(k), r(k);
  vector<ll> dp(n, 0);

  for (ll i = 0; i < k; i++) {
    cin >> l[i] >> r[i];
  }

  sort(l.begin(), l.end());
  sort(r.begin(), r.end());

  dp[0] = 1;
  for (ll j = 0; j < n; j++) {
    for (ll i = 0; i < k; i++) {
      for (ll x = l[i]; x <= r[i]; x++) {
        if (j + x < n)
          dp[j + x] = (dp[j] + dp[j + x]) % MOD;
      }
    }
  }

  cout << dp[n - 1] << endl;

  return 0;
}