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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define AB_BHI_NI_DEGI                                                         \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define int long long
#define pb push_back
#define N 100009
#define inf 1e18
const double PI = 3.141592653589793238462643383279;
// int mod = 1e9 + 7;
int mod = 998244353;
#define P pair<int, int>
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define vi vector<int>
#define ld long double

#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

void solve() {
  int n, k;
  cin >> n >> k;
  int vis[n + 1] = {0};
  int l[k], r[k];
  int dp[n + 1] = {0};
  for (int i = 0; i < k; i++) {
    cin >> l[i] >> r[i];
    for (int j = l[i]; j <= r[i]; j++)
      vis[j] = 1;
  }
  int ans = 0;
  for (int i = 2; i <= n; i++) {
    dp[i] = dp[i - 1] - vis[i - 2] + vis[i - 1];
    for (int j = 0; j < k; j++) {
      int L = l[j];
      int R = r[j];
      if (i - R - 1 > 0)
        dp[i] = (dp[i] - dp[i - R - 1] + mod) % mod;
      if (i - L > 0)
        dp[i] = (dp[i] + dp[i - L]) % mod;
    }
  }
  cout << dp[n] << "\\n";
}
int32_t main() {
  AB_BHI_NI_DEGI
  int Test = 1;
  // cin >> Test;
  // init();
  while (Test--)
    solve();
  return 0;
}
/*

*/

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define AB_BHI_NI_DEGI                                                         \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define int long long
#define pb push_back
#define N 100009
#define inf 1e18
const double PI = 3.141592653589793238462643383279;
// int mod = 1e9 + 7;
int mod = 998244353;
#define P pair<int, int>
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define vi vector<int>
#define ld long double

#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

int n, k;
vector<int> v;
int dp[2 * N];
int fun(int cur) {
  if (cur > n)
    return 0;
  if (cur == n)
    return 1;
  if (dp[cur] != -1)
    return dp[cur];
  int ans = 0;
  for (int i : v) {
    if (cur + i > n)
      break;
    ans += fun(cur + i) % mod;
    ans %= mod;
  }
  return dp[cur] = ans;
}

void solve() {
  cin >> n >> k;
  set<int> s;
  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++)
      s.insert(j);
  }
  for (int i : s)
    v.pb(i);
  sort(all(v));
  memset(dp, -1, sizeof(dp));
  cout << fun(1) << "\\n";
}
int32_t main() {
  AB_BHI_NI_DEGI
  int Test = 1;
  // cin >> Test;
  // init();
  while (Test--)
    solve();
  return 0;
}
/*

*/