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

#include <bits/stdc++.h>
using namespace std;

#define int long long int
#define ff first
#define ss second
#define pb push_back
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

const int mod = 1e9 + 7;
const int cmod = 998244353;
const int N = 3e5 + 5;
const int inf = 1e18 + 2;

void solve(int Case) {
  int n, k;
  cin >> n >> k;
  // set<int> s ;
  vector<pair<int, int>> seg;
  while (k--) {
    int a, b;
    cin >> a >> b;
    seg.push_back({a, b});
  }

  // for(auto X : seg) cout << X.ff << ' ' << X.ss << '\\n';
  int ad[n + 2], dp[n + 2];
  memset(ad, 0, sizeof ad);
  memset(dp, 0, sizeof dp);
  dp[1] = 1;
  for (int i = 1; i <= n; i++) {
    ad[i] = (ad[i] + ad[i - 1] + cmod) % cmod;
    dp[i] = (dp[i] + ad[i] + cmod) % cmod;
    for (int j = 0; j < seg.size(); j++) {
      if (i + seg[j].ff > n)
        continue;
      ad[i + seg[j].ff] = (ad[i + seg[j].ff] + dp[i] + cmod) % cmod;
      int temp = min(i + seg[j].ss, n);
      ad[temp + 1] = (ad[temp + 1] - dp[i] + cmod) % cmod;
    }
  }

  cout << dp[n] << '\\n';
}
/*
 */

int32_t main() {
  fastio int testcase = 1;
  // cin >> testcase;
  int Case = 1;
  while (testcase--) {
    solve(Case);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long int
#define ff first
#define ss second
#define pb push_back
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

const int mod = 1e9 + 7;
const int cmod = 998244353;
const int N = 3e5 + 5;
const int inf = 1e18 + 2;

void solve(int Case) {
  int n, k;
  cin >> n >> k;
  set<int> s;
  while (k--) {
    int a, b;
    cin >> a >> b;
    for (int i = a; i <= b; i++)
      s.insert(i);
  }
  vector<pair<int, int>> seg;
  int prev = -1, l = 0, r = 0;
  for (auto x : s) {
    if (prev == -1) {
      l = r = x;
      prev = x;
      continue;
    }
    if (x == prev + 1)
      r++;
    else {
      seg.push_back({l, r});
      prev = x;
      l = r = x;
    }
  }
  if (l != 0)
    seg.push_back({l, r});
  // for(auto X : seg) cout << X.ff << ' ' << X.ss << '\\n';
  int ad[n + 10], dp[n + 10];
  memset(ad, 0, sizeof ad);
  memset(dp, 0, sizeof dp);
  dp[1] = 1;
  for (int i = 1; i <= n; i++) {
    ad[i] = (ad[i] + ad[i - 1]) % cmod;
    dp[i] = (dp[i] + ad[i] + cmod) % cmod;
    for (int j = 0; j < seg.size(); j++) {
      if (i + seg[j].ff > n)
        continue;
      ad[i + seg[j].ff] = (ad[i + seg[j].ff] + dp[i]) % cmod;
      int temp = min(i + seg[j].ss, n);
      ad[temp + 1] = (ad[temp + 1] - dp[i] + cmod) % cmod;
    }
  }

  cout << dp[n] << '\\n';
}
/*
 */

int32_t main() {
  fastio int testcase = 1;
  // cin >> testcase;
  int Case = 1;
  while (testcase--) {
    solve(Case);
  }
  return 0;
}