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

/*
ID: Trump
TASK:
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
using namespace std;
typedef long long ll;
typedef long double lb;
// #define MARK
const ll losn = 1e6 + 5;
const ll maxn = 1e5 + 5;
const ll minn = 1e3 + 5;
const ll tiny = 1e2 + 5;
const ll inf = 1e9;
const ll mod = 1e9 + 7;
const ll hmod = 4698571;
ll u[11], v[11];
ll tot = 0;
ll mods = 998244353;
ll tree[(maxn * 2) << 2];

void push_up(ll rt) {
  tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
  tree[rt] %= mods;
}

void build(ll l, ll r, ll rt) {
  if (l == r) {
    return;
  }
  ll mid = (l + r) >> 1;
  if (l <= mid) {
    build(l, mid, rt << 1);
  }
  if (r > mid) {
    build(mid + 1, r, rt << 1 | 1);
  }
  push_up(rt);
}

ll query(ll a, ll b, ll l, ll r, ll rt) {
  if (a <= l && r <= b) {
    return tree[rt] % mods;
  }
  ll mid = (l + r) >> 1;
  ll sums = 0;
  if (a <= mid) {
    sums += query(a, b, l, mid, rt << 1);
  }
  if (b > mid) {
    sums += query(a, b, mid + 1, r, rt << 1 | 1);
  }
  sums %= mods;
  push_up(rt);
  return sums;
}

void update(ll x, ll sums, ll l, ll r, ll rt) {
  if (l == r) {
    tree[rt] = sums;
    return;
  }
  ll mid = (l + r) >> 1;
  if (x <= mid) {
    update(x, sums, l, mid, rt << 1);
  }
  if (x > mid) {
    update(x, sums, mid + 1, r, rt << 1 | 1);
  }
  push_up(rt);
}

int main() {
  // #ifdef MARK
  //     freopen("input.in", "r", stdin);
  //     freopen("ouput.out", "w", stdout);
  //     freopen("input.in", "r", stdin);
  //     freopen("ouput.out", "w", stdout);
  // #endif
  ll n, k;
  cin >> n >> k;
  for (ll i = 1; i <= k; i++) {
    cin >> u[i] >> v[i];
  }
  update(1, 1, 1, n, 1);
  for (ll i = 2; i <= n; i++) {
    ll sums = 0;
    for (ll j = 1; j <= k; j++) {
      if (u[j] >= i)
        continue;
      ll a = i - u[j], b = max((ll)1, i - v[j]);
      if (a >= 1 && b >= 1 && b <= a) {
        sums += query(b, a, 1, n, 1);
        sums %= mods;
      }
    }
    update(i, sums, 1, n, 1);
  }
  cout << query(n, n, 1, n, 1) % mods << endl;
  return 0;
}

Error: 0

Fixed submission:

/*
ID: Trump
TASK:
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
using namespace std;
typedef long long ll;
typedef long double lb;
// #define MARK
const ll losn = 1e6 + 5;
const ll maxn = 1e5 + 5;
const ll minn = 1e3 + 5;
const ll tiny = 1e2 + 5;
const ll inf = 1e9;
const ll mod = 1e9 + 7;
const ll hmod = 4698571;
ll u[11], v[11];
ll tot = 0;
ll mods = 998244353;
ll tree[maxn * 2 * 4];

ll push_up(ll rt) {
  tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
  tree[rt] %= mods;
}

void build(ll l, ll r, ll rt) {
  if (l == r) {
    return;
  }
  ll mid = (l + r) >> 1;
  if (l <= mid) {
    build(l, mid, rt << 1);
  }
  if (r > mid) {
    build(mid + 1, r, rt << 1 | 1);
  }
  push_up(rt);
}

ll query(ll a, ll b, ll l, ll r, ll rt) {
  if (a <= l && r <= b) {
    return tree[rt] % mods;
  }
  ll mid = (l + r) >> 1;
  ll sums = 0;
  if (a <= mid) {
    sums += query(a, b, l, mid, rt << 1);
  }
  if (b > mid) {
    sums += query(a, b, mid + 1, r, rt << 1 | 1);
  }
  sums %= mods;
  push_up(rt);
  return sums;
}

void update(ll x, ll sums, ll l, ll r, ll rt) {
  if (l == r) {
    tree[rt] = sums;
    return;
  }
  ll mid = (l + r) >> 1;
  if (x <= mid) {
    update(x, sums, l, mid, rt << 1);
  } else {
    update(x, sums, mid + 1, r, rt << 1 | 1);
  }
  push_up(rt);
}

int main() {
  // #ifdef MARK
  //     freopen("input.in", "r", stdin);
  //     freopen("ouput.out", "w", stdout);
  //     freopen("input.in", "r", stdin);
  //     freopen("ouput.out", "w", stdout);
  // #endif
  ll n, k;
  cin >> n >> k;
  for (ll i = 1; i <= k; i++) {
    cin >> u[i] >> v[i];
  }
  update(1, 1, 1, n, 1);
  for (ll i = 2; i <= n; i++) {
    ll sums = 0;
    for (ll j = 1; j <= k; j++) {
      if (u[j] >= i)
        continue;
      ll a = i - u[j], b = max((ll)1, i - v[j]);
      if (a >= 1 && b >= 1 && b <= a) {
        sums += query(b, a, 1, n, 1);
        sums %= mods;
      }
    }
    update(i, sums, 1, n, 1);
  }
  cout << query(n, n, 1, n, 1) % mods << endl;
  return 0;
}