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

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

#include <boost/multi_array.hpp>
#include <boost/optional.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/irange.hpp>
namespace adaptor = boost::adaptors;

// 多倍長テンプレ
/* ---------------------- ここから ---------------------- */
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
// 任意長整数型
using Bint = mp::cpp_int;
// 仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)
using Real = mp::number<mp::cpp_dec_float<1024>>;
/* ---------------------- ここまで ---------------------- */

using ll = long long;
using ull = unsigned long long;

ll dp[200010] = {0};

void Main() {
  int N, K;
  cin >> N >> K;

  std::vector<pair<int, int>> S;
  for (int i = 0; i < K; ++i) {
    int L, R;
    cin >> L >> R;
    S.push_back({L, R});
  }

  dp[0] = 1;
  dp[1] = -1;
  for (int i = 0; i < N; ++i) {
    if (i > 0)
      dp[i] = (dp[i] + dp[i - 1]) % 998244353;
    for (int j = 0; j < (int)S.size(); ++j) {
      int l = S[j].first;
      int r = S[j].second;
      if (i + l <= N) {
        dp[i + l] = (dp[i + l] + dp[i]) % 998244353;
      }
      if (i + r + 1 <= N) {
        dp[i + r + 1] = (dp[i + r + 1] + 998244353 - dp[i]) % 998244353;
      }
    }
  }

  cout << dp[N - 1];
  return;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
}

Error: TLE

Fixed submission:

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

#include <boost/multi_array.hpp>
#include <boost/optional.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/irange.hpp>
namespace adaptor = boost::adaptors;

// 多倍長テンプレ
/* ---------------------- ここから ---------------------- */
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
// 任意長整数型
using Bint = mp::cpp_int;
// 仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)
using Real = mp::number<mp::cpp_dec_float<1024>>;
/* ---------------------- ここまで ---------------------- */

using ll = long long;
using ull = unsigned long long;

ll dp[200010] = {0};

