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

#include <bits/stdc++.h>
#define pb push_back
#define SZ(x) ((int)(x.size()))
#define FOR(i, s, n) for (ll i = (s); (i) < (n); ++i)
#define FORD(i, s, l) for (ll i = (s); (i) >= l; --i)
#define F first
#define S second
#define TC                                                                     \\
  int __tc;                                                                    \\
  cin >> __tc;                                                                 \\
  FOR(case_num, 1, __tc + 1)
#define TEST(x, i) ((x) & (1ll << (i)))
#define SET(x, i) ((x) | (1ll << (i)))
#define FLIP(x, i) ((x) ^ (1ll << (i)))
#define CLEAR(x, i) ((x) & ~(1ll << (i)))
const double pi = 4 * atan(1);
using namespace std;
typedef long long ll;

template <typename T> void print_vec(const vector<T> &v) {
  for (int i = 0; i < SZ(v); i++) {
    if (i)
      cout << " ";
    cout << v[i];
  }
  cout << "\\n";
}

inline ll floorDiv(ll x, ll y) {
  ll d = x / y;
  ll r = x % y;
  return r ? (d - ((x < 0) ^ (y < 0))) : d;
}

ll ceilDiv(ll x, ll y) { return -floorDiv(-x, y); }

bool predicate(ll mid) { return mid > 10; }

ll bin_search(ll lo, ll hi) {
  int sign = lo < hi ? 1 : -1;
  lo *= sign;
  hi *= sign;
  hi++;
  while (lo < hi) {
    ll mid = floorDiv(lo + hi, 2);
    if (!predicate(sign * mid)) {
      lo = mid + 1;
    } else {
      hi = mid;
    }
  }
  return sign * lo;
}
const ll MOD = 998244353;
const int MAXN = 200005;

typedef vector<ll> vll;
typedef vector<int> vi;
#define LSOne(S) ((S) & -(S))

class FenwickTree { // index 0 is not used
private:
  vll ft; // internal FT is an array
public:
  FenwickTree(int m) { ft.assign(m + 1, 0); } // create an empty FT

  ll rsq(int j) { // returns RSQ(1, j)
    ll sum = 0;
    for (; j; j -= LSOne(j)) {
      sum += ft[j];
      sum = ((sum % MOD) + MOD) % MOD;
    }
    return sum;
  }

  ll rsq(int i, int j) {
    return (((rsq(j) - rsq(i - 1)) % MOD) + MOD) % MOD;
  } // inc/exclusion

  // updates value of the i-th element by v (v can be +ve/inc or -ve/dec)
  void update(int i, ll v) {
    for (; i < (int)ft.size(); i += LSOne(i)) {
      ft[i] += v;
      ft[i] = ((ft[i] % MOD) + MOD) % MOD;
    }
  }
};

class RUPQ { // RUPQ variant
private:
  FenwickTree ft; // internally use PURQ FT
public:
  RUPQ(int m) : ft(FenwickTree(m)) {}
  void range_update(int ui, int uj, ll v) {
    ft.update(ui, v);                         // [ui, ui+1, .., m] +v
    ft.update(uj + 1, -v);                    // [uj+1, uj+2, .., m] -v
  }                                           // [ui, ui+1, .., uj] +v
  ll point_query(int i) { return ft.rsq(i); } // rsq(i) is sufficient
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> ranges;
  while (k--) {
    int a, b;
    cin >> a >> b;
    ranges.emplace_back(a, b);
  }
  RUPQ ft(n);
  ft.range_update(1, 1, 1);
  for (int i = 1; i < n; i++) {
    ll v = ft.point_query(i);
    for (auto p : ranges) {
      p.first += i;
      p.second += i;
      p.second = min(p.second, n);
      if (p.first > p.second) {
        continue;
      }
      ft.range_update(p.first, p.second, v);
    }
  }

  cout << ft.point_query(n) << "\\n";
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define SZ(x) ((int)(x.size()))
#define FOR(i, s, n) for (ll i = (s); (i) < (n); ++i)
#define FORD(i, s, l) for (ll i = (s); (i) >= l; --i)
#define F first
#define S second
#define TC                                                                     \\
  int __tc;                                                                    \\
  cin >> __tc;                                                                 \\
  FOR(case_num, 1, __tc + 1)
#define TEST(x, i) ((x) & (1ll << (i)))
#define SET(x, i) ((x) | (1ll << (i)))
#define FLIP(x, i) ((x) ^ (1ll << (i)))
#define CLEAR(x, i) ((x) & ~(1ll << (i)))
const double pi = 4 * atan(1);
using namespace std;
typedef long long ll;

template <typename T> void print_vec(const vector<T> &v) {
  for (int i = 0; i < SZ(v); i++) {
    if (i)
      cout << " ";
    cout << v[i];
  }
  cout << "\\n";
}

inline ll floorDiv(ll x, ll y) {
  ll d = x / y;
  ll r = x % y;
  return r ? (d - ((x < 0) ^ (y < 0))) : d;
}

ll ceilDiv(ll x, ll y) { return -floorDiv(-x, y); }

bool predicate(ll mid) { return mid > 10; }

ll bin_search(ll lo, ll hi) {
  int sign = lo < hi ? 1 : -1;
  lo *= sign;
  hi *= sign;
  hi++;
  while (lo < hi) {
    ll mid = floorDiv(lo + hi, 2);
    if (!predicate(sign * mid)) {
      lo = mid + 1;
    } else {
      hi = mid;
    }
  }
  return sign * lo;
}
const ll MOD = 998244353;
const int MAXN = 200005;

typedef vector<ll> vll;
typedef vector<int> vi;
#define LSOne(S) ((S) & -(S))

class FenwickTree { // index 0 is not used
private:
  vll ft; // internal FT is an array
public:
  FenwickTree(int m) { ft.assign(m + 1, 0); } // create an empty FT

  ll rsq(int j) { // returns RSQ(1, j)
    ll sum = 0;
    for (; j; j -= LSOne(j))
      sum += ft[j];
    sum %= MOD;
    return sum;
  }

  ll rsq(int i, int j) {
    return (((rsq(j) - rsq(i - 1)) % MOD) + MOD) % MOD;
  } // inc/exclusion

  // updates value of the i-th element by v (v can be +ve/inc or -ve/dec)
  void update(int i, ll v) {
    for (; i < (int)ft.size(); i += LSOne(i))
      ft[i] += v;
    ft[i] %= MOD;
  }
};

class RUPQ { // RUPQ variant
private:
  FenwickTree ft; // internally use PURQ FT
public:
  RUPQ(int m) : ft(FenwickTree(m)) {}
  void range_update(int ui, int uj, ll v) {
    ft.update(ui, v);                         // [ui, ui+1, .., m] +v
    ft.update(uj + 1, -v);                    // [uj+1, uj+2, .., m] -v
  }                                           // [ui, ui+1, .., uj] +v
  ll point_query(int i) { return ft.rsq(i); } // rsq(i) is sufficient
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> ranges;
  while (k--) {
    int a, b;
    cin >> a >> b;
    ranges.emplace_back(a, b);
  }
  RUPQ ft(n);
  ft.range_update(1, 1, 1);
  for (int i = 1; i < n; i++) {
    ll v = ft.point_query(i);
    for (auto p : ranges) {
      p.first += i;
      p.second += i;
      p.second = min(p.second, n);
      if (p.first > p.second) {
        continue;
      }
      ft.range_update(p.first, p.second, v);
    }
  }

  cout << ft.point_query(n) << "\\n";
}