prithivMLmods/Coder-Stat huggingface dataset:
Problem p02548 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl ("\n")
#define pi (3.141592653589)
#define mod 1000000007
#define INF 1000000009
#define float double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define so sizeof
#define pii pair<ll, ll>
#define all(c) c.begin(), c.end()
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define max3(a, b, c) max(c, max(a, b))
#define max4(a, b, c, d) max(d, max(c, max(a, b)))
#define repe(i, a) for (auto i : a)
#define st string
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define rrep(i, a, b) for (ll i = b; i >= a; i--)
#define fast                                                                   \
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
void solve() {
  ll n;
  cin >> n;
  ll cnt = 0;
  rep(i, 1, n - 1) {
    for (ll j = 1; j * j <= n - i; j++) {
      if ((j * ((n - i) / j)) == (n - i)) {
        //  cout<<j<<" "<<(n-i)/j<<" "<<i<<endl;
        if (j != (n - i) / j)
          cnt += 2;
        else
          cnt++;
      }
    }
  }
  cout << cnt << endl;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast
  // ll t;
  // cin>>t;
  // while(t--)
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl ("\n")
#define pi (3.141592653589)
#define mod 1000000007
#define INF 1000000009
#define float double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define so sizeof
#define pii pair<ll, ll>
#define all(c) c.begin(), c.end()
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define max3(a, b, c) max(c, max(a, b))
#define max4(a, b, c, d) max(d, max(c, max(a, b)))
#define repe(i, a) for (auto i : a)
#define st string
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define rrep(i, a, b) for (ll i = b; i >= a; i--)
#define fast                                                                   \
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
void solve() {
  ll n;
  cin >> n;
  ll cnt = 0;
  rep(i, 1, n - 1) { cnt += (n - 1) / i; }
  //}
  cout << cnt << endl;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast
  // ll t;
  // cin>>t;
  // while(t--)
  solve();
  return 0;
}
Problem p02548 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<pii, int> p3i;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<p3i> v3i;
typedef vector<vii> vvii;
typedef vector<p3i> vp3i;
typedef long double ld;
typedef vector<ld> vld;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define REPD(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define FORD(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define rv(v) reverse(all(v))
#define CL(v, val) memset((v), (val), sizeof((v)))
#define SORT(a) sort(all(a))
#define un(v) SORT(v), (v).resize(unique(all(v)) - (v).begin())
#define eps 1.0e-9
#define X first
#define Y second
#define bit(n) (1 << (n))
#define bit64(n) (ll(1) << (n))
#define sqr(x) ((x) * (x))
#define sq5(x) ((x) * (x) * (x) * (x) * (x))

int main(void) {
  int n;
  scanf("%d", &n);
  ll cnt = 0;

  FOR(i, 1, n) {
    for (int j = 1; j * j <= i; j++) {
      if (i % j == 0) {
        if (j * j == i)
          cnt += 1;
        else
          cnt += 2;
      }
    }
  }

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

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<pii, int> p3i;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<p3i> v3i;
typedef vector<vii> vvii;
typedef vector<p3i> vp3i;
typedef long double ld;
typedef vector<ld> vld;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define REPD(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define FORD(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define rv(v) reverse(all(v))
#define CL(v, val) memset((v), (val), sizeof((v)))
#define SORT(a) sort(all(a))
#define un(v) SORT(v), (v).resize(unique(all(v)) - (v).begin())
#define eps 1.0e-9
#define X first
#define Y second
#define bit(n) (1 << (n))
#define bit64(n) (ll(1) << (n))
#define sqr(x) ((x) * (x))
#define sq5(x) ((x) * (x) * (x) * (x) * (x))

int main(void) {
  int n;
  scanf("%d", &n);
  ll cnt = 0;

  for (int a = 1; a < n; a++) {
    cnt += (n - 1) / a;
  }

  printf("%lld\n", cnt);
}
Problem p02548 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, s, n) for (int i = s; i < n; i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
using Graph = vector<vector<int>>;
const int INF = 1000000007;
int main() {
  ll n;
  cin >> n;
  ll ans = 0;
  ll tmp, j;
  REP(i, 1, n) {
    tmp = n - i;
    for (j = 1; j * j <= tmp; j++) {
      if (tmp % j == 0) {
        if (j == sqrt(tmp))
          ans++;
        else
          ans += 2;
      }
    }
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, s, n) for (int i = s; i < n; i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
using Graph = vector<vector<int>>;
const int INF = 1000000007;
int main() {
  ll n;
  cin >> n;
  ll ans = 0;
  REP(i, 1, n + 1) { ans += (n - 1) / i; }
  cout << ans << endl;
  return 0;
}
Problem p02548 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
using namespace std;
int main() {
  // 整数の入力
  int N;
  int cnt = 0;
  int a, b;
  cin >> N;
  for (int i = 1; i < N; i++) {
    for (int j = 1; j <= int(sqrt(N - i)); j++) {
      a = (N - i) % j;
      b = (N - i) / j;
      // cout << i <<" "<< j <<" " << (N-i)%j<< endl;
      if (a == 0) {
        if (b == j) {
          cnt += 1;
        } else {
          cnt += 2;
        }
      }
    }
  }

  cout << cnt << endl;
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
int main() {
  // 整数の入力
  int N;
  int cnt = 0;
  int a, b;
  cin >> N;
  for (int i = 1; i < N; i++) {
    for (int j = 1; j * j <= N - i; j++) {
      a = (N - i) % j;
      b = (N - i) / j;
      // cout << i <<" "<< j <<" " << (N-i)%j<< endl;
      if (a == 0) {
        if (b == j) {
          cnt += 1;
        } else {
          cnt += 2;
        }
      }
    }
  }

  cout << cnt << endl;
  return 0;
}
Problem p02548 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long N;
  cin >> N;
  int count = 0;
  for (long long i = 1; i < N; i++) {
    int a = sqrt(N - i);
    for (int j = 1; j <= a; j++) {
      if ((N - i) % j == 0) {
        long long b = (N - i) / j;
        if (b == j) {
          count++;
        } else {
          count++;
          count++;
        }
      }
    }
  }
  cout << count << endl;
}

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

int main() {
  long long N;
  cin >> N;
  int count = 0;
  for (int i = 1; i <= N - 1; i++) {
    count += (N - 1) / i;
  }
  cout << count << endl;
}
Problem p02548 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()
#define pb push_back
#define eb emplace_back
using namespace std;
const long long INF = 1LL << 60; // 仮想的な無限大の値;
using ll = long long;
using P = pair<int, int>;
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// const int mod = 1000000007;

vector<pair<long long, long long>> prime_factorize(long long N) {
  vector<pair<long long, long long>> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;
    long long ex = 0; // 指数

    // 割れる限り割り続ける
    while (N % a == 0) {
      ++ex;
      N /= a;
    }

    // その結果を push
    res.push_back({a, ex});
  }

  // 最後に残った数について
  if (N != 1)
    res.push_back({N, 1});
  return res;
}

int main() {
  int N;
  cin >> N;
  int ans = 0;
  rep2(i, 1, N) {
    int x = i;
    if (x == 1) {
      ++ans;
      continue;
    }
    // if(x <= 3){
    //     ans += 2;continue;
    // }
    ans += 2;
    for (ll a = 2; a * a <= x; ++a) {
      if (x % a != 0)
        continue;
      if (a * a == x) {
        ++ans;
        continue;
      }
      ans += 2;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()
#define pb push_back
#define eb emplace_back
using namespace std;
const long long INF = 1LL << 60; // 仮想的な無限大の値;
using ll = long long;
using P = pair<int, int>;
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// const int mod = 1000000007;

vector<pair<long long, long long>> prime_factorize(long long N) {
  vector<pair<long long, long long>> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;
    long long ex = 0; // 指数

    // 割れる限り割り続ける
    while (N % a == 0) {
      ++ex;
      N /= a;
    }

    // その結果を push
    res.push_back({a, ex});
  }

  // 最後に残った数について
  if (N != 1)
    res.push_back({N, 1});
  return res;
}

int main() {
  int N;
  cin >> N;
  int ans = 0;
  rep2(i, 1, N) {
    int a = N / i;
    int b = N % i;
    if (b != 0) {
      ans += a;
      // if(a >=i) --ans;
    } else {
      --a;
      ans += a;
      // if(a >=i) --ans;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using ll = long long;
const ll mod = 998244353;
using namespace std;

#define int ll
struct node {
  ll val = 0;
  ll lz = 0;
} st[1 << 19];

void push(int p, int l = 0, int r = 0) {
  st[p].val %= mod;
  st[p].val += st[p].lz * (r - l + 1);
  st[p].val %= mod;
  st[p * 2].lz += st[p].lz;
  st[p * 2].lz %= mod;
  st[p * 2 + 1].lz += st[p].lz;
  st[p * 2 + 1].lz %= mod;
  st[p].lz = 0;
}
void update(int p, int l, int r, int i, int j, ll val) {
  push(p);
  if (r < i || j < l)
    return;
  if (i <= l && r <= j) {
    st[p].lz = val;
    push(p, l, r);
  } else {
    int m = (l + r) / 2;
    update(p * 2, l, m, i, j, val);
    update(p * 2 + 1, m + 1, r, i, j, val);
  }
}

int qry(int p, int l, int r, int i) {
  push(p);
  if (l == r) {
    return st[p].val;
  } else {
    int m = (l + r) / 2;
    if (i <= m) {
      return qry(p * 2, l, m, i);
    } else {
      return qry(p * 2 + 1, m + 1, r, i);
    }
  }
}

signed main() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> segs;
  while (k--) {
    int l, r;
    cin >> l >> r;
    segs.emplace_back(l, r);
  }
  update(1, 0, n - 1, 0, 0, 1);
  for (int i = 0; i < n - 1; i++) {
    for (auto p : segs) {
      int l, r;
      l = p.first;
      r = p.second;
      if (i + l < n) {
        update(1, 0, n - 1, i + l, min(n - 1, i + r),
               qry(1, 0, n - 1, i) % mod);
      }
    }
  }
  cout << qry(1, 0, n - 1, n - 1);
}

Fixed submission:
#include <bits/stdc++.h>

using ll = long long;
const ll mod = 998244353;
using namespace std;

#define int ll
struct node {
  ll val = 0;
  ll lz = 0;
} st[8000001];

void push(int p, int l = 0, int r = 0) {
  st[p].val %= mod;
  st[p].val += st[p].lz * (r - l + 1);
  st[p].val %= mod;
  st[p * 2].lz += st[p].lz;
  st[p * 2].lz %= mod;
  st[p * 2 + 1].lz += st[p].lz;
  st[p * 2 + 1].lz %= mod;
  st[p].lz = 0;
}
void update(int p, int l, int r, int i, int j, ll val) {
  push(p);
  if (r < i || j < l)
    return;
  if (i <= l && r <= j) {
    st[p].lz = val;
    push(p, l, r);
  } else {
    int m = (l + r) / 2;
    update(p * 2, l, m, i, j, val);
    update(p * 2 + 1, m + 1, r, i, j, val);
  }
}

int qry(int p, int l, int r, int i) {
  push(p);
  if (l == r) {
    return st[p].val;
  } else {
    int m = (l + r) / 2;
    if (i <= m) {
      return qry(p * 2, l, m, i);
    } else {
      return qry(p * 2 + 1, m + 1, r, i);
    }
  }
}

signed main() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> segs;
  while (k--) {
    int l, r;
    cin >> l >> r;
    segs.emplace_back(l, r);
  }
  update(1, 0, n - 1, 0, 0, 1);
  for (int i = 0; i < n - 1; i++) {
    for (auto p : segs) {
      int l, r;
      l = p.first;
      r = p.second;
      if (i + l < n) {
        update(1, 0, n - 1, i + l, min(n - 1, i + r),
               qry(1, 0, n - 1, i) % mod);
      }
    }
  }
  cout << qry(1, 0, n - 1, n - 1);
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <string>
using namespace std;
typedef long long ll;

ll mod = 998244353;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> t, ans(n + 100000, {0, 0});
  for (ll _ = 0; _ < k; ++_) {
    pair<ll, ll> p;
    cin >> p.first >> p.second;
    t.push_back(p);
  }

  ll f = 0;
  for (ll i = 1; i <= n; i++) {
    f += ans[i].first;
    if (i == 1 || f > 0) {
      for (auto p : t) {
        ans[i + p.first].first =
            ans[i + p.first].first + (i == 1 ? 1 : f) % mod;
        ans[i + p.second].second =
            ans[i + p.second].second + (i == 1 ? 1 : f) % mod;
      }
    }
    f -= ans[i].second;
  }
  ll aaaa = 0;
  for (ll i = 1; i < n; i++) {
    aaaa = ((aaaa + ans[i].first) % mod - (ans[i].second)) % mod;
  }
  aaaa = (aaaa + ans[n].first) % mod;
  cout << aaaa;
}

Fixed submission:
#include <bits/stdc++.h>
#include <string>
using namespace std;
typedef long long ll;

ll mod = 998244353;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> t, ans(n + 250000, {0, 0});
  for (ll _ = 0; _ < k; ++_) {
    pair<ll, ll> p;
    cin >> p.first >> p.second;
    t.push_back(p);
  }

  ll f = 0;
  for (ll i = 1; i <= n; i++) {
    f += ans[i].first;
    if (i == 1 || f > 0) {
      for (auto p : t) {
        ans[i + p.first].first =
            ans[i + p.first].first + (i == 1 ? 1 : f) % mod;
        ans[i + p.second].second =
            ans[i + p.second].second + (i == 1 ? 1 : f) % mod;
      }
    }
    f -= ans[i].second;
  }
  ll aaaa = 0;
  for (ll i = 1; i < n; i++) {
    aaaa = ((aaaa + ans[i].first) % mod - (ans[i].second)) % mod;
  }
  aaaa = (aaaa + ans[n].first) % mod;
  cout << aaaa;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
struct _IO {
  _IO() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
  }
} _io;
typedef long long ll;
typedef long double db;

ll exgcd(ll a, ll b, ll &x, ll &y) {
  if (!b) {
    x = 1;
    y = 0;
    return a;
  }
  ll d = exgcd(b, a % b, x, y);
  ll t = x;
  x = y;
  y = t - (a / b) * y;
  return d;
}
ll getInv(ll a, ll mod) {
  ll x, y;
  return exgcd(a, mod, x, y) == 1 ? (x % mod + mod) % mod : -1;
}

const ll maxn = 2e5 + 5, mod = 998244353;
ll dp[maxn], sum[maxn];
bool used[maxn];

int main() {
  ll n, k;
  vector<ll> s;
  vector<pair<ll, ll>> seg;
  cin >> n >> k;
  while (k--) {
    ll a, b;
    cin >> a >> b;
    seg.push_back({a, b});
  }
  sort(seg.begin(), seg.end());
  dp[1] = 1;
  sum[1] = 1;
  for (ll i = 1; i <= n; ++i) {
    for (pair<ll, ll> &j : seg) {
      if (i - j.second - 1 >= 0)
        dp[i] = (dp[i] + sum[i - j.first] - sum[i - j.second - 1]) % mod;
      else
        dp[i] = (dp[i] + sum[i - j.first] - sum[0]) % mod;
    }
    sum[i] = sum[i - 1] + dp[i];
  }
  cout << dp[n] % mod << endl;
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
struct _IO {
  _IO() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
  }
} _io;
typedef long long ll;
typedef long double db;

ll exgcd(ll a, ll b, ll &x, ll &y) {
  if (!b) {
    x = 1;
    y = 0;
    return a;
  }
  ll d = exgcd(b, a % b, x, y);
  ll t = x;
  x = y;
  y = t - (a / b) * y;
  return d;
}
ll getInv(ll a, ll mod) {
  ll x, y;
  return exgcd(a, mod, x, y) == 1 ? (x % mod + mod) % mod : -1;
}

const ll maxn = 2e5 + 5, mod = 998244353;
ll dp[maxn], sum[maxn];
bool used[maxn];

int main() {
  ll n, k;
  vector<ll> s;
  vector<pair<ll, ll>> seg;
  cin >> n >> k;
  while (k--) {
    ll a, b;
    cin >> a >> b;
    seg.push_back({a, b});
  }
  sort(seg.begin(), seg.end());
  dp[1] = 1;
  sum[1] = 1;
  for (ll i = 1; i <= n; ++i) {
    for (pair<ll, ll> &j : seg) {
      if (i - j.second - 1 >= 0)
        dp[i] = (dp[i] + sum[i - j.first] - sum[i - j.second - 1]) % mod;
      else if (i - j.first >= 0)
        dp[i] = (dp[i] + sum[i - j.first] - sum[0]) % mod;
    }
    sum[i] = sum[i - 1] + dp[i];
  }
  cout << dp[n] % mod << endl;
  return 0;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(x) x.begin(), x.end()
typedef long long ll;
ll mod = 998244353;
ll inf = 1e18;
int main() {

  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  vector<ll> f(n + 5), a(n + 5);
  vector<bool> num(n + 5);
  rep(i, k) {
    cin >> l[i] >> r[i];
    for (int j = l[i]; j <= r[i]; j++)
      num[j] = true;
  }

  a[0] = 0;
  f[0] = 0;
  rep2(i, 1, n) {
    if (num[i]) {
      a[i]++;
      a[i + 1]--;
    }
    f[i] = f[i - 1] + a[i];
    f[i] %= mod;
    rep(j, k) {
      a[i + l[j]] += f[i];
      a[i + l[j]] %= mod;
      a[i + r[j] + 1] -= f[i];
      a[i + r[j] + 1] = (a[i + r[j] + 1] + mod) % mod;
    }
  }
  // rep2(i,1,n) cout << i << ' ' << a[i] << ' ' << f[i] << endl;
  cout << f[n - 1] << endl;

  return 0;
}

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(x) x.begin(), x.end()
typedef long long ll;
ll mod = 998244353;
ll inf = 1e18;
int main() {

  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  vector<ll> f(2 * n + 5), a(2 * n + 5);
  vector<bool> num(2 * n + 5);
  rep(i, k) {
    cin >> l[i] >> r[i];
    for (int j = l[i]; j <= r[i]; j++)
      num[j] = true;
  }

  a[0] = 0;
  f[0] = 0;
  rep2(i, 1, n) {
    if (num[i]) {
      a[i]++;
      a[i + 1]--;
    }
    f[i] = f[i - 1] + a[i];
    f[i] %= mod;
    rep(j, k) {
      a[i + l[j]] += f[i];
      a[i + l[j]] %= mod;
      a[i + r[j] + 1] -= f[i];
      a[i + r[j] + 1] = (a[i + r[j] + 1] + mod) % mod;
    }
  }
  // rep2(i,1,n) cout << i << ' ' << a[i] << ' ' << f[i] << endl;
  cout << f[n - 1] << endl;

  return 0;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int n, k;
int l[200005], r[200005];

int dp[200005];

int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= k; i++) {
    scanf("%d %d", &l[i], &r[i]);
  }
  dp[1] = 1;
  dp[2] = -1;
  for (int i = 1; i <= n; i++) {
    dp[i] = (dp[i] + dp[i - 1]) % mod;
    for (int j = 1; j <= k; j++) {
      dp[i + l[j]] = (dp[i + l[j]] + dp[i]) % mod;
      dp[i + r[j] + 1] = (dp[i + r[j] + 1] - dp[i] + mod) % mod;
    }
  }
  printf("%d", dp[n]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int n, k;
int l[200005], r[200005];

int dp[2000005];

int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= k; i++) {
    scanf("%d %d", &l[i], &r[i]);
  }
  dp[1] = 1;
  dp[2] = -1;
  for (int i = 1; i <= n; i++) {
    dp[i] = (dp[i] + dp[i - 1]) % mod;
    for (int j = 1; j <= k; j++) {
      dp[i + l[j]] = (dp[i + l[j]] + dp[i]) % mod;
      dp[i + r[j] + 1] = (dp[i + r[j] + 1] - dp[i] + mod) % mod;
    }
  }
  printf("%d", dp[n]);
  return 0;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
#define MOD 998244353
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> v, tmp;
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    tmp.push_back({x, y});
  }
  sort(all(tmp));
  int px = 0, py = 0;
  for (auto g : tmp) {
    if (g.ff <= py) {
      py = g.ss;
    } else {
      if (px != 0 && py != 0)
        v.push_back({px, py});
      px = g.ff;
      py = g.ss;
    }
  }
  v.push_back({px, py});
  vector<int> A(n + 1);
  A[1] = 1;
  A[2] = -1;
  for (int i = 1; i <= n; i++) {
    A[i] += A[i - 1];
    if (A[i] >= MOD)
      A[i] -= MOD;
    for (auto g : v) {
      A[i + g.ff] += A[i];
      if (A[i + g.ff] >= MOD)
        A[i + g.ff] -= MOD;
      A[i + g.ss + 1] -= A[i];
      if (A[i + g.ss + 1] < 0)
        A[i + g.ss + 1] += MOD;
    }
  }
  cout << A[n];
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
#define MOD 998244353
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> v, tmp;
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    tmp.push_back({x, y});
  }
  sort(all(tmp));
  int px = 0, py = 0;
  for (auto g : tmp) {
    if (g.ff <= py) {
      py = g.ss;
    } else {
      if (px != 0 && py != 0)
        v.push_back({px, py});
      px = g.ff;
      py = g.ss;
    }
  }
  v.push_back({px, py});
  vector<int> A(n + 1);
  A[1] = 1;
  A[2] = -1;
  for (int i = 1; i <= n; i++) {
    A[i] += A[i - 1];
    if (A[i] >= MOD)
      A[i] -= MOD;
    for (auto g : v) {
      if (i + g.ff <= n) {
        A[i + g.ff] += A[i];
        if (A[i + g.ff] >= MOD)
          A[i + g.ff] -= MOD;
      }
      if (i + g.ss + 1 <= n) {
        A[i + g.ss + 1] -= A[i];
        if (A[i + g.ss + 1] < 0)
          A[i + g.ss + 1] += MOD;
      }
    }
  }
  cout << A[n];
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
/*
                ******

        Author: Nguyen Phuc Chuong

                ******
*/

#include <bits/stdc++.h>

using namespace std;

long long a[10][2];
long long dp[200000];
const long long mod = 998244353;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    cin >> a[i][0] >> a[i][1];
  }
  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++) {
      dp[i + a[j][0]] = (dp[i + a[j][0]] + dp[i]) % mod;
      dp[i + a[j][1] + 1] = (dp[i + a[j][1] + 1] - dp[i]) % mod;
      if (dp[i + a[j][1] + 1] < 0) {
        dp[i + a[j][1] + 1] += mod;
      }
    }
  }
  cout << dp[n - 1];
  return 0;
}


Fixed submission:
/*
                ******

        Author: Nguyen Phuc Chuong

                ******
*/

#include <bits/stdc++.h>

using namespace std;

long long a[10][2];
long long dp[200000];
const long long mod = 998244353;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    cin >> a[i][0] >> a[i][1];
  }
  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 + a[j][0] < n) {
        dp[i + a[j][0]] = (dp[i + a[j][0]] + dp[i]) % mod;
      }
      if (i + a[j][1] + 1 < n) {
        dp[i + a[j][1] + 1] = (dp[i + a[j][1] + 1] - dp[i]) % mod;
        if (dp[i + a[j][1] + 1] < 0) {
          dp[i + a[j][1] + 1] += mod;
        }
      }
    }
  }
  cout << dp[n - 1];
  return 0;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <set>

