Problem p02549 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
// #include <ext/rope>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
// template <typename T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>; #define Find order_of_key #define at
// find_by_order
#define x first
#define y second
#define ll long long
#define lll __int128
#define ull unsigned long long
#define ulll unsigned __int128
#define pii pair<int, int>
#define pb push_back
#define eb emplace_back
#define gu getchar_unlocked()
#define pu putchar_unlocked()
#define all(x) x.begin(), x.end()
#define endl '\n'

const int mod = 998244353;

const static int mx = 200010;
ll BIT[mx];

void update(ll pos, ll val) {
  for (; pos <= mx; pos += pos & (-pos))
    BIT[pos] += val;
}
ll query(ll pos) {
  ll sum = 0;
  for (; pos > 0; pos -= pos & (-pos))
    sum += BIT[pos];
  return sum;
}

int k;
int L[15], R[15];
int n;
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> L[i] >> R[i];
  }
  update(n, 1);
  for (int i = n - 1; i >= 1; i--) {
    int d = n - i;
    ll ans = 0;
    for (int j = 0; j < k; j++) {
      int l = i + L[j];
      int r = min(i + R[j], n);
      ans += query(r);
      if (l != 1)
        ans -= query(l - 1);
      ans %= mod;
    }
    if (ans < 0)
      ans += mod;
    update(i, ans);
  }
  cout << query(1) << endl;
  return 0;
}

Fixed submission:
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
// #include <ext/rope>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
// template <typename T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>; #define Find order_of_key #define at
// find_by_order
#define x first
#define y second
#define ll long long
#define lll __int128
#define ull unsigned long long
#define ulll unsigned __int128
#define pii pair<int, int>
#define pb push_back
#define eb emplace_back
#define gu getchar_unlocked()
#define pu putchar_unlocked()
#define all(x) x.begin(), x.end()
#define endl '\n'

const int mod = 998244353;

const static int mx = 200010;
ll BIT[mx];

void update(ll pos, ll val) {
  for (; pos <= mx; pos += pos & (-pos))
    BIT[pos] += val;
}
ll query(ll pos) {
  ll sum = 0;
  for (; pos > 0; pos -= pos & (-pos))
    sum += BIT[pos];
  return sum;
}

int k;
int L[15], R[15];
int n;
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> L[i] >> R[i];
  }
  update(n, 1);
  for (int i = n - 1; i >= 1; i--) {
    int d = n - i;
    ll ans = 0;
    for (int j = 0; j < k; j++) {
      int l = i + L[j];
      int r = min(i + R[j], n);
      if (l <= r) {
        ans += query(r);
        if (l != 1)
          ans -= query(l - 1);
      }
      ans %= mod;
    }
    if (ans < 0)
      ans += mod;
    update(i, ans);
  }
  cout << query(1) << endl;
  return 0;
}