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

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <string>
#define ll long long
#define F first
#define S second
#define pb push_back
#define ld long double
#define pll pair<long long, long long>
#define pld pair<long double, long double>
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
using namespace std;
ll mod = 998244353;
ll INF = 1e18;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll fastexp(ll x, ll n) {
  ll result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result *= x;
    x *= x;
    n = n / 2;
  }
  return result;
}

ll modularExp(ll x, ll n, ll mod) {
  ll result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = (result * x) % mod;
    x = ((x % mod) * (x % mod)) % mod;
    n = n / 2;
  }
  return (result) % mod;
}
bool isPrime(ll n) {
  for (ll i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
bool mycomp(pll &a, pll &b) {
  if (a.F != b.F)
    return a.F < b.F;
  else
    return a.S < b.S;
}
vector<ll> par(2005), sz(2005, 1);

ll froot(ll x) {
  if (x == par[x])
    return x;
  return par[x] = froot(par[x]);
}
void solve() {
  ll n, k;
  cin >> n >> k;
  vector<pll> v(k);
  vector<ll> dp(n + 1), pre(n + 1);
  ll i, j;
  for (i = 0; i < k; i++)
    cin >> v[i].F >> v[i].S;
  dp[1] = 1;
  pre[1] = 1;
  for (i = 2; i <= n; i++) {
    for (j = 0; j < k; j++) {
      ll l = max(0LL, i - v[j].F);
      ll r = max(0LL, i - v[j].S - 1);
      // cout<<l<<" "<<r<<"!\\n";
      dp[i] = (dp[i] + pre[l] % mod - pre[r] % mod + mod) % mod;
    }
    pre[i] = (pre[i - 1] + dp[i]) % mod;
  }
  cout << dp[n];
}

int main() {
  ios;
  ll t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <string>
#define ll long long
#define F first
#define S second
#define pb push_back
#define ld long double
#define pll pair<long long, long long>
#define pld pair<long double, long double>
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
using namespace std;
ll mod = 998244353;
ll INF = 1e18;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll fastexp(ll x, ll n) {
  ll result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result *= x;
    x *= x;
    n = n / 2;
  }
  return result;
}

ll modularExp(ll x, ll n, ll mod) {
  ll result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = (result * x) % mod;
    x = ((x % mod) * (x % mod)) % mod;
    n = n / 2;
  }
  return (result) % mod;
}
bool isPrime(ll n) {
  for (ll i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
bool mycomp(pll &a, pll &b) {
  if (a.F != b.F)
    return a.F < b.F;
  else
    return a.S < b.S;
}
vector<ll> par(2005), sz(2005, 1);

ll froot(ll x) {
  if (x == par[x])
    return x;
  return par[x] = froot(par[x]);
}
void solve() {
  ll n, k;
  cin >> n >> k;
  vector<pll> v(k);
  vector<ll> dp(n + 1);
  ll i, j;
  for (i = 0; i < k; i++)
    cin >> v[i].F >> v[i].S;
  sort(v.begin(), v.end());
  vector<ll> st;
  for (i = 0; i < k; i++) {
    for (j = v[i].F; j <= v[i].S; j++)
      st.pb(j);
  }

  ll sz = st.size();
  dp[1] = 1;
  for (i = 1; i <= n; i++) {
    ll mdiff = (n - i);
    ll idx = upper_bound(st.begin(), st.end(), mdiff) - st.begin();
    idx--;
    for (j = 0; j <= min(sz - 1, idx); j++)
      dp[i + st[j]] = (dp[i + st[j]] % mod + dp[i] % mod) % mod;
  }
  cout << dp[n];
}

int main() {
  ios;
  ll t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
}