Problem p02758 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \
  do {                                                                         \
  } while (0)
#define dump(x)                                                                \
  do {                                                                         \
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

constexpr int MOD = 998244353;

// auto mod int
struct mint {
  LL x;
  mint(LL x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(const LL &t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime MOD
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
  LL operator()() const { return x; }
};
using VM = vector<mint>;
using VVM = vector<VM>;

struct S {
  int x, d;
};

template <typename T = long long> class SegmentTree {
public:
  std::vector<T> ary;
  int n;
  const T default_value = -1 * (1 << 30);
  SegmentTree(int n) {
    this->n = 1;
    while (this->n < n)
      this->n *= 2;
    ary.resize(this->n * 2 - 1, default_value); // sum -> 0, min -> INF, 0?
  }
  T ope(T l, T r) { return std::max(l, r); }
  void update(int k, T d) {
    k += n - 1;
    ary[k] = d;
    while (k > 0) {
      k = (k - 1) / 2;
      ary[k] = ope(ary[2 * k + 1], ary[2 * k + 2]);
    }
  }
  T query(int a, int b) { return query(a, b, 0, n, 0); }
  T query(int a, int b, int l, int r, int i) {
    if (a <= l && b >= r)
      return ary[i];
    // if (a >= r || b <= l) return 0; // sum
    if (a >= r || b <= l)
      return default_value; // min
    int m = (l + r) / 2;
    T vl = query(a, b, l, m, 2 * i + 1);
    T vr = query(a, b, m, r, 2 * i + 2);
    return ope(vl, vr);
  }
};

void solve() {
  int n;
  cin >> n;

  vector<S> ss(n);
  REP(i, 0, n) cin >> ss[i].x >> ss[i].d;
  sort(all(ss), [](auto &l, auto &r) { return l.x < r.x; });

  SegmentTree<int> sg(n);
  REP(i, 0, n) sg.update(i, ss[i].x + ss[i].d);

  vector<mint> dp(n), ru(n + 1);
  ru[n] = 1;
  RREP(i, 0, n) {
    S tmps;
    tmps.x = ss[i].x + ss[i].d - 1;

    int prevd = -1, d;
    auto it =
        upper_bound(all(ss), tmps, [](auto &l, auto &r) { return l.x < r.x; });
    prevd = distance(ss.begin(), it);
    // if (i == 0) dump(prevd);
    for (;;) {
      int mv = sg.query(i, prevd);
      // if (i == 0) dump(mv);
      tmps.x = mv - 1;
      auto it = upper_bound(all(ss), tmps,
                            [](auto &l, auto &r) { return l.x < r.x; });
      d = distance(ss.begin(), it);
      if (d == prevd)
        break;
      prevd = d;
    }

    dp[i] = ru[d];
    ru[i] = ru[i + 1] + dp[i];
  }
  cout << ru[0]() << endl;

  return;

  REP(i, 0, n) { cout << dp[i]() << " "; }
  cout << endl;
  REP(i, 0, n + 1) { cout << ru[i]() << " "; }
  cout << endl;
}

signed main() {
  solve();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \
  do {                                                                         \
  } while (0)
#define dump(x)                                                                \
  do {                                                                         \
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

constexpr int MOD = 998244353;

// auto mod int
struct mint {
  LL x;
  mint(LL x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(const LL &t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime MOD
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
  LL operator()() const { return x; }
};
using VM = vector<mint>;
using VVM = vector<VM>;

struct S {
  int x, d;
};

template <typename T = long long> class SegmentTree {
public:
  std::vector<T> ary;
  int n;
  const T default_value = -1 * (1 << 30);
  SegmentTree(int n) {
    this->n = 1;
    while (this->n < n)
      this->n *= 2;
    ary.resize(this->n * 2 - 1, default_value); // sum -> 0, min -> INF, 0?
  }
  T ope(T l, T r) { return std::max(l, r); }
  void update(int k, T d) {
    k += n - 1;
    ary[k] = d;
    while (k > 0) {
      k = (k - 1) / 2;
      ary[k] = ope(ary[2 * k + 1], ary[2 * k + 2]);
    }
  }
  T query(int a, int b) { return query(a, b, 0, n, 0); }
  T query(int a, int b, int l, int r, int i) {
    if (a <= l && b >= r)
      return ary[i];
    // if (a >= r || b <= l) return 0; // sum
    if (a >= r || b <= l)
      return default_value; // min
    int m = (l + r) / 2;
    T vl = query(a, b, l, m, 2 * i + 1);
    T vr = query(a, b, m, r, 2 * i + 2);
    return ope(vl, vr);
  }
};

void solve() {
  int n;
  cin >> n;

  vector<S> ss(n);
  REP(i, 0, n) cin >> ss[i].x >> ss[i].d;
  sort(all(ss), [](auto &l, auto &r) { return l.x < r.x; });

  SegmentTree<int> sg(n);
  REP(i, 0, n) sg.update(i, ss[i].x + ss[i].d);

  vector<mint> dp(n), ru(n + 1);
  ru[n] = 1;
  RREP(i, 0, n) {
    S tmps;
    tmps.x = ss[i].x + ss[i].d - 1;

    int prevd = -1, d;
    auto it =
        upper_bound(all(ss), tmps, [](auto &l, auto &r) { return l.x < r.x; });
    prevd = distance(ss.begin(), it);
    // if (i == 0) dump(prevd);
    for (;;) {
      int mv = sg.query(i, prevd);
      // if (i == 0) dump(mv);
      tmps.x = mv - 1;
      auto it = upper_bound(all(ss), tmps,
                            [](auto &l, auto &r) { return l.x < r.x; });
      d = distance(ss.begin(), it);
      if (d == prevd)
        break;
      prevd = d;
    }
    sg.update(i, max(ss[prevd].x, ss[i].x + ss[i].d));

    dp[i] = ru[d];
    ru[i] = ru[i + 1] + dp[i];
  }
  cout << ru[0]() << endl;

  return;

  REP(i, 0, n) { cout << dp[i]() << " "; }
  cout << endl;
  REP(i, 0, n + 1) { cout << ru[i]() << " "; }
  cout << endl;
}

signed main() {
  solve();

  return 0;
}