Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define MAX 200005
#define mod 998244353
ll tree[MAX] = {0};
ll lazy[MAX] = {0};
void updateRangeUtil(ll si, ll ss, ll se, ll us, ll ue, ll diff) {
  if (lazy[si] != 0) {
    tree[si] += (se - ss + 1) * lazy[si];
    tree[si] %= mod;
    if (ss != se) {
      lazy[si * 2 + 1] += lazy[si];
      lazy[si * 2 + 1] %= mod;
      lazy[si * 2 + 2] += lazy[si];
      lazy[si * 2 + 2] %= mod;
    }
    lazy[si] = 0;
  }
  if (ss > se || ss > ue || se < us)
    return;
  if (ss >= us && se <= ue) {
    tree[si] += (se - ss + 1) * diff;
    tree[si] %= mod;
    if (ss != se) {
      lazy[si * 2 + 1] += diff;
      lazy[si * 2 + 1] %= mod;
      lazy[si * 2 + 2] += diff;
      lazy[si * 2 + 2] %= mod;
    }
    return;
  }
  ll mid = (ss + se) / 2;
  updateRangeUtil(si * 2 + 1, ss, mid, us, ue, diff);
  updateRangeUtil(si * 2 + 2, mid + 1, se, us, ue, diff);
  tree[si] = tree[si * 2 + 1] + tree[si * 2 + 2];
  tree[si] %= mod;
}
void updateRange(ll n, ll us, ll ue, ll diff) {
  updateRangeUtil(0, 0, n - 1, us, ue, diff);
}
ll getSumUtil(ll ss, ll se, ll qs, ll qe, ll si) {
  if (lazy[si] != 0) {
    tree[si] += (se - ss + 1) * lazy[si];
    tree[si] %= mod;
    if (ss != se) {
      lazy[si * 2 + 1] += lazy[si];
      lazy[si * 2 + 1] %= mod;
      lazy[si * 2 + 2] += lazy[si];
      lazy[si * 2 + 2] %= mod;
    }
    lazy[si] = 0;
  }
  if (ss > se || ss > qe || se < qs)
    return 0;
  if (ss >= qs && se <= qe)
    return tree[si];
  ll mid = (ss + se) / 2;
  ll mkp = getSumUtil(ss, mid, qs, qe, 2 * si + 1) +
           getSumUtil(mid + 1, se, qs, qe, 2 * si + 2);
  mkp %= mod;
  return mkp;
}
ll getSum(ll n, ll qs, ll qe) {
  if (qs < 0 || qe > n - 1 || qs > qe) {
    exit(78);
  }
  return getSumUtil(0, n - 1, qs, qe, 0);
}
void constructSTUtil(ll arr[], ll ss, ll se, ll si) {
  if (ss > se)
    return;
  if (ss == se) {
    tree[si] = arr[ss];
    return;
  }
  ll mid = (ss + se) / 2;
  constructSTUtil(arr, ss, mid, si * 2 + 1);
  constructSTUtil(arr, mid + 1, se, si * 2 + 2);
  tree[si] = tree[si * 2 + 1] + tree[si * 2 + 2];
}
void constructST(ll arr[], ll n) { constructSTUtil(arr, 0, n - 1, 0); }
int main() {
  // freopen("in", "r", stdin);
  ll n, k;
  cin >> n >> k;
  ll arr[n + 10];
  for (int i = 0; i < n + 5; i++)
    arr[i] = 0;
  arr[0] = 1;
  constructST(arr, n);
  vector<pair<ll, ll>> ami(k);
  for (auto &a : ami) {
    cin >> a.first;
    cin >> a.second;
  }
  for (int i = 0; i < n; i++) {
    for (auto a : ami) {
      if (i + a.first >= n)
        continue;
      updateRange(n, i + a.first, min(i + a.second, n - 1), getSum(n, i, i));
    }
  }
  cout << getSum(n, n - 1, n - 1) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define MAX 2000005
#define mod 998244353
ll tree[MAX] = {0};
ll lazy[MAX] = {0};
void updateRangeUtil(ll si, ll ss, ll se, ll us, ll ue, ll diff) {
  if (lazy[si] != 0) {
    tree[si] += (se - ss + 1) * lazy[si];
    tree[si] %= mod;
    if (ss != se) {
      lazy[si * 2 + 1] += lazy[si];
      lazy[si * 2 + 1] %= mod;
      lazy[si * 2 + 2] += lazy[si];
      lazy[si * 2 + 2] %= mod;
    }
    lazy[si] = 0;
  }
  if (ss > se || ss > ue || se < us)
    return;
  if (ss >= us && se <= ue) {
    tree[si] += (se - ss + 1) * diff;
    tree[si] %= mod;
    if (ss != se) {
      lazy[si * 2 + 1] += diff;
      lazy[si * 2 + 1] %= mod;
      lazy[si * 2 + 2] += diff;
      lazy[si * 2 + 2] %= mod;
    }
    return;
  }
  ll mid = (ss + se) / 2;
  updateRangeUtil(si * 2 + 1, ss, mid, us, ue, diff);
  updateRangeUtil(si * 2 + 2, mid + 1, se, us, ue, diff);
  tree[si] = tree[si * 2 + 1] + tree[si * 2 + 2];
  tree[si] %= mod;
}
void updateRange(ll n, ll us, ll ue, ll diff) {
  updateRangeUtil(0, 0, n - 1, us, ue, diff);
}
ll getSumUtil(ll ss, ll se, ll qs, ll qe, ll si) {
  if (lazy[si] != 0) {
    tree[si] += (se - ss + 1) * lazy[si];
    tree[si] %= mod;
    if (ss != se) {
      lazy[si * 2 + 1] += lazy[si];
      lazy[si * 2 + 1] %= mod;
      lazy[si * 2 + 2] += lazy[si];
      lazy[si * 2 + 2] %= mod;
    }
    lazy[si] = 0;
  }
  if (ss > se || ss > qe || se < qs)
    return 0;
  if (ss >= qs && se <= qe)
    return tree[si];
  ll mid = (ss + se) / 2;
  ll mkp = getSumUtil(ss, mid, qs, qe, 2 * si + 1) +
           getSumUtil(mid + 1, se, qs, qe, 2 * si + 2);
  mkp %= mod;
  return mkp;
}
ll getSum(ll n, ll qs, ll qe) {
  if (qs < 0 || qe > n - 1 || qs > qe) {
    exit(78);
  }
  return getSumUtil(0, n - 1, qs, qe, 0);
}
void constructSTUtil(ll arr[], ll ss, ll se, ll si) {
  if (ss > se)
    return;
  if (ss == se) {
    tree[si] = arr[ss];
    return;
  }
  ll mid = (ss + se) / 2;
  constructSTUtil(arr, ss, mid, si * 2 + 1);
  constructSTUtil(arr, mid + 1, se, si * 2 + 2);
  tree[si] = tree[si * 2 + 1] + tree[si * 2 + 2];
}
void constructST(ll arr[], ll n) { constructSTUtil(arr, 0, n - 1, 0); }
int main() {
  // freopen("in", "r", stdin);
  ll n, k;
  cin >> n >> k;
  ll arr[n + 10];
  for (int i = 0; i < n + 5; i++)
    arr[i] = 0;
  arr[0] = 1;
  constructST(arr, n);
  vector<pair<ll, ll>> ami(k);
  for (auto &a : ami) {
    cin >> a.first;
    cin >> a.second;
  }
  for (int i = 0; i < n; i++) {
    for (auto a : ami) {
      if (i + a.first >= n)
        continue;
      updateRange(n, i + a.first, min(i + a.second, n - 1), getSum(n, i, i));
    }
  }
  cout << getSum(n, n - 1, n - 1) << endl;
}