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

#include <bits/stdc++.h>

#define FAST_IO ios_base::sync_with_stdio(false), cin.tie(nullptr)
#define setIO(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)
#define trav(e, x) for (const auto &(e) : (x))
#define pb(x) push_back(x)
#define eb(x...) emplace_back(x)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define lc(i) 2 * i
#define rc(i) 2 * i + 1
// #define int long long
using namespace std;
using ii = pair<int, int>;
using vi = vector<int>;
using vb = vector<bool>;
using vii = vector<ii>;
using vivi = vector<vi>;

const int N = 2e5 + 5, MOD = 998244353, INF = 1e9 + 5;

int add(int x, int y) { return (((x + MOD) % MOD) + ((y + MOD) % MOD)) % MOD; }

signed main() {
  FAST_IO;
  //    setIO("input.txt", "output.txt");

  int n, m;
  cin >> n >> m;
  vii segments;
  while (m--) {
    int l, r;
    cin >> l >> r;
    segments.eb(l, r);
  }
  vi dp(n + 2, 0), delta(n + 2, 0);
  dp[1] = 1;
  for (int i = 1; i <= n; i++) {
    delta[i] = add(delta[i], delta[i - 1]);
    dp[i] = add(dp[i], delta[i]);
    for (const auto &[l, r] : segments) {
      if (i + l > n)
        continue;
      int x = min(i + l, n), y = min(i + r, n);
      delta[x] = add(delta[x], dp[i]);
      delta[y + 1] = add(delta[y + 1], -dp[i]);
    }
  }
  cout << dp[n];
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

#define FAST_IO ios_base::sync_with_stdio(false), cin.tie(nullptr)
#define setIO(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)
#define trav(e, x) for (const auto &(e) : (x))
#define pb(x) push_back(x)
#define eb(x...) emplace_back(x)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define lc(i) 2 * i
#define rc(i) 2 * i + 1
// #define int long long
using namespace std;
using ii = pair<int, int>;
using vi = vector<int>;
using vb = vector<bool>;
using vii = vector<ii>;
using vivi = vector<vi>;

const int N = 2e5 + 5, MOD = 998244353, INF = 1e9 + 5;

int add(int x, int y) { return ((x % MOD) + (y % MOD)) % MOD; }

struct SegmentTree {
  int n;
  vector<int> t, lazy;

  void init(int x) {
    n = x;
    t.assign(n, 0);
    lazy.assign(4 * n, 0);
  }

  int merge(int x, int y) { return add(x, y); }

  void push(int i, int l, int r) {
    if (lazy[i] != 0) {
      t[i] = add(t[i], lazy[i] * (r - l + 1));
      if (l != r) {
        lazy[lc(i)] = add(lazy[i], lazy[lc(i)]);
        lazy[rc(i)] = add(lazy[i], lazy[rc(i)]);
      }
      lazy[i] = 0;
    }
  }

  void modify(int i, int l, int r, int ql, int qr, int val) {
    push(i, l, r);
    if (l > qr || r < ql)
      return;
    if (l >= ql && r <= qr) {
      lazy[i] = add(lazy[i], val);
      push(i, l, r);
      return;
    }
    int mid = (l + r) / 2;
    modify(lc(i), l, mid, ql, qr, val);
    modify(rc(i), mid + 1, r, ql, qr, val);
    t[i] = merge(t[lc(i)], t[rc(i)]);
  }

  int query(int i, int l, int r, int ql, int qr) {
    push(i, l, r);
    if (l > qr || r < ql)
      return 0;
    if (l >= ql && r <= qr)
      return t[i];
    int mid = (l + r) / 2;
    int resL = query(lc(i), l, mid, ql, qr);
    int resR = query(rc(i), mid + 1, r, ql, qr);
    return merge(resL, resR);
  }

  void modify(int l, int r, int val) { modify(1, 0, n - 1, l, r, val); }

  int query(int l, int r) { return query(1, 0, n - 1, l, r); }
};

signed main() {
  FAST_IO;
  //    setIO("input.txt", "output.txt");

  int n, m;
  cin >> n >> m;
  vii segments;
  while (m--) {
    int l, r;
    cin >> l >> r;
    segments.eb(l, r);
  }
  SegmentTree seg;
  seg.init(n);
  seg.modify(1, 1, 1);
  for (int i = 1; i <= n; i++)
    for (const auto &[l, r] : segments)
      seg.modify(i + l, i + r, seg.query(i, i));
  cout << seg.query(n, n);
}