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

#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
template <typename T> bool chmax(T &u, const T z) {
  if (u < z) {
    u = z;
    return true;
  } else
    return false;
}
template <typename T> bool chmin(T &u, const T z) {
  if (u > z) {
    u = z;
    return true;
  } else
    return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
typedef pair<ll, ll> P;
const ll INF = 1LL << 60;
// const int MOD = 1000000007;
const ll MOD = 998244353;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

struct mint {
  long long x; // typedef long long ll;
  mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  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 { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(long long t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime MOD
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

mint dp[202020];
mint dprui[202020];
int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<int, int>> h(k);
  rep(i, k) {
    int l, r;
    cin >> h[i].first >> h[i].second;
  }
  dp[1] = 1;
  dprui[1] = 1;
  for (int i = 2; i <= n; i++) {
    rep(j, k) {
      int li = i - h[j].second;
      int ri = i - h[j].first;
      if (ri < 0)
        continue;
      li = max(li, 1);
      dp[i] += dprui[ri] - dprui[li - 1];
    }
    dprui[i] = dprui[i - 1] + dp[i];
  }
  cout << dp[n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
template <typename T> bool chmax(T &u, const T z) {
  if (u < z) {
    u = z;
    return true;
  } else
    return false;
}
template <typename T> bool chmin(T &u, const T z) {
  if (u > z) {
    u = z;
    return true;
  } else
    return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
typedef pair<ll, ll> P;
const ll INF = 1LL << 60;
// const int MOD = 1000000007;
const ll MOD = 998244353;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

ll dp[202020];
ll mp[202020];
int main() {
  ll n, k;
  cin >> n >> k;
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    mp[l]++;
    mp[r + 1]--;
  }
  for (int i = 1; i < 202020; i++) {
    mp[i] += mp[i - 1];
  }
  vector<int> num;
  rep(i, 202020) if (mp[i] > 0) num.push_back(i);
  sort(num.begin(), num.end());
  dp[1] = 1;
  for (int i = 1; i <= n; i++) {
    rep(j, num.size()) {
      if (i + num[j] > n)
        break;
      dp[i + num[j]] += dp[i];
      dp[i + num[j]] %= MOD;
    }
  }
  cout << dp[n] << endl;
  return 0;
}