#define N_MAX (2 * 1000)
#define MOD (998244353)

unsigned int dp[N_MAX];

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

  int L[10], R[10];
  for (int i = 0; i < K; i++) {
    std::cin >> L[i] >> R[i];
  }
  dp[0] = 1;
  for (int i = 1; i < N; i++) {
    dp[i] = dp[i - 1];
    for (int k = 0; k < K; k++) {
      if (i - L[k] >= 0 && i - R[k] - 1 >= 0) {
        dp[i] += (MOD + dp[i - L[k]] - dp[i - R[k] - 1]) % MOD;
      } else if (i - L[k] >= 0) {
        dp[i] += dp[i - L[k]];
      }
      dp[i] %= MOD;
    }
  }
  std::cout << (MOD + dp[N - 1] - dp[N - 2]) % MOD << std::endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <set>

#define N_MAX (2 * 100000)
#define MOD (998244353)

unsigned int dp[N_MAX];

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

  int L[10], R[10];
  for (int i = 0; i < K; i++) {
    std::cin >> L[i] >> R[i];
  }
  dp[0] = 1;
  for (int i = 1; i < N; i++) {
    dp[i] = dp[i - 1];
    for (int k = 0; k < K; k++) {
      if (i - L[k] >= 0 && i - R[k] - 1 >= 0) {
        dp[i] += (MOD + dp[i - L[k]] - dp[i - R[k] - 1]) % MOD;
      } else if (i - L[k] >= 0) {
        dp[i] += dp[i - L[k]];
      }
      dp[i] %= MOD;
    }
  }
  std::cout << (MOD + dp[N - 1] - dp[N - 2]) % MOD << std::endl;
  return 0;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const long long MOD = 998244353;
