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

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define mod 998244353
#define int long long int
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

int32_t main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    int a[k][2];
    for (int i = 0; i < k; i++)
      cin >> a[i][0] >> a[i][1];
    vector<int> dp(n + 1, 0);
    vector<int> prev(n + 1, 0);
    dp[1] = 1;
    prev[1] = 1;
    for (int i = 2; i <= n; i++) {
      for (int j = 0; j < k; j++) {
        int l = i - a[j][0];
        int r = i - a[j][1] - 1;
        if (l <= 0) {
          continue;
        }
        r = max(r, 0LL);
        // cout<<i<<" "<<l<<" "<<r<<" "<<prev[l]-prev[r]<<endl;
        dp[i] = (dp[i] % mod + (prev[l] - prev[r]) % mod + mod) % mod;
        // cout<<dp[i]<<endl;
      }
      prev[i] = (dp[i] % mod + prev[i - 1] % mod + mod) % mod;
      // cout<<prev[i]<<" ";
    }
    cout << dp[n] % mod << endl;
  }
}
// 1 1 1 2 4
// 1 2 3 5 9


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define mod 998244353
#define int long long int
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

int32_t main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    int a[k][2];
    for (int i = 0; i < k; i++)
      cin >> a[i][0] >> a[i][1];
    vector<int> dp(n + 1, 0);
    dp[1] = 1;
    for (int i = 1; i < n; i++) {
      for (int j = 0; j < k; j++) {
        for (int p = a[j][0]; p <= a[j][1]; p++) {
          // cout<<i<<" "<<p<<endl;
          if (i + p <= n)
            dp[i + p] = (dp[i + p] % mod + dp[i] % mod) % mod;
        }
      }
    }
    cout << dp[n] << endl;
  }
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <utility>
#include <vector>
using namespace std;
const int mod = 998244353;
const int max_size = 2e5 + 11;
int n, k, a, b;
long long dp[max_size] = {};
vector<pair<int, int>> arr;

int main() {
  scanf("%d%d", &n, &k);
  while (k--) {
    scanf("%d%d", &a, &b);
    arr.emplace_back(a, b);
  }
  dp[1] = 1, dp[2] = -1;
  for (int i = 1; i <= n; ++i) {
    if (i > 1) {
      dp[i] += dp[i - 1];
      dp[i] %= mod;
    }
    for (auto &it : arr) {
      if (i + it.first <= n) {
        dp[i + it.first] += dp[i];
        dp[i + it.first] %= mod;
      }
      if (i + it.second + 1 <= n) {
        dp[i + it.second + 1] -= (dp[i] - mod);
        dp[i + it.second + 1] %= mod;
      }
    }
  }
  printf("%lld", dp[n] % mod);
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
using namespace std;
const int mod = 998244353;
const int max_size = 2e5 + 11;
int n, k, a, b;
map<int, int> m;
long long dp[max_size] = {};

int main() {
  scanf("%d%d", &n, &k);
  while (k--) {
    scanf("%d%d", &a, &b);
    for (int i = a; i <= b; ++i)
      m[i]++;
  }
  dp[1] = 1;
  for (int i = 1; i <= n; ++i) {
    for (auto &it : m) {
      if (i + it.first > n)
        break;
      dp[i + it.first] += dp[i];
      dp[i + it.first] %= mod;
    }
  }
  printf("%lld", dp[n] % mod);
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

// #define int 		long long
#define endl "\\n"
#define mod 998244353
#define inf 1e18
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)

int32_t main() {

  IOS;

  int n, k, x, y;
  cin >> n >> k;

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

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

  vector<int> dp(n + 1, 0);
  dp[1] = 1;

  for (int i = 2; i <= n; i++) {

    for (int j = 0; j < k; j++) {
      if (i <= s[j].first)
        continue;

      int left = s[j].first;
      int right = min(s[j].second, i - 1);

      int val = (dp[i - left] - dp[i - right - 1] + mod) % mod;

      dp[i] = (dp[i] + val) % mod;
    }

    if (i != n)
      (dp[i] += dp[i - 1]) %= mod;
  }

  cout << dp[n];

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

// #define int 		long long
#define endl "\\n"
#define mod 998244353
#define inf 1e18
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)

int32_t main() {

  IOS;

  int n, k, x, y;
  cin >> n >> k;

  vector<bool> jumpT(n + 1);

  for (int i = 0; i < k; i++) {
    cin >> x >> y;

    for (int i = x; i <= y; i++)
      jumpT[i] = 1;
  }

  vector<int> jump;

  for (int i = 1; i <= n; i++) {
    if (jumpT[i])
      jump.push_back(i);
  }

  vector<int> dp(n + 1, 0);
  dp[1] = 1;

  for (int i = 1; i <= n; i++) {

    for (int dx : jump) {

      x = i + dx;
      if (x > n)
        break;

      dp[x] = (dp[x] + dp[i]) % mod;
    }
  }

  cout << dp[n];

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

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

#define fi first
#define se second
#define pb push_back

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;

const int N = 2e5;
const int MOD = 998244353;
int n, k;
int l[N + 2], r[N + 2];
ll dp[N + 2], pref[N + 2];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  clock_t ck;
  ck = clock();
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    cin >> l[i] >> r[i];
  }
  dp[1] = 1;
  pref[1] = 1;
  for (int i = 2; i <= n; i++) {
    // cout << i << " :\\n";
    for (int j = 1; j <= k; j++) {
      if (i - l[j] > 0)
        dp[i] += pref[i - l[j]];
      if (i - r[j] - 1 > 0)
        dp[i] -= pref[i - r[j] - 1];
      // cout << max(0,i-l[j]) << ' ' << max(0,i-r[j]-1) << '\\n';
      dp[i] %= MOD;
      dp[i] += MOD;
      dp[i] %= MOD;
    }
    pref[i] = pref[i - 1] + dp[i];
    pref[i] %= MOD;
  }
  cout << dp[n] << '\\n';
  ck = clock() - ck;
  cerr << "It took " << 1.0 * ck / CLOCKS_PER_SEC << " sec\\n";
  return 0;
}

