Problem p02758 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

struct dor {
  dor *l, *r;
  int sd;

  void pp() {
    if (l == NULL && r == NULL)
      ;
    else if (l == NULL)
      sd = r->sd;
    else if (r == NULL)
      sd = l->sd;
    else
      sd = max(l->sd, r->sd);
  }
} *rt;

const int LN = -1e9, NR = 1e9, mod = 998244353;

int M(long long l, long long r) {
  int m = (l + r) / 2;
  if (m == r)
    --m;
  return m;
}

int si, vl;
void rgnk(int l, int r, dor *&t) {
  if (t == NULL)
    t = new dor();
  if (l == r)
    t->sd = vl;
  else if (M(l, r) >= si)
    rgnk(l, M(l, r), t->l);
  else
    rgnk(M(l, r) + 1, r, t->r);
  t->pp();
}

void gnk(int Si, int Vl) {
  si = Si, vl = Vl;
  rgnk(LN, NR, rt);
}

int sl, sr;
int rsgm(int l, int r, dor *&t) {
  if (l > sr || r < sl || t == NULL)
    return LN;
  if (l >= sl && r <= sr)
    return t->sd;
  return max(rsgm(l, M(l, r), t->l), rsgm(M(l, r) + 1, r, t->r));
}

int sgm(int l, int r) {
  sl = l, sr = r;
  return rsgm(LN, NR, rt);
}

int n, x, d;
vector<pair<int, int>> v, ve;
set<pair<int, int>> s;
long long fp;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  freopen("12.txt", "r", stdin);
  v.push_back((*new pair<int, int>()));
  ve.push_back((*new pair<int, int>()));
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> x >> d;
    v.push_back({x, x + d - 1});
  }
  sort(v.begin() + 1, v.end());
  reverse(v.begin() + 1, v.end());
  for (int i = 1; i <= n; ++i) {
    int tp = max(v[i].second, sgm(v[i].first, v[i].second));
    gnk(v[i].first, tp);
    ve.push_back({v[i].first, tp});
  }
  for (int i = 1; i <= n; ++i) {
    long long tp = 1;
    while (s.size() && s.begin()->first <= ve[i].second) {
      tp *= s.begin()->second;
      tp %= mod;
      s.erase(s.begin());
    }
    ++tp;
    tp = (tp == mod ? 0 : tp);
    s.insert({ve[i].first, tp});
  }
  fp = 1;
  while (s.size()) {
    fp *= s.begin()->second;
    fp %= mod;
    s.erase(s.begin());
  }
  cout << fp << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

struct dor {
  dor *l, *r;
  int sd;

  void pp() {
    if (l == NULL && r == NULL)
      ;
    else if (l == NULL)
      sd = r->sd;
    else if (r == NULL)
      sd = l->sd;
    else
      sd = max(l->sd, r->sd);
  }
} *rt;

const int LN = -1e9, NR = 1e9, mod = 998244353;

int M(long long l, long long r) {
  int m = (l + r) / 2;
  if (m == r)
    --m;
  return m;
}

int si, vl;
void rgnk(int l, int r, dor *&t) {
  if (t == NULL)
    t = new dor();
  if (l == r)
    t->sd = vl;
  else if (M(l, r) >= si)
    rgnk(l, M(l, r), t->l);
  else
    rgnk(M(l, r) + 1, r, t->r);
  t->pp();
}

void gnk(int Si, int Vl) {
  si = Si, vl = Vl;
  rgnk(LN, NR, rt);
}

int sl, sr;
int rsgm(int l, int r, dor *&t) {
  if (l > sr || r < sl || t == NULL)
    return LN;
  if (l >= sl && r <= sr)
    return t->sd;
  return max(rsgm(l, M(l, r), t->l), rsgm(M(l, r) + 1, r, t->r));
}

int sgm(int l, int r) {
  sl = l, sr = r;
  return rsgm(LN, NR, rt);
}

int n, x, d;
vector<pair<int, int>> v, ve;
set<pair<int, int>> s;
long long fp;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  v.push_back((*new pair<int, int>()));
  ve.push_back((*new pair<int, int>()));
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> x >> d;
    v.push_back({x, x + d - 1});
  }
  sort(v.begin() + 1, v.end());
  reverse(v.begin() + 1, v.end());
  for (int i = 1; i <= n; ++i) {
    int tp = max(v[i].second, sgm(v[i].first, v[i].second));
    gnk(v[i].first, tp);
    ve.push_back({v[i].first, tp});
  }
  for (int i = 1; i <= n; ++i) {
    long long tp = 1;
    while (s.size() && s.begin()->first <= ve[i].second) {
      tp *= s.begin()->second;
      tp %= mod;
      s.erase(s.begin());
    }
    ++tp;
    tp = (tp == mod ? 0 : tp);
    s.insert({ve[i].first, tp});
  }
  fp = 1;
  while (s.size()) {
    fp *= s.begin()->second;
    fp %= mod;
    s.erase(s.begin());
  }
  cout << fp << endl;
  return 0;
}