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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1e18
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define pb push_back
#define ff first
#define ss second
#define vll vector<ll>
#define pii pair<ll, ll>
#define sz(v) (v).size()
#define srt(v) sort(v.begin(), v.end())
#define ull unsigned long long
#define tell(a) cout << a << " "
#define el cout << endl
#define rev(v) reverse(v.begin(), v.end())
#define rep(i, a, n) for (int i = a; i < n; i++)
#define ld long double
#define mem(a, b) memset(a, b, sizeof(a))
#define M 1000000
#define md 998244353

ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
/*inline ll add(ll a,ll b){return (a+b)%md;}
inline ll mul(ll a,ll b){return (a*b)%md;}
ll f[M],iv[M];
ll C(ll n,ll r){return mul(f[n],mul(iv[r],iv[n-r]));}
void prep_fac(){
    f[0]=1;
    rep(i,1,M)f[i]=mul(i,f[i-1]);
    iv[M-1]=power(f[M-1],md-2,md);
    for(ll i=M-2;i>=0;--i)iv[i]=mul(i+1,iv[i+1]);
}*/

int main() {
  IOS;
  int n, k;
  cin >> n >> k;
  vector<pii> v;
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    v.pb({x, y});
  }

  ll dp[n + 1], pre[n + 2];
  mem(dp, 0);
  dp[1] = 1;
  pre[2] = 1;
  pre[1] = 0;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      int u = i - v[j].ff;
      int l = i - v[j].ss;
      if (u < 1)
        continue;
      dp[i] = (dp[i] + pre[u + 1] - (l >= 1 ? pre[l] : 0) + md) % md;
    }
    pre[i + 1] = (pre[i] + dp[i]) % md;
  }
  cout << dp[n] % md;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1e18
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define pb push_back
#define ff first
#define ss second
#define vll vector<ll>
#define pii pair<ll, ll>
#define sz(v) (v).size()
#define srt(v) sort(v.begin(), v.end())
#define ull unsigned long long
#define tell(a) cout << a << " "
#define el cout << endl
#define rev(v) reverse(v.begin(), v.end())
#define rep(i, a, n) for (int i = a; i < n; i++)
#define ld long double
#define mem(a, b) memset(a, b, sizeof(a))
#define M 1000000
#define md 998244353

ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
/*inline ll add(ll a,ll b){return (a+b)%md;}
inline ll mul(ll a,ll b){return (a*b)%md;}
ll f[M],iv[M];
ll C(ll n,ll r){return mul(f[n],mul(iv[r],iv[n-r]));}
void prep_fac(){
    f[0]=1;
    rep(i,1,M)f[i]=mul(i,f[i-1]);
    iv[M-1]=power(f[M-1],md-2,md);
    for(ll i=M-2;i>=0;--i)iv[i]=mul(i+1,iv[i+1]);
}*/

int main() {
  IOS;
  int n, k;
  cin >> n >> k;
  set<int> s;
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    for (int j = x; j <= y; j++)
      s.insert(j);
  }
  vll v;
  for (auto it = s.begin(); it != s.end(); it++) {
    v.pb(*it);
  }
  ll dp[n + 1];
  mem(dp, 0);
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    int pos = lower_bound(v.begin(), v.end(), i) - v.begin();
    for (int j = 0; j < pos; j++) {
      if (i - v[j] >= 1) {
        dp[i] += dp[i - v[j]];
        dp[i] %= md;
      }
    }
  }
  cout << dp[n] % md;
}