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

#include <bits/stdc++.h>

#define FLASH                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define int long long int
#define MAX 1e9
#define MIN -1e9
#define mod 998244353
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << '=' << (x) << endl;
#define output(x) cout << (x ? "Yes" : "No") << '\\n';
#define mxn 100005
using namespace std;

void solve() {
  int n, k;
  cin >> n >> k;
  int l[k], r[k];
  for (int i = 0; i < k; i++) {
    cin >> l[i] >> r[i];
  }
  int dp[n + 5] = {};
  int pref[n + 5] = {};
  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 val = (pref[i - left] - pref[i - right - 1] + mod) % mod;

      dp[i] = (dp[i] + val) % mod;
    }
    pref[i] = (pref[i - 1] % mod + dp[i] % mod) % mod;
  }
  cout << dp[n];
}

int32_t main() {
  FLASH
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:


#include <bits/stdc++.h>
#define ll long long int
#define all(x) x.begin(), x.end()
using namespace std;
ll power(ll x, ll y, ll m) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2, m) % m;
  p = (p * p) % m;

  return (y % 2 == 0) ? p : (x * p) % m;
}
ll modInverse(ll a, ll m) {
  ll g = __gcd(a, m);
  if (g != 1)
    cout << "Inverse doesn't exist";

  // If a and m are relatively prime, then modulo inverse
  // is a^(m-2) mode m

  return power(a, m - 2, m);
}

ll fact[1000];
void fac(ll m) {
  ll i;
  fact[0] = 1;
  for (i = 1; i < 1000; i++) {
    fact[i] = (i * fact[i - 1]) % m;
  }
}

ll comb(ll n, ll k, ll m) {
  return (fact[n] * modInverse(fact[k], m) % m * modInverse(fact[n - k], m) %
          m) %
         m;
}
bool cmp(pair<ll, ll> a, pair<ll, ll> b) {
  if (a.second == b.second)
    return a.first < b.first;
  return a.second > b.second;
}
void SieveOfEratosthenes(ll n) {
  // Create a boolean array "prime[0..n]" and initialize
  // all entries it as true. A value in prime[i] will
  // finally be false if i is Not a prime, else true.
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));

  for (ll p = 2; p * p <= n; p++) {
    // If prime[p] is not changed, then it is a prime
    if (prime[p] == true) {
      // Update all multiples of p greater than or
      // equal to the square of it
      // numbers which are multiple of p and are
      // less than p^2 are already been marked.
      for (ll i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
}
/*ll f(ll a)
{
ll i;ll res=1;ll c;
for(i=2;i*i<=a;i++)
{
        if(a%i==0)
        {
                c=0;
                while(a%i==0)
                {
                        c++;
                        a=a/i;
                }

        }
        if(c>0)
        {
                res*=(c+1);
        }
}
        if(a>1)
        {
                res*=2;
        }

        return res;
}*/
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, k;
  cin >> n >> k;
  ll mod = 998244353;
  ll i;
  ll pre[100001] = {0};
  ll dp[100001] = {0};
  vector<pair<ll, ll>> a;
  for (i = 0; i < k; i++) {
    ll l, r;
    cin >> l >> r;
    a.push_back({l, r});
  }
  sort(all(a));
  dp[1] = 1;
  pre[1] = 1;
  ll j;
  for (i = 2; i <= n; i++) {
    for (j = 0; j < k; j++) {
      if (a[j].first > i) {
        continue;
      } else if (a[j].second < i) {
        dp[i] = (dp[i] + pre[i - a[j].first] - pre[i - a[j].second - 1] + mod) %
                mod;
      } else {
        dp[i] = (dp[i] + pre[i - a[j].first] - pre[0] + mod) % mod;
      }
    }
    pre[i] = (pre[i - 1] + dp[i]) % mod;
  }

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