long long N, K, l[11], r[11], memo[200010], prefix[400010];

void Input() {
  cin >> N >> K;
  for (long long i = 1; i <= K; i++) {
    cin >> l[i] >> r[i];
  }
}

void Solve() {
  memset(prefix, 0, sizeof prefix);
  prefix[N] = 1;
  memo[N] = 1;
  for (long long i = N - 1; i >= 1; i--) {
    long long total = 0;
    for (long long j = 1; j <= K; j++) {
      long long nl = l[i], nr = r[i];
      total += (prefix[i + nl] - prefix[i + nr + 1]) % MOD;
      total = total % MOD;
    }
    memo[i] = total;
    prefix[i] = (prefix[i + 1] + total) % MOD;
  }
  cout << memo[1] << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  Input();
  Solve();
}

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

const long long MOD = 998244353;
long long N, K, l[11], r[11], memo[200010], prefix[400010];

void Input() {
  cin >> N >> K;
  for (long long i = 1; i <= K; i++) {
    cin >> l[i] >> r[i];
  }
}

void Solve() {
  memset(prefix, 0, sizeof prefix);
  prefix[N] = 1;
  memo[N] = 1;
  for (long long i = N - 1; i >= 1; i--) {
    long long total = 0;
    for (long long j = 1; j <= K; j++) {
      long long nl = l[j], nr = r[j];
      total += (prefix[i + nl] - prefix[i + nr + 1] + MOD) % MOD;
      total = total % MOD;
    }
    memo[i] = total;
    prefix[i] = (prefix[i + 1] + total) % MOD;
  }
  cout << memo[1] << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  Input();
  Solve();
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
using ll = long long;
const ll mod = 998244353;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  rep(i, k) { cin >> l[i] >> r[i]; }
  vector<int> dp(n + 10);
  dp[1] = 1;
  for (int i = 1; i < n; i++) {
    dp[i] += dp[i - 1];
    dp[i] %= mod;
    rep(j, k) {
      int num = min(i + r[j] + 1, (int)(n + 1));
      dp[i + l[j]] += dp[i];
      dp[i + l[j]] %= mod;
      dp[num] -= dp[i];
      if (dp[num] < 0)
        dp[num] += mod;
    }
  }
  dp[n] %= mod;
  cout << dp[n] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
using ll = long long;
const ll mod = 998244353;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  rep(i, k) { cin >> l[i] >> r[i]; }
  vector<int> dp(n + 10);
  dp[1] = 1;
  for (int i = 1; i < n; i++) {
    dp[i] += dp[i - 1];
    dp[i] %= mod;
    rep(j, k) {
      int num = min(i + r[j] + 1, (int)(n + 1));
      int nu = min(i + l[j], (int)(n + 1));
      dp[nu] += dp[i];
      dp[nu] %= mod;
      dp[num] -= dp[i];
      if (dp[num] < 0)
        dp[num] += mod;
    }
  }
  dp[n] %= mod;
  cout << dp[n] << endl;
  return 0;
}
Problem p02549 (C++)

