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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define ll long long
#define FOR(i, n) for (int i = 0; i < n; i++)
#define sz(x) (int)(x.size())
#define all(x) (x).begin(), (x).end()
template <typename tp> void read(tp &x) { cin >> x; }
template <typename tp, typename... Args> void read(tp &x, Args &...args) {
  read(x);
  read(args...);
}
const int mxN = (int)1e5, INF = (int)1e9, MOD = 998244353;

ll rnge(ll l, ll r, vector<ll> &sum) { return sum[r] - sum[l - 1]; }
bool comp(pair<ll, ll> &a, pair<ll, ll> &b) {
  if (a.first == b.first)
    return a.second < b.second;
  return a.first < b.first;
}
void solve() {
  ll n, k;
  read(n, k);
  vector<pair<ll, ll>> segm(k);
  FOR(i, k) read(segm[i].first, segm[i].second);
  sort(all(segm), comp);
  vector<pair<ll, ll>> S;
  for (int i = 0; i < k; i++) {
    if (S.empty()) {
      S.push_back(segm[i]);
      continue;
    }
    pair<int, int> a = S.back(), b = segm[i];
    if (a.first == b.first)
      S.back() = b;
    else if (b.first <= a.second)
      S.back().second = b.second;
    else
      S.push_back(b);
  }
  vector<ll> dp(n + 1), sum(n + 1);
  dp[1] = 1;
  sum[1] = 1;
  for (int i = 2; i <= n; i++) {
    dp[i] = 0;
    FOR(j, sz(S)) {
      if (i - S[j].first < 1LL)
        break;
      ll temp;
      if (i - S[j].second > 0)
        temp = (sum[i - S[j].first] - sum[i - S[j].second - 1] + MOD) % MOD;
      else
        temp = sum[i - S[j].first];
      dp[i] = (dp[i] % MOD + temp % MOD) % MOD;
    }
    sum[i] = (dp[i] % MOD + sum[i - 1] % MOD) % MOD;
  }
  cout << dp[n] << '\\n';
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int test = 1;
  // read(test);
  while (test--)
    solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define ll long long
#define FOR(i, n) for (int i = 0; i < n; i++)
#define sz(x) (int)(x.size())
#define all(x) (x).begin(), (x).end()
template <typename tp> void read(tp &x) { cin >> x; }
template <typename tp, typename... Args> void read(tp &x, Args &...args) {
  read(x);
  read(args...);
}
const int mxN = (int)1e5, INF = (int)1e9, MOD = 998244353;

bool comp(pair<int, int> &a, pair<int, int> &b) {
  if (a.first == b.first)
    return a.second < b.second;
  return a.first < b.first;
}
void scan(int &number) {
  bool negative = false;
  register int c;
  number = 0;
  c = getchar();
  if (c == '-') {
    negative = true;
    c = getchar_unlocked();
  }
  for (; (c > 47 && c < 58); c = getchar())
    number = number * 10 + c - 48;
  if (negative)
    number *= -1;
}
void solve() {
  int n, k;
  scan(n);
  scan(k);
  pair<int, int> segm[k];
  FOR(i, k) {
    scan(segm[i].first);
    scan(segm[i].second);
  }
  sort(segm, segm + k, comp);
  vector<pair<int, int>> S;
  for (int i = 0; i < k; i++) {
    if (S.empty()) {
      S.push_back(segm[i]);
      continue;
    }
    pair<int, int> a = S.back(), b = segm[i];
    if (a.first == b.first)
      S.back() = b;
    else if (b.first <= a.second)
      S.back().second = b.second;
    else
      S.push_back(b);
  }
  int dp[n + 1];
  dp[0] = 0;
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    dp[i] = 0;
    FOR(j, sz(S)) {
      for (int l = S[j].first; l <= S[j].second; l++) {
        if (i - l <= 0)
          break;
        dp[i] = (dp[i] + dp[i - l]) % MOD;
      }
    }
  }
  cout << dp[n];
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}