alexjercan/bugnet huggingface dataset:
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

using ll = long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr ll MOD = 998244353;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, k, l, r;
  cin >> n >> k;

  vector<Pii> lr;
  for (int i = 0; i < k; ++i) {
    cin >> l >> r;
    lr.emplace_back(l, r);
  }

  vector<ll> dp(n + 1, 0);
  dp[1] = 1LL;
  for (int i = 2; i <= n; ++i) {
    dp[i - 1] += dp[i - 2];
    dp[i - 1] %= MOD;
    for (int j = 0; j < k; ++j) {
      if (i - lr[j].first < 1)
        continue;
      ll diff =
          (dp[i - lr[j].first] - dp[max(0, i - lr[j].second - 1)] + MOD) % MOD;
      dp[i] += diff;
      dp[i] %= MOD;
    }
  }

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

using ll = long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr ll MOD = 998244353;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, k, l, r;
  cin >> n >> k;

  vector<int> v;
  for (int i = 0; i < k; ++i) {
    cin >> l >> r;
    for (int j = l; j <= r; ++j) {
      v.push_back(j);
    }
  }
  sort(v.begin(), v.end());
  int m = v.size();

  vector<ll> dp(n + 1, 0);
  dp[1] = 1LL;
  for (int i = 2; i <= n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (i - v[j] <= 0)
        continue;
      dp[i] += dp[i - v[j]];
      dp[i] %= MOD;
    }
  }

  cout << dp[n] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int mod = 998244353;
const int maxn = 200000 + 5;
ll f[maxn], a[maxn], b[maxn];
ll c[20];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);

  for (int i = 0; i < m; ++i)
    scanf("%d%d", &a[i], &b[i]);

  f[1] = 1;
  for (int i = 2; i <= n; ++i)
    for (int j = 0; j < m; ++j) {
      c[j] += (i - a[j] >= 1 ? f[i - a[j]] : 0) -
              (i - b[j] - 1 >= 1 ? f[i - b[j] - 1] : 0);
      f[i] = (f[i] + c[j]) % mod;
    }
  cout << f[n];
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int mod = 998244353;
int A[200005];
int f[200005];