Error: TLE

Fixed submission:

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

#define fi first
#define se second
#define pb push_back

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;

const int N = 2e5;
const int MOD = 998244353;
int n, k;
int dp[N + 2];
vector<int> adj;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  clock_t ck;
  ck = clock();
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++) {
      adj.pb(j);
    }
  }
  sort(adj.begin(), adj.end());
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j : adj) {
      if (j > i)
        break;
      dp[i] += dp[i - j];
      dp[i] %= MOD;
    }
  }
  cout << dp[n] << '\\n';
  ck = clock() - ck;
  cerr << "It took " << 1.0 * ck / CLOCKS_PER_SEC << " sec\\n";
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define int long long int
using namespace std;
int mod = 998244353;

int n;
// int dp[100000001];
// int find(vector<int> v, int curr) {
// 	if (curr == n)
// 		return 1;
// 	if (curr > n)
// 		return 0;
// 	if (dp[curr] != -1)
// 		return dp[curr];
// 	int ans = 0;
// 	for (int j = 0; j < v.size(); j++) {
// 		if (curr + v[j] <= n) {
// 			ans = (ans + find(v, curr + v[j])) % mod;
// 		}
// 	}
// 	return dp[curr] = ans % mod;
// }

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio;

  int k, a, b;
  cin >> n >> k;
  int l[k], r[k];
  for (int i = 0; i < k; i++) {
    cin >> l[i] >> r[i];
    ++r[i];
  }
  int dp[n];
  memset(dp, 0, sizeof dp);
  dp[0] = 1, dp[1] = -1;
  for (int i = 0; i < n; i++) {
    if (i != 0)
      dp[i] = (dp[i] + dp[i - 1]) % mod;
    for (int j = 0; j < k; j++) {
      if (i + l[j] < n)
        dp[i + l[j]] = (dp[i + l[j]] + dp[i] + mod) % mod;
      if (i + r[j] < n)
        dp[i + r[j]] = (dp[i + r[j]] - dp[i] + mod) % mod;
    }
  }
  cout << dp[n - 1] % mod;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define int long long int
using namespace std;
int mod = 998244353;

