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

// is there any case of -1 or any special corner cases ?
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int, int>
#define all(v) v.begin(), v.end()
#define zeros(arr) memset(arr, 0, sizeof(arr));
#define precise(num)                                                           \\
  cout << fixed;                                                               \\
  cout << std::setprecision(num)
#define nl cout << "\\n";
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
const int maxx = 3 * 1e5, limit = 9 * 1e18, MOD = 998244353;
#define boost                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n';
int32_t main() {
  boost;
  int n, k;
  cin >> n >> k;
  map<int, int> mark;
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    for (int i = a; i <= b; i++) {
      mark[i] = 1;
    }
  }
  int l, r;
  bool flag = false;
  vector<pair<int, int>> range;
  for (int i = 1; i <= 1e6; i++) {

    if (mark[i] == 1) {
      if (!flag) {
        l = i;
        flag = true;
      }
    } else {
      if (flag) {
        r = i - 1;
        flag = false;
        range.pb(mp(l, r));
      }
    }
  }

  // for(int i=0 ; i< range.size() ; i++ ){
  //     cout<<range[i].fi<<" "<<range[i].se<<endl;
  // }

  int dp[n + 2];
  zeros(dp);
  dp[1] = 1;
  dp[0] = 0;
  int pre[n + 2];
  zeros(pre);

  pre[0] = 0;
  pre[1] = 1;

  for (int i = 2; i <= n; i++) {

    for (int j = 0; j < range.size(); j++) {

      int l = range[j].fi;
      int r = range[j].se;
      // cout<<l<<

      if (l >= i)
        continue;

      l = i - l;
      r = i - r;

      if (r < 1)
        r = 1;

      // cout<<l<<" "<<r<<endl;
      int minl = min(l, r);
      int maxr = max(l, r);

      dp[i] = (dp[i] + (pre[maxr] - pre[minl - 1] + 2 * MOD) % MOD) % MOD;
    }

    pre[i] = (pre[i - 1] + dp[i]) % MOD;
  }

  cout << dp[n] << endl;
}

Error: TLE

Fixed submission:

// is there any case of -1 or any special corner cases ?
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int, int>
#define all(v) v.begin(), v.end()
#define zeros(arr) memset(arr, 0, sizeof(arr));
#define precise(num)                                                           \\
  cout << fixed;                                                               \\
  cout << std::setprecision(num)
#define nl cout << "\\n";
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
const int maxx = 3 * 1e5, limit = 9 * 1e18, MOD = 998244353;
#define boost                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n';
int32_t main() {
  boost;
  int n, k;
  cin >> n >> k;
  set<int> s;
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    for (int i = a; i <= b; i++) {
      s.insert(i);
    }
  }
  int len = s.size();
  int itr = 0;
  int arr[len + 1];
  for (auto x : s) {
    arr[itr++] = x;
  }
  int dp[n + 2];
  zeros(dp);
  dp[1] = 1;
  dp[0] = 0;
  for (int i = 2; i <= n; i++) {

    for (int j = 0; j < len; j++) {
      int x = arr[j];
      if (x > i)
        break;

      dp[i] = (dp[i - x] + dp[i]) % MOD;
    }
  }
  cout << dp[n] << endl;
}