int main() {
  int N, K;
  scanf("%d%d", &N, &K);

  set<int> s;
  set<int>::iterator it;
  int flag = 1;
  for (int i = 1; i <= K; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    for (int j = x; j <= y; ++j)
      s.insert(j);
  }
  f[1] = 1;
  for (int i = 1; i <= N; ++i) {
    for (it = s.begin(); it != s.end(); ++it) {
      if (i + *it <= N)
        f[i + *it] = (f[i + *it] + f[i]) % mod;
      else
        break;
    }
  }
  cout << f[N];
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {
  long long N;
  int K;
  cin >> N >> K;
  vector<int> L(K);
  vector<int> R(K);
  for (int i = 0; i < K; i++) {
    cin >> L[i] >> R[i];
  }
  vector<long long> dp(N + 1, 0);
  vector<long long> dpsum(N + 1, 0);
  dp[1] = 1;
  dpsum[1] = 1;

  for (int i = 2; i <= N; i++) {
    for (int j = 0; j < K; j++) {
      // if(i < R[j])continue;
      dp[i] += dpsum[max(i - L[j], 0)] - dpsum[max(i - R[j] - 1, 0)];
      dp[i] %= 998244353;
    }
    dpsum[i] = dpsum[i - 1] + dp[i];
    dpsum[i] %= 998244353;
    // cout << dp[i] << endl;
    // cout << dpsum[i] << endl;
  }

  if (dp[N] < 0)
    dp[N] += 998244353;
  cout << dp[N] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  long long N;
  int K, scount;
  scount = 0;
  cin >> N >> K;
  vector<int> L(K);
  vector<int> R(K);
  vector<int> table(N + 2, 0);
  vector<int> S = {};
  for (int i = 0; i < K; i++) {
    cin >> L.at(i) >> R.at(i);
    table.at(L.at(i))++;
    table.at(R.at(i) + 1)--;
  }
  for (int i = 1; i <= N; i++) {
    table.at(i) += table.at(i - 1);
    // cout << table.at(i) << endl;
    if (table.at(i) == 1)
      S.emplace_back(i);
  }
  sort(table.begin(), table.end());

  vector<long long> dp(N + 1, 0);
  dp.at(1) = 1;
  for (int i = 2; i <= N; i++) {
    for (int j = 0; j < S.size(); j++) {
      if (i - S.at(j) >= 0)
        dp.at(i) += dp.at(i - S.at(j));
      else
        continue;
    }
  }

  cout << dp.at(N) % 998244353 << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {
  const int64_t mod = 998244353;
  int N, K;
  cin >> N >> K;
  vector<pair<int, int>> vp(K);
  for (int i = 0; i < K; i++) {
    cin >> vp.at(i).first >> vp.at(i).second;
  }
  vector<int64_t> dp(N + 1, 0), sum(N + 1, 0);
  dp.at(1) = 1;
  sum.at(1) = sum.at(0) + dp.at(1);
  for (int i = 2; i <= N; i++) {
    for (int j = 0; j < K; j++) {
      if (i > vp.at(j).first) {
        dp.at(i) += (sum.at(i - vp.at(j).first) -
                     sum.at(max(0, i - vp.at(j).second - 1))) %
                    mod;
        dp.at(i) %= mod;
      }
    }
    sum.at(i) += (sum.at(i - 1) + dp.at(i)) % mod;
    sum.at(i) %= mod;
  }
  if (dp.at(N) < 0) {
    dp.at(N) += mod;
  }
  cout << dp.at(N) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  const int64_t mod = 998244353;
  int N, K;
  cin >> N >> K;
  set<int> s;
  int L, R;
  for (int i = 0; i < K; i++) {
    cin >> L >> R;
    for (int j = L; j <= R; j++) {
      s.insert(j);
    }
  }
  vector<int64_t> dp(N + 1, 0);
  dp.at(1) = 1;
  for (int i = 2; i <= N; i++) {
    for (auto x : s) {
      if (i > x) {
        dp.at(i) += dp.at(i - x);
        dp.at(i) %= mod;
      } else {
        break;
      }
    }
  }
  cout << dp.at(N) << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll INFL = 1e17;
const int MOD = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(k + 1), r(k + 1);
  for (int i = 1; i <= k; i++) {
    cin >> l[i] >> r[i];
  }

  vector<ll> dp(n + 1);
  vector<ll> dpsum(n + 1);
  dp[1] = 1;
  dpsum[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      int lim_r = i - l[j];
      int lim_l = i - r[j];
      if (lim_r < 0)
        continue;
      lim_l = max(lim_l, 1);
      dp[i] = (dp[i] + dpsum[lim_r] - dpsum[lim_l - 1] + MOD) % MOD;
    }
    dpsum[i] = (dp[i] + dpsum[i - 1]) % MOD;
  }
  cout << dp[n] << endl;
  return 0;
}

Error: WA

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll INFL = 1e17;
const int MOD = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(k + 1), r(k + 1);
  for (int i = 1; i <= k; i++) {
    cin >> l[i] >> r[i];
  }

  vector<ll> dp(n + 1);
  vector<ll> dpsum(n + 1);
  dp[1] = 1;
  dpsum[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      int lim_r = i - l[j];
      int lim_l = i - r[j];
      dp[i] = (dp[i] + dpsum[lim_r] - dpsum[lim_l - 1]) % MOD;
    }
    dpsum[i] = (dp[i] + dp[i - 1]) % MOD;
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, x, n) for (int i = (x); i < (n); ++i)
#define ll long long
#define P pair<ll, ll>
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()

// const ll mod = 1e9+7;
const ll INF = 1e18;
const double pi = acos(-1);
const ll mod = 998244353;

vector<ll> dp(440000);

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<P> a(k);
  rep(i, k) {
    ll l, r;
    cin >> l >> r;
    r++;
    a[i] = {l, r};
  }
  dp[1] = 1;
  dp[2] = -1;
  REP(i, 1, n + 1) {
    dp[i] += dp[i - 1];
    rep(j, k) {
      ll l, r;
      l = a[j].first;
      r = a[j].second;
      dp[i + l] += dp[i];
      dp[i + l] %= mod;
      dp[i + r] -= dp[i];
      dp[i + r] = (dp[i + r] + mod) % mod;
    }
  }
  cout << (dp[n] % mod + mod) % mod << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, x, n) for (int i = (x); i < (n); ++i)
#define ll long long
#define P pair<ll, ll>
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()

// const ll mod = 1e9+7;
const ll INF = 1e18;
const double pi = acos(-1);
const ll mod = 998244353;

