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

#include <bits/stdc++.h>

#define f(i, a, b) for (ll i = a; i < (ll)b; i++)
#define af(i, a, b) for (ll i = a; i >= b; i--)
#define rep(i, a, b, k) for (ll i = a; i < b; i += k)
#define arep(i, a, b, k) for (ll i = a; i >= b; i -= k)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz(a) (ll) a.size()
#define all(a) a.begin(), a.end()
#define sor(a) sort(a.begin(), a.end())
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define inter                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)

// policy-based

using namespace std;

typedef long long ll; // int or long long
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;

const ll MAX = 2e6 + 1;
const ll inf = 1e9;
const ll mod = 998244353;

ll tree[MAX * 2];

ll l[20], r[20], n, k;

void update(ll l, ll r, ll v) {
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      (tree[l++] += v) %= mod;
    if (r & 1)
      (tree[--r] += v) %= mod;
  }
}

ll query(ll x) {
  ll ans = 0;
  x += n;
  while (x)
    (ans += tree[x]) %= mod, x >>= 1;
  return ans;
}

int main() {
  fastio;
  cin >> n >> k;
  f(i, 0, k) cin >> l[i] >> r[i];
  update(0, 1, 1);
  f(i, 0, n) {
    ll aux = query(i);
    f(j, 0, k) update(i + l[j], i + r[j] + 1, aux);
  }
  cout << query(n - 1) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define f(i, a, b) for (ll i = a; i < (ll)b; i++)
#define af(i, a, b) for (ll i = a; i >= b; i--)
#define rep(i, a, b, k) for (ll i = a; i < b; i += k)
#define arep(i, a, b, k) for (ll i = a; i >= b; i -= k)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz(a) (ll) a.size()
#define all(a) a.begin(), a.end()
#define sor(a) sort(a.begin(), a.end())
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define inter                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)

// policy-based

using namespace std;

typedef long long ll; // int or long long
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;

const ll MAX = 2e5 + 1;
const ll inf = 1e9;
const ll mod = 998244353;

ll tree[MAX * 2];

ll l[20], r[20], n, k;

void update(ll l, ll r, ll v) {
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      (tree[l++] += v) %= mod;
    if (r & 1)
      (tree[--r] += v) %= mod;
  }
}

ll query(ll x) {
  ll ans = 0;
  x += n;
  while (x)
    (ans += tree[x]) %= mod, x >>= 1;
  return ans;
}

int main() {
  fastio;
  cin >> n >> k;
  f(i, 0, k) cin >> l[i] >> r[i];
  update(0, 1, 1);
  f(i, 0, n) {
    ll aux = query(i);
    f(j, 0, k) update(i + l[j], i + r[j] + 1, aux);
  }
  cout << query(n - 1) << endl;
  return 0;
}