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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

const int MAX_L = 20; // ~ Log N
const long long MOD = 1e9 + 7;
const long long INF = 1e9 + 7;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vi> vvi;

#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, k, n) for (int i = k; i <= n; ++i)
#define REPR(i, k, n) for (int i = k; i >= n; --i)
#define vin(v, N)                                                              \\
  vi v;                                                                        \\
  rep(i, N) {                                                                  \\
    int x;                                                                     \\
    cin >> x;                                                                  \\
    v.push_back(x);                                                            \\
  }
#define vout(v, N)                                                             \\
  rep(i, N) { cout << v[i] << " "; }                                           \\
  cout << endl;

#define LSOne(S) (S & (-S))
#define isBitSet(S, i) ((S >> i) & 1)

const int MAX_N = 2e5 + 5;

ll dp[MAX_N];
ll pre[MAX_N];
void solve() {
  ll mod = 998244353;
  memset(dp, 0, sizeof(dp));
  memset(pre, 0, sizeof(pre));
  int N, K;
  cin >> N >> K;
  vii v;
  rep(i, K) {
    int l, r;
    cin >> l >> r;
    v.push_back({l, r});
  }
  dp[0] = 1;
  pre[1] = dp[0];
  REP(i, 1, N - 1) {
    rep(j, K) {
      dp[i] = (dp[i] + pre[max(0, i - v[j].first + 1)] -
               pre[max(0, i - v[j].second)] + mod) %
              mod;
    }
    pre[i + 1] = (pre[i] + dp[i]) % mod;
  }
  //    vout(dp, N)
  //    vout(pre, N+1)
  cout << dp[N - 1];
}

// #define LOCAL 1

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

const int MAX_L = 20; // ~ Log N
const long long MOD = 1e9 + 7;
const long long INF = 1e9 + 7;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vi> vvi;

#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, k, n) for (int i = k; i <= n; ++i)
#define REPR(i, k, n) for (int i = k; i >= n; --i)
#define vin(v, N)                                                              \\
  vi v;                                                                        \\
  rep(i, N) {                                                                  \\
    int x;                                                                     \\
    cin >> x;                                                                  \\
    v.push_back(x);                                                            \\
  }

#define LSOne(S) (S & (-S))
#define isBitSet(S, i) ((S >> i) & 1)

const int MAX_N = 2e5 + 5;

long dp[MAX_N];
void solve() {
  memset(dp, 0, sizeof(dp));
  int N, K;
  cin >> N >> K;
  vi v;
  rep(i, K) {
    int l, r;
    cin >> l >> r;
    REP(j, l, r) { v.push_back(j); }
  }
  dp[0] = 1;
  REP(i, 1, N - 1) {
    for (auto move : v) {
      if (i - move >= 0) {
        dp[i] = (dp[i] + dp[i - move]) % 998244353;
      }
    }
  }
  cout << dp[N - 1];
}

// #define LOCAL 1

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  solve();

  return 0;
}