vector<ll> dp(220000);

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<P> a(k);
  rep(i, k) {
    ll l, r;
    cin >> l >> r;
    r++;
    a[i] = {l, r};
  }
  dp[1] = 1;
  dp[2] = -1;
  REP(i, 1, n + 1) {
    dp[i] += dp[i - 1];
    rep(j, k) {
      ll l, r;
      l = a[j].first;
      r = a[j].second;
      dp[i + l] += dp[i];
      dp[i + l] %= mod;
      dp[i + r] -= dp[i];
      dp[i + r] = (dp[i + r] + mod) % mod;
    }
  }
  cout << (dp[n] % mod + mod) % mod << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int P = 998244353;

  int N, K;
  //    vector<int> s;
  vector<pair<int, int>> s;
  cin >> N >> K;

  for (int i = 0; i < K; ++i) {
    int l, r;
    cin >> l >> r;

    s.push_back(pair<int, int>(l, r));
  }

  vector<int> c(N + 1, 0); // c[i]  1 から i ますまでのパスの数

  for (size_t j = 0; j < s.size(); ++j) {
    for (int k = 0; k < K; ++k) {
      for (int ss = s[k].first; ss <= s[k].second; ++ss) {
        if (ss + 1 <= N) {
          c[ss + 1] = 1;
        } else {
          break;
        }
      }
    }
  }

  vector<int> C(N + 1, 0); // C[i] = c[0] + .. + c[i];

  for (int i = 1; i <= N; ++i) {
    for (int k = 0; k < K; ++k) {
      // c[i - s[k].first] + .. + c[i - s[k].second] を加算

      int add; //  = C[s[k].second] - C[s[k].first - 1];
      int upper = max(i - s[k].first, 0);
      int lower = max(i - s[k].second - 1, 0);
      if (C[upper] >= C[lower]) {
        add = (C[upper] - C[lower]) % P;
      } else {
        add = (P + C[upper] - C[lower]) % P;
      }

      c[i] = (c[i] + add) % P;
    }

    C[i] = (C[i - 1] + c[i]) % P;
  }

  cout << c[N] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int P = 998244353;

  int N, K;
  vector<int> s;
  cin >> N >> K;

  for (int i = 0; i < K; ++i) {
    int l, r;
    cin >> l >> r;

    for (int j = l; j <= r; ++j) {
      s.push_back(j);
    }
  }

  sort(s.begin(), s.end());

  vector<int> c(N + 1, 0); // c[i]  1 から i ますまでのパスの数

  for (size_t j = 0; j < s.size(); ++j) {
    if (s[j] + 1 <= N) {
      c[s[j] + 1] = 1;
    }
  }

  for (int i = 1; i <= N; ++i) {
    for (size_t j = 0; j < s.size(); ++j) {
      if ((i - s[j]) >= 0) {
        c[i] = (c[i] + c[i - s[j]]) % P;
      } else {
        break;
      }
    }
  }

  cout << c[N] << endl;

  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
constexpr int MOD = 998244353;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  for (int i = 0; i < k; i++)
    cin >> l[i] >> r[i];
  vector<long long> dp(2 * n + 100);
  dp[1] = 1;
  dp[2] = -1;
  for (int i = 1; i <= n; i++) {
    dp[i] = (dp[i] % MOD + MOD) % MOD;
    dp[i + 1] += dp[i];
    for (int j = 0; j < k; j++) {
      dp[i + l[j]] += dp[i];
      dp[i + r[j] + 1] -= dp[i];
    }
  }
  // dp[1] = 1;
  // for (int i = 1; i < n; i++) {
  // 	for (int j = 0; j < k; j++) {
  // 		for (int kk = l[j]; kk <= r[j]; kk++) {
  // 			(dp[i + kk] += dp[i]) %= MOD;
  // 		}
  // 	}
  // }
  cout << dp[n] << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
