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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
ll mod = 998244353;

void solve() {
  ll n, k;
  cin >> n >> k;
  vector<vector<ll>> v;
  vector<ll> dp(n + 1, 0);
  for (ll i = 0; i < k; i++) {
    ll x, y;
    cin >> x >> y;
    vector<ll> v1(2, 0);
    v1[0] = x;
    v1[1] = y;
    // for(ll j = x; j <= y; j++) v.push_back(j);
    v.push_back(v1);
  }
  sort(v.begin(), v.end());
  vector<ll> cdp(n + 1, 0);
  cdp[1] = 1;
  // for(ll i = 0; i < v.size(); i++) cout << v[i] << " ";
  // cout << endl;
  dp[1] = 1;
  for (ll i = 2; i < n + 1; i++) {
    // for(ll j = 0; j < v.size(); j++) {
    //     ll step = v[j];
    //     if(step > i-1) break;
    //     dp[i] = (dp[i] + dp[i-step])%mod;
    // }

    for (ll j = 0; j < k; j++) {
      ll x = v[j][0];
      ll y = v[j][1];
      if (x > i - 1)
        continue;
      if (y <= i - 1) {
        dp[i] = (dp[i] + (cdp[i - x] - cdp[i - y - 1] + mod)) % mod;
      } else {
        ll newx = x;
        ll newy = i - 1;
        dp[i] =
            (dp[i] + ((cdp[i - newx] - cdp[i - newy - 1] + mod) % mod)) % mod;
      }
    }

    cdp[i] = (cdp[i - 1] + dp[i]) % mod;
  }
  // for(ll i = 0; i <= n; i++) cout << dp[i] << " ";
  // cout << endl;
  // for(ll i = 0; i <= n; i++) cout << cdp[i] << " ";
  // cout << endl;
  cout << dp[n] << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll T = 1;

  while (T--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
int mod = 998244353;

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> v;
  vector<int> dp(n + 1, 0);
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    for (int j = x; j <= y; j++)
      v.push_back(j);
  }
  sort(v.begin(), v.end());
  // for(int i = 0; i < v.size(); i++) cout << v[i] << " ";
  // cout << endl;
  dp[1] = 1;
  for (int i = 2; i < n + 1; i++) {
    for (int j = 0; j < v.size(); j++) {
      int step = v[j];
      if (step > i - 1)
        break;
      dp[i] = (dp[i] + dp[i - step]) % mod;
    }
  }
  // for(int i = 0; i <= n; i++) cout << dp[i] << " ";
  // cout << endl;
  cout << dp[n] << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll T = 1;

  while (T--) {
    solve();
  }
  return 0;
}