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

#include <bits/stdc++.h>
#define MOD 998244353
#define ll long long
using namespace std;
ll ff[200100];
#define MAXN 200010
int x[200100], y[200100];
unsigned ll n, m, ans[MAXN << 2], tag[MAXN << 2];
inline ll ls(ll x) { return x << 1; }
inline ll rs(ll x) { return x << 1 | 1; }
inline void push_up(ll p) { ans[p] = ans[ls(p)] + ans[rs(p)]; }
void build(ll p, ll l, ll r) {
  tag[p] = 0;
  if (l == r) {
    ans[p] = ff[l];
    return;
  }
  ll mid = (l + r) >> 1;
  build(ls(p), l, mid);
  build(rs(p), mid + 1, r);
  push_up(p);
}
inline void f(ll p, ll l, ll r, ll k) {
  tag[p] = (tag[p] + k) % MOD;
  ans[p] = ans[p] % MOD + k % MOD * (r - l + 1) % MOD;
}
inline void push_down(ll p, ll l, ll r) {
  ll mid = (l + r) >> 1;
  f(ls(p), l, mid, tag[p]);
  f(rs(p), mid + 1, r, tag[p]);
  tag[p] = 0;
}
inline void update(ll nl, ll nr, ll l, ll r, ll p, ll k) {
  if (nl <= l && r <= nr) {
    ans[p] += (k * (r - l + 1) % MOD);
    tag[p] += k % MOD;
    return;
  }
  push_down(p, l, r);
  ll mid = (l + r) >> 1;
  if (nl <= mid)
    update(nl, nr, l, mid, ls(p), k);
  if (nr > mid)
    update(nl, nr, mid + 1, r, rs(p), k);
  push_up(p);
}
ll query(ll q_x, ll q_y, ll l, ll r, ll p) {
  if (q_x + q_y == 0)
    return 0;
  ll res = 0;
  if (q_x <= l && r <= q_y)
    return ans[p];
  ll mid = (l + r) >> 1;
  push_down(p, l, r);
  if (q_x <= mid)
    res += query(q_x, q_y, l, mid, ls(p));
  if (q_y > mid)
    res += query(q_x, q_y, mid + 1, r, rs(p));
  return res;
}
int main() {
  int n, m;
  cin >> n >> m;
  memset(ff, 0, sizeof ff);
  ff[1] = 1;
  ff[0] = 0;
  build(1, 1, n);
  for (int i = 1; i <= m; i++) {
    int p, q;
    cin >> p >> q;
    x[i] = p, y[i] = q;
  }

  for (int i = 2; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      ll g = max(0, i - x[j]), yy = max(0, i - y[j]);
      ll ans = (query(yy, g, 1, n, 1)) % MOD;
      update(i, i, 1, n, 1, ans);
    }
  }
  cout << (query(n, n, 1, n, 1) % MOD);
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define MOD 998244353
#define ll long long
using namespace std;
ll ff[200100];
#define MAXN 100010
int x[200100], y[200100];
unsigned ll n, m, ans[MAXN << 2], tag[MAXN << 2];
inline ll ls(ll x) { return x << 1; }
inline ll rs(ll x) { return x << 1 | 1; }
inline void push_up(ll p) { ans[p] = ans[ls(p)] + ans[rs(p)]; }
void build(ll p, ll l, ll r) {
  tag[p] = 0;
  if (l == r) {
    ans[p] = ff[l];
    return;
  }
  ll mid = (l + r) >> 1;
  build(ls(p), l, mid);
  build(rs(p), mid + 1, r);
  push_up(p);
}
inline void f(ll p, ll l, ll r, ll k) {
  tag[p] = (tag[p] + k) % MOD;
  ans[p] = ans[p] % MOD + k % MOD * (r - l + 1) % MOD;
}
inline void push_down(ll p, ll l, ll r) {
  ll mid = (l + r) >> 1;
  f(ls(p), l, mid, tag[p]);
  f(rs(p), mid + 1, r, tag[p]);
  tag[p] = 0;
}
inline void update(ll nl, ll nr, ll l, ll r, ll p, ll k) {
  if (nl <= l && r <= nr) {
    ans[p] += (k * (r - l + 1) % MOD);
    tag[p] += k % MOD;
    return;
  }
  push_down(p, l, r);
  ll mid = (l + r) >> 1;
  if (nl <= mid)
    update(nl, nr, l, mid, ls(p), k);
  if (nr > mid)
    update(nl, nr, mid + 1, r, rs(p), k);
  push_up(p);
}
ll query(ll q_x, ll q_y, ll l, ll r, ll p) {
  if (q_x + q_y == 0)
    return 0;
  ll res = 0;
  if (q_x <= l && r <= q_y)
    return ans[p];
  ll mid = (l + r) >> 1;
  push_down(p, l, r);
  if (q_x <= mid)
    res += query(q_x, q_y, l, mid, ls(p));
  if (q_y > mid)
    res += query(q_x, q_y, mid + 1, r, rs(p));
  return res;
}
int main() {
  int n, m;
  cin >> n >> m;
  memset(ff, 0, sizeof ff);
  ff[1] = 1;
  ff[0] = 0;
  build(1, 1, n);
  for (int i = 1; i <= m; i++) {
    int p, q;
    cin >> p >> q;
    x[i] = p, y[i] = q;
  }

  for (int i = 2; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      ll g = max(0, i - x[j]), yy = max(0, i - y[j]);
      ll ans = (query(yy, g, 1, n, 1)) % MOD;
      update(i, i, 1, n, 1, ans);
    }
  }
  cout << (query(n, n, 1, n, 1) % MOD);
  return 0;
}