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

// 2*10^9
// 9*10^18
//  __int128_t
#include <bits/stdc++.h>
using namespace std;

#define ull unsigned long long int
#define ll long long int

const ll M = 998244353;

// to handle when remainder is -neg we add + M
// but when rem is +pos that time also we add so use %M
// so it will work for both
ll mod(ll n) { return (n % M + M) % M; }
ll modAdd(ll a, ll b) { return mod(mod(a) + mod(b)); }
ll modMul(ll a, ll b) { return mod(mod(a) * mod(b)); }
ll modMinus(ll a, ll b) { return mod(mod(a) - mod(b)); }
ll modpow(ll x, ll n) {
  if (n == 0)
    return 1 % M;
  if (n == 1)
    return x % M;
  ll u = modpow(x, n / 2);
  u = modMul(u, u);
  if (n % 2)
    u = modMul(u, x);
  return u;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
bool isPrime(ll n) {
  int end = sqrt(n);
  for (int i = 2; i <= end; i++)
    if (n % i == 0)
      return false;
  return true;
}

int factors(int n) {
  int count = 0;
  int end = sqrt(n) - 1;

  for (int i = 1; i <= end; i++) {
    if (n % i == 0)
      count += 2;
  }
  if (n % (end + 1) == 0) {
    if (n / (end + 1) != (end + 1))
      count += 1;
    count += 1;
  }

  return count;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin>>t;

  while (t--) {
    int n;
    cin >> n;

    vector<int> dp(n + 1);
    vector<int> suffix(n + 1);
    int k;
    cin >> k;

    vector<vector<int>> segment(k, vector<int>(2));
    for (int i = 0; i < k; i++)
      cin >> segment[i][0] >> segment[i][1];
    sort(segment.begin(), segment.end());

    dp[n] = 1;
    suffix[n] = 1;
    for (int i = n - 1; i >= 1; i--) {
      for (int j = 0; j < k; j++) {
        int start = i + segment[j][0];
        int end = i + segment[j][1];

        if (start <= n) {
          if (end < n)
            dp[i] = modAdd(dp[i], suffix[start] - suffix[end + 1]);
          else
            dp[i] = modAdd(dp[i], suffix[start]);
        }
      }
      suffix[i] = modAdd(dp[i], suffix[i + 1]);
    }
    cout << dp[1] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 2*10^9
// 9*10^18
//  __int128_t
#include <bits/stdc++.h>
using namespace std;

#define ull unsigned long long int
#define ll long long int

const ll M = 998244353;

// to handle when remainder is -neg we add + M
// but when rem is +pos that time also we add so use %M
// so it will work for both
ll mod(ll n) { return (n % M + M) % M; }
ll modAdd(ll a, ll b) { return mod(mod(a) + mod(b)); }
ll modMul(ll a, ll b) { return mod(mod(a) * mod(b)); }
ll modMinus(ll a, ll b) { return mod(mod(a) - mod(b)); }
ll modpow(ll x, ll n) {
  if (n == 0)
    return 1 % M;
  if (n == 1)
    return x % M;
  ll u = modpow(x, n / 2);
  u = modMul(u, u);
  if (n % 2)
    u = modMul(u, x);
  return u;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
bool isPrime(ll n) {
  int end = sqrt(n);
  for (int i = 2; i <= end; i++)
    if (n % i == 0)
      return false;
  return true;
}

int factors(int n) {
  int count = 0;
  int end = sqrt(n) - 1;

  for (int i = 1; i <= end; i++) {
    if (n % i == 0)
      count += 2;
  }
  if (n % (end + 1) == 0) {
    if (n / (end + 1) != (end + 1))
      count += 1;
    count += 1;
  }

  return count;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin>>t;

  while (t--) {
    int n;
    cin >> n;

    vector<int> dp(n + 1);
    int k;
    cin >> k;

    vector<vector<int>> segment(k, vector<int>(2));
    for (int i = 0; i < k; i++)
      cin >> segment[i][0] >> segment[i][1];
    sort(segment.begin(), segment.end());

    dp[n] = 1;
    for (int i = n - 1; i >= 1; i--) {
      for (int j = 0; j < k; j++) {
        int start = i + segment[j][0];
        int end = i + segment[j][1];
        for (int k = start; k <= n && k <= end; k++)
          dp[i] = modAdd(dp[i], dp[k]);

        if (end >= n)
          break;
      }
    }
    cout << dp[1] << endl;
  }
  return 0;
}