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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
const int maxn = 10000;
const int INF32 = 1'050'000'000;
const long long INF64 = 4'000'000'000'000'000'000;
const int MOD7 = 1'000'000'007;
const int MOD9 = 1'000'000'009;

const int mod = 998244353;

void ERROR(int num) { cout << "ERROR" << num << endl; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int dx[8] = {-1, 0, 1, 0, 1, 1, -1, -1};
int dy[8] = {0, -1, 0, 1, 1, -1, 1, -1};

struct mint {
  using ull = unsigned long long int;
  ull v;
  mint(ll vv = 0) { s(vv % mod + mod); }
  mint &s(ull vv) {
    v = vv < mod ? vv : vv - mod;
    return *this;
  }
  // オーバーロード
  mint operator-() const { return mint() - *this; } // 符号反転
  mint &operator+=(const mint &val) { return s(v + val.v); }
  mint &operator-=(const mint &val) { return s(v + mod - val.v); }
  mint &operator*=(const mint &val) {
    v = ull(v) * val.v % mod;
    return *this;
  }
  mint &operator/=(const mint &val) { return *this *= val.inv(); }
  mint operator+(const mint &val) { return mint(*this) += val; }
  mint operator-(const mint &val) { return mint(*this) -= val; }
  mint operator*(const mint &val) { return mint(*this) *= val; }
  mint operator/(const mint &val) { return mint(*this) /= val; }
  mint pow(int n) const {
    mint res(1), x(*this);
    while (n) {
      if (n & 1)
        res *= x;
      x *= x;
      n >>= 1;
    }
    return res;
  }
  mint inv() const { return pow(mod - 2); }
  // 拡張ユークリッドの互除法
  /* mint inv()const{
      int x,y;
      int g=extgcd(v,mod,x,y);
      assert(g==1);
      if(x<0)x+=mod;
      return mint(x);
  }*/
  friend ostream &operator<<(ostream &os, const mint &val) {
    return os << val.v;
  } // 出力
  bool operator<(const mint &val) const { return v < val.v; }
  bool operator==(const mint &val) const { return v == val.v; }
  bool operator>(const mint &val) const { return v > val.v; }
};

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  // input
  int N, K;
  cin >> N >> K;
  vector<int> L(K), R(K);
  rep(i, K) cin >> L[i] >> R[i];
  // dp
  vector<mint> dp(N + 5, 0), dpsum(N + 5, 0);
  dp[1] = 1;
  dpsum[1] = 1;
  for (int i = 1; i < N; i++) {
    for (int k = 0; k < K; k++) {
      int left = i + 1 - R[k];
      int right = i + 1 - L[k];
      if (right < 0)
        continue;
      left = max(left, 1);
      dp[i + 1] += dpsum[right] - dpsum[left - 1];
    }
    dpsum[i + 1] = dpsum[i] + dp[i + 1];
  }
  // output
  cout << dp[N] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
const int maxn = 10000;
const int INF32 = 1'050'000'000;
const long long INF64 = 4'000'000'000'000'000'000;
const int MOD7 = 1'000'000'007;
const int MOD9 = 1'000'000'009;

const int mod = 998244353;

void ERROR(int num) { cout << "ERROR" << num << endl; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int dx[8] = {-1, 0, 1, 0, 1, 1, -1, -1};
int dy[8] = {0, -1, 0, 1, 1, -1, 1, -1};

struct mint {
  using ull = unsigned long long int;
  ull v;
  mint(ll vv = 0) { s(vv % mod + mod); }
  mint &s(ull vv) {
    v = vv < mod ? vv : vv - mod;
    return *this;
  }
  // オーバーロード
  mint operator-() const { return mint() - *this; } // 符号反転
  mint &operator+=(const mint &val) { return s(v + val.v); }
  mint &operator-=(const mint &val) { return s(v + mod - val.v); }
  mint &operator*=(const mint &val) {
    v = ull(v) * val.v % mod;
    return *this;
  }
  mint &operator/=(const mint &val) { return *this *= val.inv(); }
  mint operator+(const mint &val) { return mint(*this) += val; }
  mint operator-(const mint &val) { return mint(*this) -= val; }
  mint operator*(const mint &val) { return mint(*this) *= val; }
  mint operator/(const mint &val) { return mint(*this) /= val; }
  mint pow(int n) const {
    mint res(1), x(*this);
    while (n) {
      if (n & 1)
        res *= x;
      x *= x;
      n >>= 1;
    }
    return res;
  }
  mint inv() const { return pow(mod - 2); }
  // 拡張ユークリッドの互除法
  /* mint inv()const{
      int x,y;
      int g=extgcd(v,mod,x,y);
      assert(g==1);
      if(x<0)x+=mod;
      return mint(x);
  }*/
  friend ostream &operator<<(ostream &os, const mint &val) {
    return os << val.v;
  } // 出力
  bool operator<(const mint &val) const { return v < val.v; }
  bool operator==(const mint &val) const { return v == val.v; }
  bool operator>(const mint &val) const { return v > val.v; }
};

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  ll N, K;
  cin >> N >> K;
  vector<ll> L(K), R(K);
  rep(i, K) cin >> L[i] >> R[i];
  vector<ll> set;
  rep(i, K) {
    for (int num = L[i]; num <= R[i]; num++) {
      set.pb(num);
    }
  }

  sort(set.begin(), set.end());

  // for(int i = 0; i < set.size(); i++) cout << set[i] << " ";
  // cout << endl;

  vector<mint> dp(N + 5, 0);
  dp[0] = 1;
  for (ll i = 1; i < N; i++) {
    for (ll step : set) {
      // cout << step << endl;
      if (i - step >= 0)
        dp[i] += dp[i - step];
      else
        break;
    }
  }
  // for(int i = 0; i < N; i++) cout << dp[i] << " ";
  // cout << endl;
  cout << dp[N - 1] << endl;
}