int n;
int dp[100001];
int find(vector<int> v, int curr) {
  if (curr == n)
    return 1;
  if (curr > n)
    return 0;
  if (dp[curr] != -1)
    return dp[curr];
  int ans = 0;
  for (int j = 0; j < v.size(); j++) {
    if (curr + v[j] <= n) {
      ans = (ans + find(v, curr + v[j])) % mod;
    }
  }
  return dp[curr] = ans % mod;
}

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio;

  int k, a, b;
  cin >> n >> k;
  memset(dp, -1, sizeof dp);
  set<int> s;
  for (int i = 0; i < k; i++) {
    cin >> a >> b;
    s.insert(a), s.insert(b);
  }
  vector<int> v;
  for (auto it = s.begin(); it != s.end(); it++) {
    a = (int)*it;
    v.push_back(a);
  }
  cout << find(v, 1);
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

// #include<atcoder/all>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
// using namespace atcoder;

#define ll long long
#define pal pair<ll, ll>
#define pad pair<double, double>
#define PB push_back
#define MP make_pair
#define F first
#define S second

const ll MOD = 998244353;
const ll INF = 1e18;
const double EPS = 1e-8;

template <typename T, typename U> bool chmax(T &max, const U &x) {
  if (max < x) {
    max = x;
    return true;
  }
  return false;
}

template <typename T, typename U> bool chmin(T &min, const U &x) {
  if (min > x) {
    min = x;
    return true;
  }
  return false;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pal> ivl;
  vector<ll> dp(n + 1, 0);
  vector<ll> ruiwa(n + 1, 0);
  for (ll i = 0; i < k; i++) {
    ll l, r;
    cin >> l >> r;
    ivl.PB(MP(l, r));
  }
  dp[1] = 1;
  ruiwa[1] = 1;
  for (ll i = 2; i <= n; i++) {
    for (pal p : ivl) {
      if (i - p.F < 0) {
        continue;
      }
      ll hiku = ruiwa[0];
      if (i - p.S > 0) {
        hiku = ruiwa[i - p.S - 1];
      }
      dp[i] = dp[i] + ruiwa[i - p.F] - hiku;
      dp[i] %= MOD;
      if (dp[i] < 0)
        dp[i] += MOD;
    }
    ruiwa[i] = ruiwa[i - 1] + dp[i];
    ruiwa[i] %= MOD;
  }
  cout << dp[n] << endl;
}

Error: TLE

Fixed submission:

// #include<atcoder/all>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
// using namespace atcoder;

#define ll long long
#define pal pair<ll, ll>
#define pad pair<double, double>
#define PB push_back
#define MP make_pair
#define F first
#define S second

const ll MOD = 998244353;
const ll INF = 1e18;
const double EPS = 1e-8;

template <typename T, typename U> bool chmax(T &max, const U &x) {
  if (max < x) {
    max = x;
    return true;
  }
  return false;
}

template <typename T, typename U> bool chmin(T &min, const U &x) {
  if (min > x) {
    min = x;
    return true;
  }
  return false;
}

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

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

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

  int l[k], r[k];

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

  long long dp[n + 1];
  long long dpsum[n + 1];
  for (int i = 0; i <= n; i++) {
    dp[i] = 0;
    dpsum[i] = 0;
  }
  dp[1] = 1;
  dpsum[1] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      int li = i - r[j];
      int ri = i - l[j];
      if (ri < 1)
        continue;
      if (li < 1)
        li = 1;
      dp[i] += (dpsum[ri] - dpsum[li - 1]);
      dp[i] %= 998244353;
    }
    dpsum[i] = (dpsum[i - 1] + dp[i]);
  }
  cout << dp[n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

  int l, r;
  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());
  long long dp[n + 1];
  for (int i = 0; i <= n; i++)
    dp[i] = 0;
  dp[1] = 1;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < v.size(); j++) {
      if ((i - v[j]) > 0) {
        dp[i] += (dp[i - v[j]]);
        dp[i] = dp[i] % 998244353;
      } else {
        break;
      }
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int 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; }

