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

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 998244353LL

#define rep(i, n) for (ll(i) = 0LL; (i) < (ll)(n); (i)++)
#define rep2(i, s, e) for (ll(i) = (ll)(s); (i) < (ll)(e); (i)++)
#define repi(i, n) for (ll(i) = 0LL; (i) <= (ll)(n); (i)++)
#define repi2(i, s, e) for (ll(i) = (ll)(s); (i) <= (ll)(e); (i)++)
#define per(i, n) for (ll(i) = (ll)(n)-1LL; (i) >= 0LL; (i)--)
#define per2(i, s, e) for (ll(i) = (ll)(s)-1LL; (i) >= (ll)(e); (i)--)
#define peri(i, n) for (ll(i) = (ll)(n); (i) >= 0LL; (i)--)
#define peri2(i, s, e) for (ll(i) = (ll)(s); (i) >= (ll)(e); (i)--)
#define iter(i, it) for (auto &(i) : (it))

template <typename T, typename U>
ostream &operator<<(ostream &s, const pair<T, U> m) {
  cout << "(" << m.first << ", " << m.second << ")";
  return s;
}
template <typename T, typename U>
ostream &operator<<(ostream &s, const map<T, U> m) {
  ll c = 0;
  cout << "{ ";
  iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  cout << "{ ";
  rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const list<T> &v) {
  ll c = 0;
  cout << "{ ";
  iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}

int main(void) {
  ll N, K;
  cin >> N >> K;
  vector<pair<ll, ll>> S;
  rep(i, K) {
    ll l, r;
    cin >> l >> r;
    S.emplace_back(l, r);
  }
  vector<ll> dp(N);
  dp.front() = 1;
  dp[1] = -1;
  rep(i, N - 1) {
    if (dp[i] == 0)
      continue;
    iter(j, S) {
      ll temp;
      temp = (i + j.first) % MOD;
      if (temp < dp.size()) {
        (dp[temp] += dp[i]) %= MOD;
      }
      temp = (i + j.second + 1) % MOD;
      if (temp < dp.size()) {
        (dp[temp] -= dp[i]) %= MOD;
        if (dp[temp] < 0)
          (dp[temp] += MOD) %= MOD;
      }
    }
    (dp[i + 1] += dp[i]) %= MOD;
  }
  cout << dp.back() << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 998244353LL

#define rep(i, n) for (ll(i) = 0LL; (i) < (ll)(n); (i)++)
#define rep2(i, s, e) for (ll(i) = (ll)(s); (i) < (ll)(e); (i)++)
#define repi(i, n) for (ll(i) = 0LL; (i) <= (ll)(n); (i)++)
#define repi2(i, s, e) for (ll(i) = (ll)(s); (i) <= (ll)(e); (i)++)
#define per(i, n) for (ll(i) = (ll)(n)-1LL; (i) >= 0LL; (i)--)
#define per2(i, s, e) for (ll(i) = (ll)(s)-1LL; (i) >= (ll)(e); (i)--)
#define peri(i, n) for (ll(i) = (ll)(n); (i) >= 0LL; (i)--)
#define peri2(i, s, e) for (ll(i) = (ll)(s); (i) >= (ll)(e); (i)--)
#define iter(i, it) for (auto &(i) : (it))

template <typename T, typename U>
ostream &operator<<(ostream &s, const pair<T, U> m) {
  cout << "(" << m.first << ", " << m.second << ")";
  return s;
}
template <typename T, typename U>
ostream &operator<<(ostream &s, const map<T, U> m) {
  ll c = 0;
  cout << "{ ";
  iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  cout << "{ ";
  rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const list<T> &v) {
  ll c = 0;
  cout << "{ ";
  iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}

int main(void) {
  ll N, K;
  cin >> N >> K;
  vector<ll> S;
  rep(i, K) {
    ll l, r;
    cin >> l >> r;
    repi2(j, l, r) S.push_back(j);
  }
  vector<ll> dp(N);
  dp.front() = 1;
  rep(i, N) {
    if (dp[i] == 0)
      continue;
    iter(j, S) {
      ll temp = (i + j) % MOD;
      if (temp < dp.size())
        (dp[temp] += dp[i]) %= MOD;
    }
  }
  cout << dp.back() << endl;
  return 0;
}