Buggy submission (status: 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(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;
}


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(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;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define debug_value(x)                                                         \
  cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x     \
       << endl;
#define debug(x)                                                               \
  cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

using namespace std;
typedef long long ll;

const ll MOD = 998244353;

int N, K;
int L[10], R[10];

ll cnt[200005];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> N >> K;
  for (int i = 0; i < K; i++) {
    cin >> L[i] >> R[i];
  }
  cnt[0] = 1;
  cnt[1] = -1;
  for (int i = 0; i < N; i++) {
    if (i > 0)
      cnt[i] += cnt[i - 1];
    cnt[i] %= MOD;
    for (int j = 0; j < K; j++) {
      cnt[i + L[j]] += cnt[i];
      cnt[i + L[j]] %= MOD;
      cnt[i + R[j] + 1] -= cnt[i];
      cnt[i + R[j] + 1] %= MOD;
    }
  }
  cout << (cnt[N - 1] + MOD) % MOD << endl;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define debug_value(x)                                                         \
  cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x     \
       << endl;
#define debug(x)                                                               \
  cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

using namespace std;
typedef long long ll;

const ll MOD = 998244353;

int N, K;
int L[10], R[10];

ll cnt[500005];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> N >> K;
  for (int i = 0; i < K; i++) {
    cin >> L[i] >> R[i];
  }
  cnt[0] = 1;
  cnt[1] = -1;
  for (int i = 0; i < N; i++) {
    if (i > 0)
      cnt[i] += cnt[i - 1];
    cnt[i] %= MOD;
    for (int j = 0; j < K; j++) {
      cnt[i + L[j]] += cnt[i];
      cnt[i + L[j]] %= MOD;
      cnt[i + R[j] + 1] -= cnt[i];
      cnt[i + R[j] + 1] %= MOD;
    }
  }
  cout << (cnt[N - 1] + MOD) % MOD << endl;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MOD 998244353
#define N 200005
int n, k;
vector<pair<int, int>> d;
long dp[N], sumdp[N];
int dcnt = 0;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> k;
  d.reserve(k + 1);
  for (int i = 0; i < k; i++) {
    int s, e;
    cin >> s >> e;
    d.push_back({s, e});
  }
  dp[1] = 1;
  sumdp[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (const auto &sect : d) {
      if (sect.first > i)
        continue;
      int ubound = i - sect.first;
      int lbound = min(i - sect.second, i);
      dp[i] = (dp[i] + MOD + sumdp[ubound] - sumdp[lbound - 1]) % MOD;
    }
    sumdp[i] = (sumdp[i - 1] + dp[i]) % MOD;
  }
  cout << dp[n] << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 998244353
#define N 200005
int n, k;
vector<pair<int, int>> d;
long dp[N], sumdp[N];
int dcnt = 0;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> k;
  d.reserve(k + 1);
  for (int i = 0; i < k; i++) {
    int s, e;
    cin >> s >> e;
    d.push_back({s, e});
  }
  dp[1] = 1;
  sumdp[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (const auto &sect : d) {
      if (sect.first > i)
        continue;
      int ubound = i - sect.first;
      int lbound = max(i - sect.second, 1);
      dp[i] = (dp[i] + MOD + sumdp[ubound] - sumdp[lbound - 1]) % MOD;
    }
    sumdp[i] = (sumdp[i - 1] + dp[i]) % MOD;
  }
  cout << dp[n] << '\n';

  return 0;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 998244353;
const int N = 200005;

int a[N], b[N];
LL f[N], c[20];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> 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) -
              (f[i - b[j] - 1] >= 1 ? f[i - b[j] - 1] : 0);

      f[i] = (f[i] + c[j]) % mod;
    }
  }
  cout << (f[n] + mod) % mod << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 998244353;
