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

#include <bits/stdc++.h>
#define ll long long int
#define mod 998244353
#define pi 3.14159265358979323846264338327950288419716939937510
#define setbit(x) __builtin_popcountll(x)
#define db long double
#define pp(x) pair<x, x>
#define ff first
#define ss second
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define tc                                                                     \\
  ll tc;                                                                       \\
  cin >> tc;                                                                   \\
  for (ll number = 1; number <= tc; number++)
#define vv(x) vector<x>
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define endl "\\n"
#define pq priority_queue
#define dbg(x)                                                                 \\
  { cout << #x << ": " << (x) << endl; }
#define dbg2(x, y)                                                             \\
  { cout << #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define bp                                                                     \\
  ll kkk;                                                                      \\
  cin >> kkk;
#define dbgArr(a, n)                                                           \\
  cout << "Array " << #a << endl;                                              \\
  for (ll i = 0; i < n; i++)                                                   \\
    cout << a[i] << " ";                                                       \\
  cout << endl;
#define maxE(a, n) *max_element(a, a + n);
#define minE(a, n) *min_element(a, a + n);
#define forn(i, s, n) for (ll i = s; i < n; i++)
#define fornr(i, e, s) for (ll i = e - 1; i >= s; i--)
#define arr1(a, n)                                                             \\
  for (ll i = 1; i <= n; i++) {                                                \\
    cin >> a[i];                                                               \\
  }
#define file                                                                   \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout)
#define print cout << "Case #" << number << ": "
#define all(x) x.begin(), x.end()
ll power(ll x, ll b) {
  ll p = 1;
  while (b > 0) {
    if (b & 1) {
      p = p * x;
      p %= mod;
    }
    b >>= 1;
    x *= x;
    x %= mod;
  }
  return p % mod;
}
using namespace std;
#define INF 1e15

struct ppp {
  ll a, b, c;
};

struct lex_compare {
  bool operator()(ppp p1, ppp p2) {
    if (p1.a == p2.a) {
      if (p1.b == p2.b)
        return p1.c < p2.c;
      else
        return p1.b < p2.b;
    } else {
      return p1.a < p2.a;
    }
  }
};
ll dp[200004 + 1];

ll dfs(ll i, set<ll> &s, ll n) {
  if (i == n) {
    return 1;
  }
  if (dp[i] != -1)
    return dp[i];
  ll ans = 0;
  for (auto j : s) {
    if (i + j <= n) {
      ans += dfs(i + j, s, n);
      ans %= mod;
    } else {
      break;
    }
  }
  return dp[i] = ans;
}

int main() {
  FIO;
  ll n, k;
  cin >> n >> k;
  set<ll> s;
  pp(ll) arr[k];
  forn(i, 0, k) { cin >> arr[i].ff >> arr[i].ss; }
  sort(arr, arr + k);
  ll ans = 0;

  memset(dp, 0, sizeof(dp));
  dp[n] = 1;
  ll presum[n + 5] = {0};
  presum[n] = 1;
  for (int i = n - 1; i > 0; i--) {
    ll ind = i;
    ll temp = 0;
    for (int j = 0; j < k; j++) {
      if (ind + arr[j].ff <= n) {
        temp += (presum[ind + arr[j].ff] -
                 presum[min(n + 1, ind + arr[j].ss + 1)] + mod) %
                mod;
      } else {
        break;
      }
    }
    // dbg2(i,temp);
    presum[i] = (temp + presum[i + 1]) % mod;
  }
  // dbgArr(presum, n+3);
  cout << (presum[1] - presum[2] + mod) % mod << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define mod 998244353
#define pi 3.14159265358979323846264338327950288419716939937510
#define setbit(x) __builtin_popcountll(x)
#define db long double
#define pp(x) pair<x, x>
#define ff first
#define ss second
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define tc                                                                     \\
  ll tc;                                                                       \\
  cin >> tc;                                                                   \\
  for (ll number = 1; number <= tc; number++)
#define vv(x) vector<x>
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define endl "\\n"
#define pq priority_queue
#define dbg(x)                                                                 \\
  { cout << #x << ": " << (x) << endl; }
#define dbg2(x, y)                                                             \\
  { cout << #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define bp                                                                     \\
  ll kkk;                                                                      \\
  cin >> kkk;
#define dbgArr(a, n)                                                           \\
  cout << "Array " << #a << endl;                                              \\
  for (ll i = 0; i < n; i++)                                                   \\
    cout << a[i] << " ";                                                       \\
  cout << endl;
#define maxE(a, n) *max_element(a, a + n);
#define minE(a, n) *min_element(a, a + n);
#define forn(i, s, n) for (ll i = s; i < n; i++)
#define fornr(i, e, s) for (ll i = e - 1; i >= s; i--)
#define arr1(a, n)                                                             \\
  for (ll i = 1; i <= n; i++) {                                                \\
    cin >> a[i];                                                               \\
  }
#define file                                                                   \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout)
#define print cout << "Case #" << number << ": "
#define all(x) x.begin(), x.end()
ll power(ll x, ll b) {
  ll p = 1;
  while (b > 0) {
    if (b & 1) {
      p = p * x;
      p %= mod;
    }
    b >>= 1;
    x *= x;
    x %= mod;
  }
  return p % mod;
}
using namespace std;
#define INF 1e15

struct ppp {
  ll a, b, c;
};

struct lex_compare {
  bool operator()(ppp p1, ppp p2) {
    if (p1.a == p2.a) {
      if (p1.b == p2.b)
        return p1.c < p2.c;
      else
        return p1.b < p2.b;
    } else {
      return p1.a < p2.a;
    }
  }
};
ll dp[200004 + 1];

ll dfs(ll i, set<ll> &s, ll n) {
  if (i == n) {
    return 1;
  }
  if (dp[i] != -1)
    return dp[i];
  ll ans = 0;
  for (auto j : s) {
    if (i + j <= n) {
      ans += dfs(i + j, s, n);
      ans %= mod;
    } else {
      break;
    }
  }
  return dp[i] = ans;
}

int main() {
  FIO;
  ll n, k;
  cin >> n >> k;
  set<ll> s;
  forn(i, 0, k) {
    ll a, b;
    cin >> a >> b;
    forn(j, a, b + 1) s.insert(j);
  }
  ll ans = 0;

  memset(dp, -1, sizeof(dp));
  ans = dfs(1, s, n);
  cout << ans << endl;
  return 0;
}