void Main() {
  ll N, K;
  cin >> N >> K;

  std::vector<ll> S;
  for (ll i = 0; i < K; ++i) {
    ll L, R;
    cin >> L >> R;
    for (ll j = L; j <= R; ++j) {
      S.push_back(j);
    }
  }

  dp[0] = 1;
  for (int i = 0; i < N; ++i) {
    if (dp[i] == 0) {
      continue;
    }
    for (int j = 0; j < (int)S.size(); ++j) {
      if (i + S[j] <= N) {
        dp[i + S[j]] = (dp[i + S[j]] + dp[i]) % 998244353;
      }
    }
  }

  cout << dp[N - 1];
  return;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
const int mod = 998244353;
int n, k;

int main() {
  scanf("%d %d", &n, &k);
  vector<pair<int, int>> s;
  for (int i = 0; i < k; ++i) {
    int l, r;
    scanf("%d %d", &l, &r);
    s.push_back({l, r});
  }

  vector<long long> dp(n + 1, 0);
  vector<long long> preSum(n + 1, 0);
  dp[1] = 1;
  preSum[1] = 1;
  for (int i = 2; i <= n; ++i) {
    for (int j = 0; j < k; ++j) {
      int l = i - s[j].second;
      int r = i - s[j].first;
      if (r < 0)
        continue;
      l = max(1, l);
      dp[i] += preSum[r] - preSum[l - 1];
      dp[i] %= mod;
    }
    preSum[i] = preSum[i - 1] + dp[i];
  }
  printf("%lld\\n", dp[n]);
  return 0;

  sort(s.begin(), s.end());
  // cnt[i] := the number of possible ways to reach ith cell
  // cnt[i] = sum{cnt[i-k] | k in S}
  vector<long long> cnt(n + 1, 0);
  cnt[1] = 1;
  for (int i = 2; i <= n; ++i) {
    for (auto it : s) {
      for (int k = it.first; k <= it.second; ++k) {
        if (i - k <= 0) {
          break;
        }
        cnt[i] += cnt[i - k];
      }
      cnt[i] %= mod;
    }
  }
  printf("%lld\\n", cnt[n]);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
const int mod = 998244353;
int n, k;

int main() {
  scanf("%d %d", &n, &k);
  vector<pair<int, int>> s;
  for (int i = 0; i < k; ++i) {
    int l, r;
    scanf("%d %d", &l, &r);
    s.push_back({l, r});
  }
  sort(s.begin(), s.end());

  // cnt[i] := the number of possible ways to reach ith cell
  // cnt[i] = sum{cnt[i-k] | k in S}
  vector<long long> cnt(n + 1, 0);
  cnt[1] = 1;
  for (int i = 2; i <= n; ++i) {
    for (auto it : s) {
      for (int k = it.first; k <= it.second; ++k) {
        if (i - k <= 0) {
          break;
        }
        cnt[i] += cnt[i - k];
      }
      cnt[i] %= mod;
    }
  }
  printf("%lld\\n", cnt[n]);
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#include <vector>

#define MODULO 998244353

using namespace std;

long long moveCountCache[200001] = {0, 1};
long long moveCountSumCache[200001] = {0, 1};

int main() {
  int n;
  int k;

  cin >> n >> k;

  vector<pair<int, int>> moveSegments(k);

  for (int i = 0; i < k; i++) {
    cin >> moveSegments[i].first >> moveSegments[i].second;
  }

  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      int leftEndIndex = max(i - moveSegments[j].second, 1);
      int rightEndIndex = i - moveSegments[j].first;

      if (rightEndIndex < 0)
        continue;

      moveCountCache[i] += moveCountSumCache[rightEndIndex] -
                           moveCountSumCache[leftEndIndex - 1];
      moveCountCache[i] %= MODULO;
    }

    moveCountSumCache[i] = moveCountSumCache[i - 1] + moveCountCache[i];
  }

  cout << moveCountCache[n] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <vector>

#define MODULO 998244353

using namespace std;

vector<int> moveDistances;
long long moveCountCache[200001] = {0, 1};

int main() {
  int n;
  int k;

  scanf("%d%d", &n, &k);

  for (int i = 0; i < k; i++) {
    int leftEnd;
    int rightEnd;

    scanf("%d%d", &leftEnd, &rightEnd);

    for (int j = leftEnd; j <= rightEnd; j++) {
      moveDistances.push_back(j);
    }
  }

  for (int i = 2; i <= n; i++) {
    for (int moveDistance : moveDistances) {
      moveCountCache[i] =
          (moveCountCache[i] +
           (i - moveDistance > 0 ? moveCountCache[i - moveDistance] : 0)) %
          MODULO;
    }
  }

  printf("%d\\n", moveCountCache[n]);

  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long int
#define N int(2e5 + 10)
#define mod 998244353
using namespace std;
ll dp[N], pre[N];
int l[N], r[N], n, k;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout.precision(10);
  cin >> n >> k;
  for (int i = 1; i <= k; i++)
    cin >> l[i] >> r[i];
  pre[0] = dp[0] = 0ll;
  pre[1] = dp[1] = 1ll;
  for (int i = 2; i <= n; i++) {
    dp[i] = 0ll;
    for (int j = 1, u, v; j <= k; j++) {
      if (l[j] >= i)
        continue;
      u = l[j];
      v = min(i - 1, r[j]);
      dp[i] = ((dp[i] + pre[i - u]) % mod - pre[i - v - 1]) % mod;
      if (dp[i] < 0ll)
        dp[i] += mod;
    }
    pre[i] = (pre[i - 1] + dp[i]) % mod;
  }
  cout << dp[n] << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define N int(1e5 + 10)
#define mod 998244353
using namespace std;
ll dp[N], pre[N];
int l[N], r[N], n, k;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout.precision(10);
  cin >> n >> k;
  for (int i = 1; i <= k; i++)
    cin >> l[i] >> r[i];
  pre[0] = dp[0] = 0ll;
  pre[1] = dp[1] = 1ll;
  for (int i = 2; i <= n; i++) {
    dp[i] = 0ll;
    for (int j = 1, u, v; j <= k; j++) {
      if (l[j] >= i)
        continue;
      u = l[j];
      v = min(i - 1, r[j]);
      dp[i] = ((dp[i] + pre[i - u]) % mod - pre[i - v - 1]) % mod;
      if (dp[i] < 0ll)
        dp[i] += mod;
    }
    pre[i] = (pre[i - 1] + dp[i]) % mod;
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <cmath>
#include <string>

using namespace std;

#define ll long long int
#define ld long double
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define deb(x) cout << #x << '=' << x << endl
#define MOD 998244353
const int N = 2e5 + 5;
int dp[N];
int pref[N], l[N], r[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, k;
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    cin >> l[i] >> r[i];
  }
  dp[1] = 1;
  pref[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      if (i <= l[j]) {
        continue;
      }
      int left = l[j];
      int right = min(r[j], i - 1);
      int add = (pref[i - left] - pref[i - right - 1] + MOD) % MOD;
      dp[i] = (dp[i] + add) % MOD;
    }
    pref[i] = pref[i - 1] + dp[i];
    pref[i] %= MOD;
  }
  cout << dp[n] << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#include <string>

using namespace std;

#define ll long long int
#define ld long double
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define deb(x) cout << #x << '=' << x << endl
#define MOD 998244353
const int N = 2e5 + 5;
int dp[N];
int b[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, k;
  cin >> n >> k;
  set<int> S;
  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    b[l]++;
    b[r + 1]--;
  }
  for (int i = 1; i <= n; i++) {
    b[i] += b[i - 1];
  }
  for (int i = 1; i <= n; i++) {
    if (b[i] > 0) {
      S.insert(i);
    }
  }
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (auto j : S) {
      if (i - j > 0) {
        dp[i] += dp[i - j];
        dp[i] %= MOD;
      }
    }
  }
  cout << dp[n] << "\\n";
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int MOD = 998244353;

int main() {
  int N, K;
  cin >> N >> K;

  set<long long> s;
  vector<long long> l(K);
  vector<long long> r(K);

  for (long long i = 0; i < K; ++i) {
    cin >> l[i] >> r[i];
  }

  vector<long long> dp(N + 10);
  vector<long long> dp_sum(N + 10);

  dp[1] = 1;
  dp_sum[1] = 1;

  for (int i = 2; i < N + 10; ++i) {
    for (int j = 0; j < K; ++j) {
      int li = i - r[j];
      int ri = i - l[j];
      if (ri < 0)
        continue;
      li = max(1, li);
      long long tmp = dp_sum[ri] - dp_sum[li - 1];
      if (tmp < 0)
        tmp += MOD;
      dp[i] += tmp;
      dp[i] %= MOD;
    }
    dp_sum[i] += dp_sum[i - 1] + dp[i];
    dp_sum[i] %= MOD;
  }

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

Error: TLE

Fixed submission:

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

const int MOD = 998244353;

int main() {
  int N, K;
  cin >> N >> K;

  vector<long long> dp(N + 10);
  set<long long> s;
  for (long long i = 0; i < K; ++i) {
    long long L, R;
    cin >> L >> R;
    for (long long j = L; j <= R; ++j) {
      s.insert(j);
    }
  }

  dp[0] = 1;
  for (int i = 0; i < N + 10; ++i) {
    for (auto num : s) {
      if (i + num < N + 10) {
        dp[i + num] += dp[i];
        dp[i + num] %= MOD;
      }
    }
  }

  cout << dp[N - 1] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;
using ll = long long;

ll mod = 998244353;

int main() {
  int n, k;
  cin >> n >> k;

  vector<pair<ll, ll>> lr(k);
  for (auto &v : lr) {
    ll l, r;
    cin >> l >> r;
    v = make_pair(l, r);
  }

  vector<ll> vec1(5e5, 0), vec2(5e5, 0), vec3(5e5, 0);
  vec1[1] = 1;
  vec2[1] = 1;
  vec3[1] = 1;

  for (ll i = 1; i < n; ++i) {
    for (auto &v : lr) {
      ll l = v.first;
      ll r = v.second;
      vec1[i + l] = (vec1[i + l] + vec3[i]) % mod;
      vec2[i + r] = (vec2[i + r] + vec3[i]) % mod;
    }
    vec3[i + 1] = (vec3[i] - vec2[i]) % mod;
    if (vec3[i + 1] < 0)
      vec3[i + 1] += mod;
    vec3[i + 1] = (vec3[i + 1] + vec1[i + 1]) % mod;
  }

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


Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;
using ll = long long;

ll mod = 998244353;

int main() {
  int n, k;
  cin >> n >> k;

  vector<pair<ll, ll>> lr(k);
  for (auto &v : lr) {
    ll l, r;
    cin >> l >> r;
    v = make_pair(l, r);
  }

  vector<ll> vec1(3e5, 0), vec2(3e5, 0), vec3(3e5, 0);
  vec1[1] = 1;
  vec2[1] = 1;
  vec3[1] = 1;

  for (ll i = 1; i < n; ++i) {
    for (auto &v : lr) {
      ll l = v.first;
      ll r = v.second;
      vec1[i + l] = (vec1[i + l] + vec3[i]) % mod;
      vec2[i + r] = (vec2[i + r] + vec3[i]) % mod;
    }
    vec3[i + 1] = (vec3[i] - vec2[i]) % mod;
    if (vec3[i + 1] < 0)
      vec3[i + 1] += mod;
    vec3[i + 1] = (vec3[i + 1] + vec1[i + 1]) % mod;
  }

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

#include <bits/stdc++.h>
// #include "atcoder/modint"
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)

const ll mod = 998244353;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> l(k), r(k);
  rep(i, k) cin >> l.at(i) >> r.at(i);
  vector<mint> dp(n + 1);
  vector<mint> dpsum(n + 1);
  dp.at(1) = 1;
  dpsum.at(1) = 1;
  for (ll i = 2; i <= n; ++i) {
    rep(j, k) {
      ll li = i - r.at(j);
      ll ri = i - l.at(j);
      if (ri < 0)
        continue;
      li = max(li, 1LL);
      dp.at(i) += dpsum.at(ri) - dpsum.at(li - 1);
    }
    dpsum.at(i) = dpsum.at(i - 1) + dp.at(i);
  }

  cout << dp.at(n) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)

const ll dv = 998244353;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<P> v(k);
  rep(i, k) { cin >> v.at(i).first >> v.at(i).second; }
  sort(v.begin(), v.end());

  vector<ll> dp(n + 1);
  dp.at(1) = 1;
  for (ll i = 2; i <= n; ++i) {
    rep(j, k) {
      for (ll t = v.at(j).first; t <= v.at(j).second; ++t) {
        if ((i - t) < 1) {
          j = k;
          break;
        }
        dp.at(i) += dp.at(i - t);
        dp.at(i) %= dv;
      }
    }
  }
  cout << dp.at(n) << endl;

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

#include <iostream>

typedef long long ll;

const ll MOD = 998244353;

ll N, K, dp[200005], dps[200005];
int S[11][3];

ll sum(ll l, ll r) {
  if (r < 1)
    return 0;
  ll ans = dps[r];
  if (l > 1)
    ans = (ans - dps[l - 1] + MOD) % MOD;
  return ans;
}

int main() {
  std::cin >> N >> K;
  for (int i = 0; i < K; ++i)
    std::cin >> S[i][0] >> S[i][1];

  dps[0] = 0;
  dp[1] = 1, dps[1] = 1;
  for (int i = 2; i <= N; ++i) {
    dp[i] = 0;
    for (int k = 0; k < K; ++k)
      dp[i] = (dp[i] + sum(i - S[k][1], i - S[k][0])) % MOD;
    dps[i] = (dp[i] + dps[i - 1]) % MOD;
  }

  std::cout << dp[N] << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

typedef long long ll;

const ll MOD = 998244353;

ll N, K, dp[200005];
int S[11][3];

ll solve(int i) {
  if (i > N)
    return 0;
  if (i == N)
    return 1;
  if (dp[i] != -1)
    return dp[i];
  ll ans = 0;
  for (int k = 0; k < K; ++k)
    for (int d = S[k][0]; d <= S[k][1]; ++d)
      ans = (ans + solve(i + d)) % MOD;
  return dp[i] = ans;
}

int main() {
  std::cin >> N >> K;
  for (int i = 0; i < K; ++i)
    std::cin >> S[i][0] >> S[i][1];

  for (int i = 0; i < 200005; ++i)
    dp[i] = -1;

  std::cout << solve(1) << "\\n";

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

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

int main() {
  ll mod = 998244353;
  int n, k;
  // set<int>vals;
  cin >> n >> k;
  pair<int, int> range[k];
  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    range[i] = {l, r};
    /*for(int i=l;i<=r;i++)
            vals.insert(i);*/
  }
  sort(range, range + k);
  ll ways[n + 1];
  memset(ways, 0, sizeof(ways));
  ways[1] = 1;
  ways[2] = -1;
  for (int i = 1; i <= n; i++) {
    if (i > 1)
      ways[i] = (ways[i - 1] + ways[i]) % mod;
    for (int j = 0; j < k; j++) {
      if (range[j].first + i > n)
        continue;
      ways[i + range[j].first] = (ways[i] + ways[i + range[j].first]) % mod;

      if (range[j].second + i + 1 > n)
        continue;
      ways[i + range[j].second + 1] =
          (ways[i + range[j].second + 1] - ways[i]) % mod;
    }
    //	ways[i] += ways[i-1];
  }
  /*for(int i=2;i<=n;i++)
  {
          for(int j=0;j<k;j++)
                  ways[i] = ways[i]+ways[i-1];
  }*/
  if (ways[n] < 0)
    ways[n] = mod + ways[n];
  cout << ways[n] << "\\n";
}


Error: TLE

Fixed submission:

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

int main() {
  ll mod = 998244353;
  int n, k;
  set<int> vals;
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++)
      vals.insert(j);
  }
  ll ways[n + 1];
  memset(ways, 0, sizeof(ways));
  ways[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (auto x : vals) {
      if (x >= i)
        break;
      ways[i] = (ways[i] + ways[i - x]) % mod;
    }
  }
  cout << ways[n] << "\\n";
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;

// いもす法を使ったDP(本番終了後の実装)
int main() {
  int N, K;
  cin >> N >> K;
  vector<pair<int, int>> s;
  for (int i = 0; i < K; i++) {
    int li, ri;
    cin >> li >> ri;
    pair<int, int> si(li, ri);
    s.emplace_back(si);
  }
  int slen = s.size();

  vector<int> routnum(N, 0);
  vector<int> routnumdif(N - 1, 0); // -> routnum[i+1]-routnum[i]
  routnum[0] = 1;
  routnumdif[0] = -1;
  for (int i = 0; i < N - 1; i++) {
    for (int j = 0; j < K; j++) {
      int lj = s[j].first;
      int rj = s[j].second;
      if (i + lj - 1 < N - 1) {
        routnumdif[i + lj - 1] += routnum[i];
        routnumdif[i + lj - 1] %= mod;
      }
      if (i + rj < N - 1) {
        routnumdif[i + rj] -= routnum[i];
        routnumdif[i + rj] += mod;
        routnumdif[i + rj] %= mod;
      }
    }
    routnum[i + 1] = routnum[i] + routnumdif[i];
    routnum[i + 1] %= mod;
  }

  cout << routnum[N - 1] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> s;
  for (int i = 0; i < K; i++) {
    int li, ri;
    cin >> li >> ri;
    for (int j = li; j <= ri; j++) {
      s.emplace_back(j);
    }
  }
  int slen = s.size();

  vector<int> routnum(N, 0);
  routnum[0] = 1;
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < slen; j++) {
      if (i - s[j] >= 0) {
        routnum[i] += routnum[i - s[j]];
        routnum[i] %= mod;
      }
    }
  }

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

#include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main(void) {
  long long int mod = 998244353;
  long long int n;
  cin >> n;
  long long int k;
  cin >> k;
  vector<pair<long long int, long long int>> hoge(k);
  for (long long int i = 0; i < k; i++) {
    cin >> hoge.at(i).first >> hoge.at(i).second;
  }
  vector<long long int> dp(n, 0);
  vector<long long int> rui(n, 0);
  rui.at(0) = 0;
  dp.at(0) = 1;
  vector<long long int> imos(n, 0);
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < k; j++) {
      if (i + hoge.at(j).first < n)
        imos.at(i + hoge.at(j).first) += dp.at(i);
      if (i + hoge.at(j).second + 1 < n)
        imos.at(i + hoge.at(j).second + 1) -= dp.at(i);
      if (i != n - 1) {
        rui.at(i + 1) %= mod;
        rui.at(i + 1) = rui.at(i) + imos.at(i + 1);
        dp.at(i + 1) = rui.at(i + 1);
        dp.at(i + 1) %= mod;
      }
    }
  }
  cout << dp.at(n - 1) << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
long long int n, k;
vector<long long int> all;
vector<long long int> dp;
int main(void) {
  cin >> n >> k;
  vector<pair<long long int, long long int>> hoge(n);
  for (long long int i = 0; i < k; i++) {
    cin >> hoge.at(i).first >> hoge.at(i).second;
    for (long long int j = hoge.at(i).first; j <= hoge.at(i).second; j++) {
      all.push_back(j);
    }
  }
  sort(all.begin(), all.end());
  dp = vector<long long int>(n, 0);
  dp.at(0) = 1;
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < all.size(); j++) {
      long long int next = i + all.at(j);
      if (next >= n) {
        break;
      }
      dp.at(next) += dp.at(i);
      dp.at(next) %= 998244353;
    }
  }
  dp.at(n - 1) %= 998244353;
  cout << dp.at(n - 1) << endl;
}