const int N = 200005;

int a[N], b[N];
LL f[N], c[20];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> 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] + mod) % mod << endl;
  return 0;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;

const int MAXN = 1e5 + 5;
const int mod = 998244353;
LL tr[MAXN], dp[MAXN];
int n, k, L[15], R[15];
void update(int x, LL y) {
  for (; x <= n; x += (x & (-x)))
    tr[x] = (tr[x] + y) % mod;
}
LL find(int x) {
  if (!x)
    return 0;
  LL res = 0;
  for (; x; x -= (x & (-x)))
    res = (res + tr[x]) % mod;
  return res;
}

int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= k; ++i) {
    scanf("%d %d", &L[i], &R[i]);
  }
  dp[1] = 1;
  update(1, 1);
  for (int i = 2; i <= n; ++i) {
    for (int j = 1; j <= k; ++j) {
      if (i <= L[j])
        continue;
      dp[i] = (dp[i] + find(i - L[j]) - find(max(i - R[j] - 1, 0))) % mod;
      dp[i] = (dp[i] + mod) % mod;
    }
    update(i, dp[i]);
  }
  printf("%lld", dp[n]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;

const int MAXN = 2e5 + 5;
const int mod = 998244353;
LL tr[MAXN], dp[MAXN];
int n, k, L[15], R[15];
void update(int x, LL y) {
  for (; x <= n; x += (x & (-x)))
    tr[x] = (tr[x] + y) % mod;
}
LL find(int x) {
  if (!x)
    return 0;
  LL res = 0;
  for (; x; x -= (x & (-x)))
    res = (res + tr[x]) % mod;
  return res;
}

int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= k; ++i) {
    scanf("%d %d", &L[i], &R[i]);
  }
  dp[1] = 1;
  update(1, 1);
  for (int i = 2; i <= n; ++i) {
    for (int j = 1; j <= k; ++j) {
      if (i <= L[j])
        continue;
      dp[i] = (dp[i] + find(i - L[j]) - find(max(i - R[j] - 1, 0))) % mod;
      dp[i] = (dp[i] + mod) % mod;
    }
    update(i, dp[i]);
  }
  printf("%lld", dp[n]);
  return 0;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int mod = 998244353;
