Problem p03510 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define VARNAME(x) #x
#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "sz:" << v.size() << "\n[";
  for (const auto &p : v) {
    os << p << ",";
  }
  os << "]\n";
  return os;
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v) {
    is >> e;
  }
  return is;
}

template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

constexpr ll MOD = (ll)1e9 + 7LL;

template <typename T> constexpr T INF = numeric_limits<T>::max() / 10;

template <typename Base> class SparseTable {
public:
  using T = typename Base::T;
  using SemiLattice = Base;
  SparseTable(const vector<T> &val) : size(val.size()), lg2(size + 1, 0) {
    for (int i = 2; i <= size; i++) {
      lg2[i] = lg2[i / 2] + 1;
    }
    table.resize(size, vector<T>(lg2[size] + 1));
    for (int i = 0; i < size; i++) {
      table[i][0] = val[i];
    }
    for (int j = 0; j < lg2[size]; j++) {
      const int w = 1 << j;
      for (int i = 0; i <= size - (w << 1); i++) {
        T tl = table[i][j], tr = table[i + w][j];
        table[i][j + 1] = op(tl, tr);
      }
    }
  }

  T accumulate(const int l, const int r) const {
    assert(0 <= l and l < r and r <= size);
    const int j = lg2[r - l];
    return op(table[l][j], table[r - (1 << j)][j]);
  }

private:
  const int size;
  vector<int> lg2;
  vector<vector<T>> table;
  const SemiLattice op{};
};
struct Max {
  using T = ll;
  T operator()(const T &a, const T &b) const { return max(a, b); }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<ll> x(N + 1, 0);
  vector<ll> s(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    cin >> x[i] >> s[i];
  }
  vector<ll> t = s;
  for (int i = 1; i <= N; i++) {
    t[i] += t[i - 1];
  }
  for (int i = 0; i <= N; i++) {
    t[i] -= x[i];
  }

  SparseTable<Max> st{t};

  ll maximum = -INF<ll>;
  for (int i = 0; i <= N; i++) {
    for (int j = i + 1; j <= N; j++) {
      maximum =
          max(maximum, st.accumulate(i + 1, j + 1) - t[i] - (x[i] - x[i + 1]));
    }
  }
  cout << maximum << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define VARNAME(x) #x
#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "sz:" << v.size() << "\n[";
  for (const auto &p : v) {
    os << p << ",";
  }
  os << "]\n";
  return os;
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v) {
    is >> e;
  }
  return is;
}

template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

constexpr ll MOD = (ll)1e9 + 7LL;

template <typename T> constexpr T INF = numeric_limits<T>::max() / 10;

template <typename Base> class SparseTable {
public:
  using T = typename Base::T;
  using SemiLattice = Base;
  SparseTable(const vector<T> &val) : size(val.size()), lg2(size + 1, 0) {
    for (int i = 2; i <= size; i++) {
      lg2[i] = lg2[i / 2] + 1;
    }
    table.resize(size, vector<T>(lg2[size] + 1));
    for (int i = 0; i < size; i++) {
      table[i][0] = val[i];
    }
    for (int j = 0; j < lg2[size]; j++) {
      const int w = 1 << j;
      for (int i = 0; i <= size - (w << 1); i++) {
        T tl = table[i][j], tr = table[i + w][j];
        table[i][j + 1] = op(tl, tr);
      }
    }
  }

  T accumulate(const int l, const int r) const {
    assert(0 <= l and l < r and r <= size);
    const int j = lg2[r - l];
    return op(table[l][j], table[r - (1 << j)][j]);
  }

private:
  const int size;
  vector<int> lg2;
  vector<vector<T>> table;
  const SemiLattice op{};
};
struct Max {
  using T = ll;
  T operator()(const T &a, const T &b) const { return max(a, b); }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<ll> x(N + 1, 0);
  vector<ll> s(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    cin >> x[i] >> s[i];
  }
  vector<ll> t = s;
  for (int i = 1; i <= N; i++) {
    t[i] += t[i - 1];
  }
  for (int i = 0; i <= N; i++) {
    t[i] -= x[i];
  }

  SparseTable<Max> st{t};

  ll maximum = -INF<ll>;
  for (int i = 0; i < N; i++) {
    maximum =
        max(maximum, st.accumulate(i + 1, N + 1) - t[i] - (x[i] - x[i + 1]));
  }
  cout << maximum << endl;

  return 0;
}