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

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

#define IO                                                                     \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long
#define double long double
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vii vector<pii>
#define nl '\\n'
#define yy "YES\\n"
#define nn "NO\\n"
#define pb emplace_back
#define mkpr make_pair
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define mod 998244353

struct BIT {
  vector<int> bit; // binary indexed tree
  int n;

  BIT(int n) {
    this->n = n + 1;
    bit.assign(n + 1, 0);
  }

  int sum(int idx) {
    int ret = 0;
    if (idx <= 0) {
      return 0;
    }
    for (++idx; idx > 0; idx -= idx & -idx)
      ret += bit[idx], ret %= mod;
    return ret;
  }

  int sum(int l, int r) {
    int x = sum(r);
    int y = sum(l - 1);
    int ans = (x - y + mod) % mod;
    return ans;
  }

  void add(int idx, int delta) {
    for (++idx; idx < n; idx += idx & -idx) {
      bit[idx] += delta;
      bit[idx] %= mod;
    }
  }
};

signed main() {
  IO;
  int n, k;
  cin >> n >> k;
  BIT b(n);
  vi dp(n + 1);
  vii segs(k);
  for (int i = 0; i < k; i++) {
    cin >> segs[i].first >> segs[i].second;
  }
  sort(all(segs));
  b.add(1, 1);
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      int l = segs[j].first, r = segs[j].second;
      int needl = max(0LL, i - r), needr = max(0LL, i - l);
      dp[i] += b.sum(needl, needr);
      dp[i] %= mod;
    }
    b.add(i, dp[i]);
  }
  cout << dp[n];
}


Error: WA

Fixed submission:

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

#define IO                                                                     \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long
#define double long double
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vii vector<pii>
#define nl '\\n'
#define yy "YES\\n"
#define nn "NO\\n"
#define pb emplace_back
#define mkpr make_pair
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define mod 998244353

struct BIT {
  vector<int> bit; // binary indexed tree
  int n;

  BIT(int n) {
    this->n = n + 1;
    bit.assign(n + 1, 0);
  }

  int sum(int idx) {
    int ret = 0;
    if (idx <= 0) {
      return 0;
    }
    for (++idx; idx > 0; idx -= idx & -idx)
      ret += bit[idx], ret %= mod;
    return ret;
  }

  int sum(int l, int r) {
    int x = sum(r);
    int y = sum(l - 1);
    int ans = (x - y + mod) % mod;
    return ans;
  }

  void add(int idx, int delta) {
    for (++idx; idx < n; idx += idx & -idx)
      bit[idx] += delta;
    bit[idx] %= mod;
  }
};

signed main() {
  IO;
  int n, k;
  cin >> n >> k;
  BIT b(n);
  vi dp(n + 1);
  vii segs(k);
  for (int i = 0; i < k; i++) {
    cin >> segs[i].first >> segs[i].second;
  }
  sort(all(segs));
  b.add(1, 1);
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      int l = segs[j].first, r = segs[j].second;
      int needl = max(0LL, i - r), needr = max(0LL, i - l);
      dp[i] += b.sum(needl, needr);
      dp[i] %= mod;
    }
    b.add(i, dp[i]);
  }
  cout << dp[n];
}