Problem p03823 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long giant;
inline char nchar() {
  static const int bufl = 1 << 20;
  static char buf[bufl], *a = NULL, *b = NULL;
  return a == b && (b = (a = buf) + fread(buf, 1, bufl, stdin), a == b) ? EOF
                                                                        : *a++;
}
template <class T> inline T read() {
  T x = 0, f = 1;
  char c = nchar();
  for (; !isdigit(c); c = nchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = nchar())
    x = x * 10 + c - '0';
  return x * f;
}
const int maxn = 1e5 + 1;
const int q = 1e9 + 7;
const giant inf = 2e18;
inline int Plus(int x, int y) { return (x += y) >= q ? x - q : x; }
inline void Pe(int &x, int y) { x = Plus(x, y); }
int n, f[maxn][2];
giant lim[2], a[maxn];
namespace sgt {
const int maxp = 8e6;
struct node {
  int l, r, s;
} t[maxp];
int tot = 0;
inline void newnode(int &x) {
  int y = x;
  t[x = ++tot] = t[y];
}
void modify(int &x, int l, int r, int p, int d) {
  newnode(x);
  if (l == r) {
    t[x].s = d;
    return;
  }
  int mid = l + r >> 1;
  p <= mid ? modify(t[x].l, l, mid, p, d) : modify(t[x].r, mid + 1, r, p, d);
  t[x].s = Plus(t[t[x].l].s, t[t[x].r].s);
}
inline void modify(int &x, int p, int d) { modify(x, 0, n, p, d); }
int sum(int x, int L, int R, int l, int r) {
  if (!x)
    return 0;
  if (L == l && R == r)
    return t[x].s;
  int mid = L + R >> 1;
  if (r <= mid)
    return sum(t[x].l, L, mid, l, r);
  if (l > mid)
    return sum(t[x].r, mid + 1, R, l, r);
  return Plus(sum(t[x].l, L, mid, l, mid), sum(t[x].r, mid + 1, R, mid + 1, r));
}
inline int sum(int x, int l, int r) { return sum(x, 0, n, l, r); }
} // namespace sgt
int main() {
#ifndef ONLINE_JUDGE
  freopen("test.in", "r", stdin);
#endif
  n = read<int>(), lim[0] = read<giant>(), lim[1] = read<giant>();
  a[0] = -inf, generate(a + 1, a + n + 1, read<giant>);
  for (int i = 0; i < 2; ++i)
    sgt::modify(f[1][i], 0, 1);
  for (int x = 2; x <= n; ++x)
    for (int c = 0; c < 2; ++c) {
      if (a[x] - a[x - 1] >= lim[c])
        f[x][c] = f[x - 1][c];
      int j = upper_bound(a, a + x, a[x] - lim[c]) - a - 1;
      sgt::modify(f[x][c], x - 1, sgt::sum(f[x - 1][c ^ 1], 0, j));
    }
  int ans = Plus(sgt::sum(f[n][0], 0, n - 1), sgt::sum(f[n][1], 0, n - 1));
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long giant;
inline char nchar() {
  static const int bufl = 1 << 20;
  static char buf[bufl], *a = NULL, *b = NULL;
  return a == b && (b = (a = buf) + fread(buf, 1, bufl, stdin), a == b) ? EOF
                                                                        : *a++;
}
template <class T> inline T read() {
  T x = 0, f = 1;
  char c = nchar();
  for (; !isdigit(c); c = nchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = nchar())
    x = x * 10 + c - '0';
  return x * f;
}
const int maxn = 1e5 + 1;
const int q = 1e9 + 7;
const giant inf = 2e18;
inline int Plus(int x, int y) { return (x += y) >= q ? x - q : x; }
inline void Pe(int &x, int y) { x = Plus(x, y); }
int n, f[maxn][2];
giant lim[2], a[maxn];
namespace sgt {
const int maxp = 8e6;
struct node {
  int l, r, s;
} t[maxp];
int tot = 0;
inline void newnode(int &x) {
  int y = x;
  t[x = ++tot] = t[y];
}
void modify(int &x, int l, int r, int p, int d) {
  newnode(x);
  if (l == r) {
    t[x].s = d;
    return;
  }
  int mid = l + r >> 1;
  p <= mid ? modify(t[x].l, l, mid, p, d) : modify(t[x].r, mid + 1, r, p, d);
  t[x].s = Plus(t[t[x].l].s, t[t[x].r].s);
}
inline void modify(int &x, int p, int d) { modify(x, 0, n, p, d); }
int sum(int x, int L, int R, int l, int r) {
  if (!x)
    return 0;
  if (L == l && R == r)
    return t[x].s;
  int mid = L + R >> 1;
  if (r <= mid)
    return sum(t[x].l, L, mid, l, r);
  if (l > mid)
    return sum(t[x].r, mid + 1, R, l, r);
  return Plus(sum(t[x].l, L, mid, l, mid), sum(t[x].r, mid + 1, R, mid + 1, r));
}
inline int sum(int x, int l, int r) { return sum(x, 0, n, l, r); }
} // namespace sgt
int main() {
  n = read<int>(), lim[0] = read<giant>(), lim[1] = read<giant>();
  a[0] = -inf, generate(a + 1, a + n + 1, read<giant>);
  for (int i = 0; i < 2; ++i)
    sgt::modify(f[1][i], 0, 1);
  for (int x = 2; x <= n; ++x)
    for (int c = 0; c < 2; ++c) {
      if (a[x] - a[x - 1] >= lim[c])
        f[x][c] = f[x - 1][c];
      int j = upper_bound(a, a + x, a[x] - lim[c]) - a - 1;
      sgt::modify(f[x][c], x - 1, sgt::sum(f[x - 1][c ^ 1], 0, j));
    }
  int ans = Plus(sgt::sum(f[n][0], 0, n - 1), sgt::sum(f[n][1], 0, n - 1));
  cout << ans << endl;
  return 0;
}