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

#include <bits/stdc++.h>
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repi(i, 0, n)
#define SZ(x) ((int)(x).size())
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
// using P = pair<int, int>;
struct Vec {
  double x, y;
};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;

const int mod = 998244353;
// const int mod = 1000000007;
class mint {
public:
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint &operator+=(const mint a) {
    if ((x += a.x) > mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll a) const {
    if (a == 0)
      return 1;
    mint b = pow(a >> 1);
    b *= b;
    if (a & 1)
      b *= (*this);
    return b;
  }
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
  friend ostream &operator<<(ostream &os, const mint &m) {
    os << m.x;
    return os;
  }
};

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  // cout << fixed << setprecision(15);

  // DPを使ってTLE
  int n, k;
  cin >> n >> k;

  vector<pair<int, int>> s;

  rep(i, k) {
    int l, r;
    cin >> l >> r;
    s.push_back(pair<int, int>(l, r));
  }

  vector<mint> imo(n + 1, 0);
  vector<mint> dp(n + 1, 0);

  dp[1] = 1;
  imo[1] = 1;
  repi(i, 2, n + 1) {
    for (auto &m : s) {
      int l, r;
      l = max(i - m.second, 1);
      r = i - m.first;
      if (r < 0)
        continue;
      dp[i] += imo[r] - imo[l - 1];
    }
    imo[i] += dp[i] + imo[i - 1];
  }
  cout << dp[n] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repi(i, 0, n)
#define SZ(x) ((int)(x).size())
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
// using P = pair<int, int>;
struct Vec {
  double x, y;
};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;

int dp[200008];
// int s[20008];
const int mod = 998244353;

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  // cout << fixed << setprecision(15);

  int n, k, p = 0;
  cin >> n >> k;

  vector<int> s;

  rep(i, k) {
    int l, r;
    cin >> l >> r;
    repi(m, l, r + 1) { s.push_back(m); }
  }

  rep(i, n) dp[i] = 0;

  dp[1] = 1;
  repi(i, 2, n + 1) {
    for (auto &l : s) {
      if ((i - l) <= 0)
        continue;
      dp[i] += dp[i - l];
      dp[i] %= mod;
    }
  }
  cout << dp[n] << endl;

  return 0;
}