void solve() {

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

  vector<int> l(k), r(k);

  rep(i, k) { cin >> l[i] >> r[i]; }

  vector<mint> dp(n + 1);
  vector<mint> dpsum(n + 1);

  dp[1] = 1;
  dpsum[1] = 1;

  for (int i = 2; i <= n; i++) {

    for (int j = 0; j < k; j++) {

      int li = i - r[j];
      int ri = i - l[j];

      if (ri < 0)
        continue;

      li = max(li, 1);

      dp[i] += dpsum[ri] - dpsum[li - 1];
    }

    dpsum[i] = dp[i] + dpsum[i - 1];
  }

  cout << dp[n];
}

int main() {

  int y;

  y = 1;
  // cin>>y ;

  while (y--) {

    solve();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int mod = 998244353;

void solve() {

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

  vector<int> v;

  rep(i, k) {
    int x, y;
    cin >> x >> y;

    for (int i = x; i <= y; i++) {
      v.push_back(i);
    }
  }

  // sort(v.begin(),v.end()) ;

  vector<int> dp(n + 1);
  dp[1] = 1;

  for (int i = 2; i <= n; i++) {

    for (auto x : v) {
      if (x < i) {
        dp[i] += dp[i - x];
        dp[i] %= mod;
      }
    }
  }
  /*
  rep(i,n){
          cout<<dp[i]<<" " ;
  }
  cout<<"\\n" ;

*/
  cout << dp[n];
}

int main() {

  int y;

  y = 1;
  // cin>>y ;

  while (y--) {

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
#define _GLIBCXX_DEBUG
ll mod = 998244353;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> l(k), r(k);
  rep(i, k) 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++) {
    rep(j, k) {
      int li = i - r[j];
      int ri = i - l[j];
      if (ri < 0)
        continue;
      li = max(li, 1);
      dp[i] += dpsum[ri] - dpsum[li - 1];
      dp[i] += mod;
      dp[i] %= mod;
    }
    dpsum[i] += dpsum[i - 1] + dp[i];
    dpsum[i] %= mod;
    // cout << dp[i] << " " << dpsum[i] << endl;
  }
  cout << dp[n] << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
#define _GLIBCXX_DEBUG
ll mod = 998244353;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> l(k), r(k);
  rep(i, k) cin >> l[i] >> r[i];

  vector<ll> dp(200010);
  vector<ll> dpsum(20010);
  dp[1] = 1;
  dpsum[1] = 1;

  for (int i = 2; i <= n; i++) {
    rep(j, k) {
      int li = i - r[j];
      int ri = i - l[j];
      if (ri < 0)
        continue;
      li = max(li, 1);
      dp[i] += dpsum[ri] - dpsum[li - 1];
      dp[i] += mod;
      dp[i] %= mod;
    }
    dpsum[i] += dpsum[i - 1] + dp[i];
    dpsum[i] %= mod;
    // cout << dp[i] << " " << dpsum[i] << endl;
  }
  cout << dp[n] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 998244353;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> sections;
  for (int i = 0; i < k; ++i) {
    ll l, r;
    cin >> l >> r;
    sections.push_back({l, r});
  }
  vector<ll> f(2 * n + 100, 0), a(2 * n + 100, 0);
  f[1] = 1;
  for (int i = 1; i < n; ++i) {
    for (int j = 0; j < k; ++j) {
      ll l = sections[j].first;
      ll r = sections[j].second;
      a[i + l] += f[i];
      a[i + r + 1] += (mod - f[i]);
      a[i + l] %= mod;
      a[i + r + 1] %= mod;
    }
    if (i == 1)
      f[i + 1] = a[i + 1];
    else
      f[i + 1] = f[i] + a[i + 1];
    f[i + 1] %= mod;
  }
  cout << f[n] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 998244353;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> sections;
  for (int i = 0; i < k; ++i) {
    ll l, r;
    cin >> l >> r;
    sections.push_back({l, r});
  }
  vector<ll> f(2 * n + 100, 0), a(2 * n + 100, 0);
  f[1] = 1;
  for (int i = 1; i < 2 * n; ++i) {
    for (int j = 0; j < k; ++j) {
      ll l = sections[j].first;
      ll r = sections[j].second;
      a[i + l] += f[i];
      a[i + r + 1] += (mod - f[i]);
      a[i + l] %= mod;
      a[i + r + 1] %= mod;
    }
    if (i == 1)
      f[i + 1] = a[i + 1];
    else
      f[i + 1] = f[i] + a[i + 1];
    f[i + 1] %= mod;
  }
  cout << f[n] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// g++ -std=c++11

using namespace std;
typedef long long ll;

#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define DIV 998244353

long long dp[200005];

int main() {
  ll N, K;
  cin >> N >> K;
  vector<pair<ll, ll>> LR(K);

  rep(i, K) { cin >> LR[i].first >> LR[i].second; }

  ll cur = 1;
  dp[0] = -1;
  ll ans = -1;

  rep(i, N) {
    rep(j, K) {
      ll l = LR[j].first;
      ll r = LR[j].second;
      if (i + l - 1 <= N) {
        dp[i + l - 1] += cur;
        dp[i + l - 1] %= DIV;
      }
      if (i + r <= N) {
        dp[i + r] -= cur;
        dp[i + r] += DIV;
        dp[i + r] %= DIV;
      }
    }
    if (i == N - 1) {
      ans = cur;
    }

    cur += dp[i];
    cur %= DIV;
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
// g++ -std=c++11

using namespace std;
typedef long long ll;

#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define DIV 998244353

long long dp[200005];

int main() {
  ll N, K;
  cin >> N >> K;
  vector<pair<ll, ll>> LR(K);

  rep(i, K) { cin >> LR[i].first >> LR[i].second; }

  ll cur = 1;
  dp[0] = -1;
  ll ans = -1;

  rep(i, N) {
    rep(j, K) {
      ll l = LR[j].first;
      ll r = LR[j].second;
      dp[i + l - 1] += cur;
      dp[i + l - 1] %= DIV;
      dp[i + r] -= cur;
      dp[i + r] += DIV;
      dp[i + r] %= DIV;
    }
    if (i == N - 1) {
      ans = cur;
    }

    cur += dp[i];
    cur %= DIV;
  }

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

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

const int mod = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  rep(i, k) cin >> l[i] >> r[i];
  vector<ll> dp(n + 1);
  vector<ll> dps(n + 1);
  dp[1] = 1;
  dps[1] = 1;
  for (int i = 2; i <= n; i++) {
    rep(j, k) {
      int li = i - r[j];
      int ri = i - l[j];
      if (ri < 0)
        continue;
      li = max(li, 1);
      dp[i] += (dps[ri] - dps[li - 1] + mod) % mod;
    }
    dps[i] = (dps[i - 1] + dp[i] + mod) % mod;
  }
  cout << (dp[n] + mod) % mod << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

const int mod = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  rep(i, k) { cin >> l[i] >> r[i]; }
  set<int> st;
  rep(i, k) {
    for (int j = l[i]; j <= r[i]; j++)
      st.insert(j);
  }
  vector<ll> dp(n + 5);
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (auto a : st) {
      if (i > a)
        dp[i] += dp[i - a];
      dp[i] %= mod;
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const ll mod = 998244353;
const ll zero = 0;

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

  vector<ll> l(k), r(k);
  rep(i, k) cin >> l[i] >> r[i];

  vector<ll> dp(n + 1);
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    ll route = 0;
    rep(j, k) {
      if (i - l[j] > 0) {
        route += dp[i - l[j]] - dp[max(i - r[j] - 1, zero)];
        route %= mod;
      }
    }
    dp[i] = (dp[i - 1] + route) % mod;
    // cout << dp[i] << endl;
  }

  ll ans = dp[n] - dp[n - 1];
  if (ans < 0)
    ans += mod;
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
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 mod = 998244353;

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

  vector<int> lr;
  rep(i, k) {
    int l, r;
    cin >> l >> r;

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

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

  vector<ll> cnt(n);
  cnt[0] = 1;
  rep(i, n) {
    rep(j, lr.size()) {
      if (lr[j] <= i) {
        // cout << i << " " << lr[j] << endl;
        cnt[i] += cnt[i - lr[j]];
        cnt[i] %= mod;
      } else
        break;
    }
  }

  cout << cnt[n - 1] << endl;

  return 0;
}