int n, k, lft[N], rgt[N];
int dp[N];

int add(int x, int y) {
  if (x + y >= mod) {
    return x + y - mod;
  }
  return x + y;
}

int sub(int x, int y) {
  if (x - y < 0) {
    return x - y + mod;
  }
  return x - y;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;

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

  dp[1] = 1;
  for (int i = 1; i <= n; i++) {
    if (i > 2) {
      dp[i] = add(dp[i], dp[i - 1]);
    }

    for (int j = 0; j < k; j++) {
      int l = i + lft[j];
      int r = i + rgt[j] + 1;

      dp[l] = add(dp[l], dp[i]);
      dp[r] = sub(dp[r], dp[i]);
    }
  }

  cout << dp[n] << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int mod = 998244353;
int n, k, lft[N], rgt[N];
int dp[N];

int add(int x, int y) {
  if (x + y >= mod) {
    return x + y - mod;
  }
  return x + y;
}

int sub(int x, int y) {
  if (x - y < 0) {
    return x - y + mod;
  }
  return x - y;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;

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

  dp[1] = 1;
  for (int i = 1; i <= n; i++) {
    if (i > 2) {
      dp[i] = add(dp[i], dp[i - 1]);
    }

    for (int j = 0; j < k; j++) {
      int l = i + lft[j];
      int r = i + rgt[j] + 1;

      if (l < N) {
        dp[l] = add(dp[l], dp[i]);
      }
      if (r < N) {
        dp[r] = sub(dp[r], dp[i]);
      }
    }
  }

  cout << dp[n] << '\n';

  return 0;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
static const ll mod = 998244353;
ll N, K;
ll dp[100005];
ll sum[100005];
int main() {
  cin >> N >> K;
  vector<P> A(K);
  for (ll i = 0; i < K; i++) {
    ll l, r;
    cin >> l >> r;
    P p(l, r);
    A[i] = p;
  }
  dp[0] = 1;
  sum[0] = 1;
  for (ll i = 1; i < N; i++) {
    for (ll j = 0; j < K; j++) {
      ll l = A[j].first;
      ll r = A[j].second;
      if (i - l < 0) {
      } else if (0 <= i - l && 0 <= i - r - 1) {
        dp[i] = (dp[i] + sum[i - l] - sum[i - r - 1] + 2 * mod) % mod;
      } else
        dp[i] = (dp[i] + sum[i - l]) % mod;
    }
    sum[i] = (sum[i - 1] + dp[i]) % mod;
  }
  cout << dp[N - 1] << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
static const ll mod = 998244353;
ll N, K;
ll dp[200005];
ll sum[200005];
int main() {
  cin >> N >> K;
  vector<P> A(K);
  for (ll i = 0; i < K; i++) {
    ll l, r;
    cin >> l >> r;
    P p(l, r);
    A[i] = p;
  }
  dp[0] = 1;
  sum[0] = 1;
  for (ll i = 1; i < N; i++) {
    for (ll j = 0; j < K; j++) {
      ll l = A[j].first;
      ll r = A[j].second;
      if (i - l < 0) {
      } else if (0 <= i - l && 0 <= i - r - 1) {
        dp[i] = (dp[i] + sum[i - l] - sum[i - r - 1] + 2 * mod) % mod;
      } else
        dp[i] = (dp[i] + sum[i - l]) % mod;
    }
    sum[i] = (sum[i - 1] + dp[i]) % mod;
  }
  cout << dp[N - 1] << endl;

  return 0;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
const ll MOD = 998244353;
const long double PI = 3.14159265358979;
const ll MAX = 0;

ll DP[200020];

int main() {

  ll N, K;
  cin >> N >> K;
  ll L[K];
  ll R[K];
  rep(i, K) { cin >> L[i] >> R[i]; }
  DP[0] = 1;
  DP[1] = -1;
  rep(i, N) {
    rep(j, K) {
      DP[i + L[j]] += DP[i];
      DP[i + L[j]] %= MOD;
      DP[i + R[j] + 1] -= DP[i];
      DP[i + R[j] + 1] %= MOD;
    }
    DP[i + 1] += DP[i];
    DP[i + 1] %= MOD;
  }
  while (DP[N - 1] < 0) {
    DP[N - 1] += MOD;
  }
  cout << DP[N - 1];
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
const ll MOD = 998244353;
const long double PI = 3.14159265358979;
const ll MAX = 0;

ll DP[600020];

int main() {

  ll N, K;
  cin >> N >> K;
  ll L[K];
  ll R[K];
  rep(i, K) { cin >> L[i] >> R[i]; }
  DP[0] = 1;
  DP[1] = -1;
  rep(i, N) {
    rep(j, K) {
      DP[i + L[j]] += DP[i];
      DP[i + L[j]] %= MOD;
      DP[i + R[j] + 1] -= DP[i];
      DP[i + R[j] + 1] %= MOD;
    }
    DP[i + 1] += DP[i];
    DP[i + 1] %= MOD;
  }
  while (DP[N - 1] < 0) {
    DP[N - 1] += MOD;
  }
  cout << DP[N - 1];
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
long long dp[200001];
long long pre[200001];
int main() {
  long long n, k;
  cin >> n >> k;
  vector<pair<long long, long long>> S;
  for (long long i = 0; i < k; i++) {
    long long l, r;
    cin >> l >> r;
    S.push_back({l, r});
  }
  dp[0] = 0;
  dp[1] = 1;
  dp[2] = -1;
  for (int i = 1; i <= n; i++) {
    dp[i] = (dp[i] % mod + dp[i - 1] % mod) % mod;
    for (int j = 0; j < k; j++) {
      int l = S[j].first;
      int r = S[j].second;
      if (i + l <= n) {
        dp[i + l] += dp[i];
        dp[i + r + 1] %= mod;
      }
      if (i + r + 1 <= n) {
        dp[i + r + 1] = (dp[i + r + 1] - dp[i] + mod) % mod;
        dp[i + r + 1] %= mod;
      }
    }
  }
  cout << dp[n] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
long long dp[200001];
long long pre[200001];
int main() {
  long long n, k;
  cin >> n >> k;
  vector<pair<long long, long long>> S;
  for (long long i = 0; i < k; i++) {
    long long l, r;
    cin >> l >> r;
    S.push_back({l, r});
  }
  dp[0] = 0;
  dp[1] = 1;
  dp[2] = -1;
  for (int i = 1; i <= n; i++) {
    dp[i] = (dp[i] % mod + dp[i - 1] % mod) % mod;
    for (int j = 0; j < k; j++) {
      int l = S[j].first;
      int r = S[j].second;
      if (i + l <= n) {
        dp[i + l] += dp[i];
        dp[i + l] %= mod;
      }
      if (i + r + 1 <= n) {
        dp[i + r + 1] = (dp[i + r + 1] - dp[i] + mod) % mod;
        dp[i + r + 1] %= mod;
      }
    }
  }
  cout << dp[n] << endl;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

const ll MOD = 998244353;

int main() {

  ll n, k;
  cin >> n >> k;
  vector<pair<int, int>> imos(k);
  rep(i, k) {
    int L, R;
    cin >> L >> R;
    imos[i] = make_pair(L, R + 1);
  }
  vector<ll> dp(500000, 0);
  dp[1] = 1;
  dp[2] = -1;
  rep(i, 500000) {
    if (i > 0)
      dp[i] += dp[i - 1];
    dp[i] %= MOD;
    rep(j, k) {
      int L = imos[j].first;
      int R = imos[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] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

const ll MOD = 998244353;

int main() {

  ll n, k;
  cin >> n >> k;
  vector<pair<int, int>> imos(k);
  rep(i, k) {
    int L, R;
    cin >> L >> R;
    imos[i] = make_pair(L, R + 1);
  }
  vector<ll> dp(500000, 0);
  dp[1] = 1;
  dp[2] = -1;
  rep(i, 300000) {
    if (i > 0)
      dp[i] += dp[i - 1];
    dp[i] %= MOD;
    rep(j, k) {
      int L = imos[j].first;
      int R = imos[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] << endl;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int64_t Q = 998244353;

int main() {
  int N, K;
  cin >> N >> K;
  vector<pair<int, int>> p(K);
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    p[i] = make_pair(a, b);
  }
  sort(p.begin(), p.end());
  vector<int64_t> dp(N + 1);
  vector<int64_t> rui(N + 1);
  dp[1] = 1;
  rui[1] = 1;
  for (int i = 2; i <= N; i++) {
    int64_t A = 0;
    for (int j = 0; j < K; j++) {
      int a, b;
      tie(a, b) = p[j];
      if (i <= a) {
        break;
      }
      A += rui[i - a] - rui[max(i - b - 1, 0)];
      A += Q;
      A %= Q;
    }
    dp[i] = A;
    rui[i] = (dp[i] + rui[i - 1]) % Q;
  }
  cout << dp[N] << endl;
}

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

int64_t Q = 998244353;

int main() {
  int N, K;
  cin >> N >> K;
  vector<pair<int, int>> p(K);
  for (int i = 0; i < K; i++) {
    int a, b;
    cin >> a >> b;
    p[i] = make_pair(a, b);
  }
  sort(p.begin(), p.end());
  vector<int64_t> dp(N + 1);
  vector<int64_t> rui(N + 1);
  dp[1] = 1;
  rui[1] = 1;
  for (int i = 2; i <= N; i++) {
    int64_t A = 0;
    for (int j = 0; j < K; j++) {
      int a, b;
      tie(a, b) = p[j];
      if (i <= a) {
        break;
      }
      A += rui[i - a] - rui[max(i - b - 1, 0)];
      A += Q;
      A %= Q;
    }
    dp[i] = A;
    rui[i] = (dp[i] + rui[i - 1]) % Q;
  }
  cout << dp[N] << endl;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 998244353;
int l[10], r[10];
// dp[i] = 範囲 [1, i] に到着する場合の数
ll dp[201] = {};
int main() {
  ll n, k;
  cin >> n >> k;
  for (int i = 0; i < k; ++i)
    cin >> l[i] >> r[i];
  dp[1] = 1;
  for (int i = 2; i <= n; ++i) {
    dp[i] = dp[i - 1];
    for (int j = 0; j < k; ++j) {
      int ll = max(1, i - r[j]);
      int rr = min(i - 1, i - l[j]);
      if (ll <= rr) {
        dp[i] += dp[rr] - dp[ll - 1];
      }
    }
    dp[i] = (dp[i] % MOD + MOD) % MOD;
  }
  cout << (dp[n] - dp[n - 1] + MOD) % MOD << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 998244353;
int l[10], r[10];
// dp[i] = 範囲 [1, i] に到着する場合の数
ll dp[200001] = {};
int main() {
  ll n, k;
  cin >> n >> k;
  for (int i = 0; i < k; ++i)
    cin >> l[i] >> r[i];
  dp[1] = 1;
  for (int i = 2; i <= n; ++i) {
    dp[i] = dp[i - 1];
    for (int j = 0; j < k; ++j) {
      int ll = max(1, i - r[j]);
      int rr = min(i - 1, i - l[j]);
      if (ll <= rr) {
        dp[i] += dp[rr] - dp[ll - 1];
      }
    }
    dp[i] = (dp[i] % MOD + MOD) % MOD;
  }
  cout << (dp[n] - dp[n - 1] + MOD) % MOD << endl;
  return 0;
}