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

#include <algorithm>
#include <cassert>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
const int mod = 998244353;

#define read_int(X)                                                            \\
  ll X;                                                                        \\
  scanf("%lld\\n", &X);

#define read_two_int(X, Y)                                                     \\
  ll X, Y;                                                                     \\
  scanf("%lld %lld\\n", &X, &Y);

#define read_three_int(X, Y, Z)                                                \\
  ll X, Y, Z;                                                                  \\
  scanf("%lld %lld %lld\\n", &X, &Y, &Z);

#define read_four_int(X, Y, Z, W)                                              \\
  ll X, Y, Z, W;                                                               \\
  scanf("%lld %lld %lld %lld\\n", &X, &Y, &Z, &W);

int main() {
  read_two_int(N, K);
  vector<pair<ll, ll>> lr(K);
  for (ll i = 0; i < K; i++) {
    scanf("%lld", &(lr[i].first));
    scanf("%lld", &(lr[i].second));
  }

  vector<ll> dp(N, 0);
  dp[0] = 1;
  for (ll i = 1; i < N; i++) {
    ll sum = 0;
    for (ll k = 0; k < K; k++) {
      ll diff = 0;
      if (i - lr[k].first >= 0)
        diff = dp[i - lr[k].first];
      if (i - lr[k].second - 1 >= 0)
        diff -= dp[i - lr[k].second - 1];
      sum += diff;
      sum %= mod;
    }

    dp[i] = sum + dp[i - 1];
  }
  ll ans = dp[N - 1] - dp[N - 2];

  printf("%lld\\n", ans);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
const int mod = 998244353;

#define read_int(X)                                                            \\
  ll X;                                                                        \\
  scanf("%lld\\n", &X);

#define read_two_int(X, Y)                                                     \\
  ll X, Y;                                                                     \\
  scanf("%lld %lld\\n", &X, &Y);

#define read_three_int(X, Y, Z)                                                \\
  ll X, Y, Z;                                                                  \\
  scanf("%lld %lld %lld\\n", &X, &Y, &Z);

#define read_four_int(X, Y, Z, W)                                              \\
  ll X, Y, Z, W;                                                               \\
  scanf("%lld %lld %lld %lld\\n", &X, &Y, &Z, &W);

vector<ll> dp(2 * 10e5, -1);
ll calc(ll n, vector<pair<ll, ll>> *lr) {
  if (dp[n] != -1)
    return dp[n];

  ll sum = 0;
  for (ll i = 0; i < (*lr).size(); i++) {
    for (ll j = (*lr)[i].first; j <= (*lr)[i].second; j++) {
      if (n - j > 0) {
        sum += calc(n - j, lr) % mod;
        continue;
      }

      if (n == j) {
        sum += 1;
      }

      break;
    }
  }

  dp[n] = sum % mod;
  return dp[n];
}

int main() {
  read_two_int(N, K);
  vector<pair<ll, ll>> lr(K);
  for (ll i = 0; i < K; i++) {
    scanf("%lld", &(lr[i].first));
    scanf("%lld", &(lr[i].second));
  }

  // ll ans = calc(N - 1, &lr);

  vector<ll> dp(N, 0);
  dp[0] = 1;
  for (ll i = 1; i < N; i++) {
    ll sum = 0;
    for (ll k = 0; k < K; k++)
      for (ll j = lr[k].first; j <= lr[k].second; j++) {
        if (i - j >= 0) {
          sum += dp[i - j];
          sum %= mod;
        } else {
          break;
        }
      }
    dp[i] = sum;
  }
  ll ans = dp[N - 1];

  printf("%lld\\n", ans);
}