constexpr int MOD = 998244353;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  for (int i = 0; i < k; i++)
    cin >> l[i] >> r[i];
  vector<int> dp(2 * n + 100);
  // dp[1] = 1;
  // for (int i = 1; i < n; i++) {
  // 	for (int j = 0; j < k; j++) {
  // 		dp[i + l[j]] = dp[i];
  // 		dp[i + r[j] + 1] = -dp[i];
  // 	}
  // 	(dp[i] += dp[i - 1]) %= MOD;
  // }
  // for (int i = 1; i <= n; i++) {
  // }
  dp[1] = 1;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < k; j++) {
      for (int kk = l[j]; kk <= min(n, r[j]); kk++) {
        (dp[i + kk] += dp[i]) %= MOD;
      }
    }
  }
  cout << dp[n] << "\\n";
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = b - 1; (ll)(a) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
string YES[2] = {"NO", "YES"};
string yes[2] = {"No", "Yes"};

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> seg(k);
  ll md = 998244353;
  vector<ll> csum(n + 2, 0);
  set<pair<ll, ll>> key;
  rep(i, k) {
    ll a, b;
    cin >> a >> b;
    // a--, b--;
    seg[i] = {a, b};
    csum[a] += 1;
    csum[b + 1] -= 1;
    key.insert({a, b + 1});
  }

  // rep(i, n+2){
  //     csum[i+1] += csum[i];
  //     if(0<csum[i+1]) key.insert(i+1);
  // }
  // rep(i, n+1) cout << csum[i+1] << ' ';
  // cout << endl;
  vector<ll> dp(n + 2, 0);
  dp[1] = 1;
  FOR(i, 1, n) {
    // cout << i << endl;
    for (auto x : key) {
      ll a = min(i + x.first, n + 1), b = min(i + x.second, n + 1);
      dp[a] += dp[i];
      dp[b] -= dp[i];
      dp[a] = (dp[a] + md) % md;
      dp[b] = (dp[b] + md) % md;
      // rep(i, n+1) cout << dp[i+1] << ' ';
      // cout << endl;
    }
    if (i < n - 1)
      dp[i + 1] += dp[i];
    dp[i + 1] %= md;
  }
  // rep(i, n+1) cout << dp[i+1] << ' ';
  // cout << endl;
  // rep(i, n) dp[i+1] = (dp[i]+dp[i+1])%md;
  // rep(i, n+1) cout << dp[i+1] << ' ';
  // cout << endl;

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = b - 1; (ll)(a) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
string YES[2] = {"NO", "YES"};
string yes[2] = {"No", "Yes"};

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> seg(k);
  ll md = 998244353;
  vector<ll> csum(n + 2, 0);
  set<ll> key;
  rep(i, k) {
    ll a, b;
    cin >> a >> b;
    // a--, b--;
    seg[i] = {a, b};
    csum[a] += 1;
    csum[b + 1] -= 1;
  }

  rep(i, n + 2) {
    csum[i + 1] += csum[i];
    if (0 < csum[i + 1])
      key.insert(i + 1);
  }
  vector<ll> dp(n + 1, 0);
  dp[1] = 1;
  FOR(i, 1, n) {
    for (auto x : key) {
      if (n < i + x)
        break;
      dp[i + x] += dp[i];
      dp[i + x] %= md;
    }
  }
  // rep(i, n+1) cout << csum[i+1] << ' ';
  cout << dp[n] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
int p = 998244353;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t, n, i, j, k, l, r, l1;
  cin >> n >> k;
  vector<pair<int, int>> v;
  for (i = 1; i <= k; i++) {
    cin >> l >> r;
    v.push_back({l, r});
  }
  sort(v.begin(), v.end());
  int dp[n + 1];
  memset(dp, 0, sizeof(dp));
  dp[0] = 0;
  dp[1] = 1;
  int dp1[n + 1];
  dp[0] = 0;
  dp[1] = 1;
  dp1[0] = 0;
  dp1[1] = 1;
  for (i = 2; i <= n; i++) {
    for (j = 0; j < v.size(); j++) {
      l = v[j].first, k = v[j].second;
      k = min(k, i);
      if (l <= k) {
        if (i == k)
          l1 = ((dp1[i - l] - dp1[i - k]) % p + p) % p;
        else
          l1 = ((dp1[i - l] - dp1[i - k - 1]) % p + p) % p;
        dp[i] = (dp[i] + l1) % p;
      }
    }
    dp1[i] = (dp[i] + dp1[i - 1]) % p;
  }
  cout << dp[n] % p;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
int p = 998244353;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t, n, i, j, k, l, r;
  cin >> n >> k;
  set<int> s;

  for (i = 1; i <= k; i++) {
    cin >> l >> r;
    for (j = l; j <= r; j++)
      s.insert(j);
  }
  int dp[n + 1];
  memset(dp, 0, sizeof(dp));
  dp[0] = 0;
  dp[1] = 1;
  for (i = 2; i <= n; i++) {
    for (auto it = s.begin(); it != s.end(); it++) {
      j = *it;
      if (j <= i)
        dp[i] = (dp[i] + dp[i - j]) % p;
      else
        break;
    }
  }
  cout << dp[n] % p;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

vector<int> s;
vector<unsigned long long int> dp;
vector<unsigned long long int> dpsum;
vector<int> l;
vector<int> r;
vector<int> tmp;
long long mod = 998244353;

int main() {

  int n, k;
  cin >> n >> k;

  for (int i = 0; i < k; i++) {
    int t1, t2;
    cin >> t1 >> t2;
    l.push_back(t1);
    r.push_back(t2);
  }

  dp.resize(n + 1, 0);
  dpsum.resize(n + 1, 0);
  dp[1] = 1;
  dpsum[1] = 1;

  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      int ll = i - r[j];
      ll = max(ll, 1);
      int rr = i - l[j];
      if (rr < 0)
        continue;
      dp[i] += dpsum[rr] - dpsum[ll - 1];
      dp[i] = (dp[i]) % mod;
    }
    dpsum[i] = dpsum[i - 1] + dp[i];
  }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

vector<int> s;
vector<unsigned long long int> dp;
vector<int> tmp;

int main() {

  int n, k;
  cin >> n >> k;

  for (int i = 0; i < k; i++) {
    int t1, t2;
    cin >> t1 >> t2;
    tmp.push_back(t1);
    tmp.push_back(t2);
  }

  sort(tmp.begin(), tmp.end());

  for (int i = 0; i < 2 * k; i = i + 2)
    for (int j = tmp[i]; j <= tmp[i + 1]; j++)
      s.push_back(j);

  dp.resize(n, 0);

  dp[0] = 1;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < s.size() && i + s[j] < n; j++) {
      dp[i + s[j]] += dp[i];
    }
  }

  cout << dp[n - 1] % 998244353 << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
using ll = long long;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)

int L[10];
int R[10];

bool Map[200007];
int K;
bool in(int x) {
  for (int i = 0; i < K; i++) {
    if (L[i] <= x && x <= R[i])
      return true;
  }
  return false;
}

int path(int from, int to) {
  int ans = 0;
  if (from > to)
    return 0;
  if (from == to)
    return 1;
  for (int i = 0; i < K; i++) {
    for (int j = L[i]; j <= R[i]; j++) {
      ans += path(from + j, to);
      ans %= 998244353;
    }
  }
  return ans;
}

ll ans[200007];
ll anssum[200007];
int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;

  ans[1] = 1;
  anssum[1] = 1;
  cin >> N >> K;
  int count = 0;
  rep(i, K) {
    int l, r;
    cin >> l >> r;
    L[i] = l;
    R[i] = r;
    /*for (int j = l; j <= r; j++) {
        Map[j] = true;
    }*/
  }
  /*rep1(i, N) {
      rep(p, K) {
          for (int j = L[p]; j <= R[p]; j++) {
              if (j <= i)ans[i] += ans[i - j], ans[i] %= 998244353;
          }
          anssum[i] = anssum[i] + ans[i];
      }
  }*/

  for (int i = 2; i <= N; i++) {
    rep(p, K) {
      /*for (int j = L[p]; j <= R[p]; j++) {
          if (j <= i)ans[i] += ans[i - j], ans[i] %= 998244353;
      }*/
      ans[i] +=
          998244353 + anssum[max(i - L[p], 0)] - anssum[max(i - R[p] - 1, 0)];
      ans[i] %= 998244353;
    }

    anssum[i] = anssum[i - 1] + ans[i];
    anssum[i] %= 998244353;
  }
  // rep1(i, N)cout << i << ": " << ans[i] << endl;
  // rep(i, N)cout << "ans" << i << ": " << ans[i] << endl;
  // rep(i, N)cout << "sum" << i << ": " << anssum[i] << endl;
  cout << ans[N] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
using ll = long long;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)

int L[10];
int R[10];

bool Map[200007];
int K;
bool in(int x) {
  for (int i = 0; i < K; i++) {
    if (L[i] <= x && x <= R[i])
      return true;
  }
  return false;
}

int path(int from, int to) {
  int ans = 0;
  if (from > to)
    return 0;
  if (from == to)
    return 1;
  for (int i = 0; i < K; i++) {
    for (int j = L[i]; j <= R[i]; j++) {
      ans += path(from + j, to);
      ans %= 998244353;
    }
  }
  return ans;
}

int ans[200007];

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;

  ans[1] = 1;
  cin >> N >> K;
  int count = 0;
  rep(i, K) {
    int l, r;
    cin >> l >> r;
    L[i] = l;
    R[i] = r;
    /*for (int j = l; j <= r; j++) {
        Map[j] = true;
    }*/
  }
  rep1(i, N) {
    rep(p, K) {
      for (int j = L[p]; j <= R[p]; j++) {
        if (j <= i)
          ans[i] += ans[i - j], ans[i] %= 998244353;
      }
    }
  }

  // rep1(i, N)cout << i << ": " << ans[i] << endl;
  cout << ans[N] << endl;
  return 0;
}