Problem p03301 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
#define lowbit(x) ((x) & -(x))
typedef long long ll;
const int N = 3e5 + 5, M = 3e6 + 5;
const ll inf = 1ll << 40;
ll a[N], s[N], f[N], g[N], b[N];
int n, m, rk[N];
template <class T> T abs(T x) { return x < 0 ? -x : x; }
template <class T> bool chkmax(T &a, T b) {
  return a < b ? a = b, true : false;
}
template <class T> T min(T a, T b) { return a < b ? a : b; }
template <class T> T div2(T x) { return (x - (x & 1)) / 2; }
inline void init() {
  std::reverse(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    s[i] = s[i - 1] + ((i & 1) ? -a[i] : a[i]);
}
struct node {
  node *c[2];
  int size;
  ll v1, v2, key, x1, x2;
  node();
  void pushup();
} Tnull, *null = &Tnull, pl[M], *rt[N];
int pp;
node::node() {
  c[0] = c[1] = null;
  v1 = v2 = x1 = x2 = key = size = 0;
}
void node::pushup() {
  size = c[0]->size + c[1]->size + 1;
  x1 = v1, x2 = v2;
  if (c[0] != null)
    chkmax(x1, c[0]->x1), chkmax(x2, c[0]->x2);
  if (c[1] != null)
    chkmax(x1, c[1]->x1), chkmax(x2, c[1]->x2);
}
void rot(node *&x, bool f) {
  node *y = x->c[f];
  x->c[f] = y->c[f ^ 1], y->c[f] = x;
  x->pushup(), x = y, x->pushup();
}
void insert(node *&x, node *y) {
  if (x == null) {
    x = y, x->c[0] = x->c[1] = null, x->pushup();
    return;
  }
  bool f = x->key < y->key;
  insert(x->c[f], y);
  if (x->c[f]->size > (x->size + 1) * 0.75)
    rot(x, f);
  else
    x->pushup();
}
ll ask(node *x, ll cut) {
  if (x == null)
    return -inf;
  ll res = -inf;
  if (x->key <= cut) {
    chkmax(res, x->v1 - cut);
    if (x->c[0] != null)
      chkmax(res, x->c[0]->x1 - cut);
    chkmax(res, ask(x->c[1], cut));
  } else {
    chkmax(res, x->v2 + cut);
    if (x->c[1] != null)
      chkmax(res, x->c[1]->x2 + cut);
    chkmax(res, ask(x->c[0], cut));
  }
  return res;
}
inline void calc(ll *f) {
  int i, x;
  for (m = 0, i = 1; i <= n; i++)
    b[++m] = s[i];
  std::sort(b + 1, b + m + 1), m = std::unique(b + 1, b + m + 1) - b - 1;
  for (i = 1; i <= n; i++)
    rk[i] = m - (std::lower_bound(b + 1, b + m + 1, s[i]) - b) + 1;
  pp = 0;
  for (i = 1; i <= m; i++)
    rt[i] = null;
  for (i = 1; i <= n; i += 2) {
    f[i] = abs(s[i]);
    for (x = rk[i] - 1; x; x -= lowbit(x))
      chkmax(f[i], ask(rt[x], s[i]));
    if (i < n) {
      for (x = rk[i + 1]; x <= m; x += lowbit(x)) {
        node *z = pl + (pp++);
        z->key = div2(2 * s[i + 1] + s[i] - f[i]);
        z->v1 = 2 * s[i + 1], z->v2 = f[i] - s[i];
        insert(rt[x], z);
      }
    }
  }
}
int main() {
  int i;
  scanf("%d", &n);
  ll dif = 0, sum = 0;
  for (i = 1; i <= n; i++)
    scanf("%lld", a + i), dif += (i & 1) ? a[i] : -a[i], sum += a[i];
  dif = abs(dif);
  if (n & 1) {
    std::reverse(a + 1, a + n + 1);
    init(), calc(f), init(), calc(g), init();
    std::reverse(g + 1, g + n + 1);
    for (i = 2; i < n; i += 2)
      chkmax(dif, a[i] + min(s[i - 1] + g[i + 1], f[i - 1] + s[n] - s[i]));
  }
  ll sigma = (sum - dif) >> 1, sugim = sigma + dif;
  printf("%lld %lld\n", sugim, sigma);
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
#define lowbit(x) ((x) & -(x))
typedef long long ll;
const int N = 3e5 + 5, M = 3e6 + 5;
const ll inf = 1ll << 40;
ll a[N], s[N], f[N], g[N], b[N];
int n, m, rk[N];
template <class T> T abs(T x) { return x < 0 ? -x : x; }
template <class T> bool chkmax(T &a, T b) {
  return a < b ? a = b, true : false;
}
template <class T> T min(T a, T b) { return a < b ? a : b; }
template <class T> T div2(T x) { return (x - (x & 1)) / 2; }
inline void init() {
  std::reverse(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    s[i] = s[i - 1] + ((i & 1) ? -a[i] : a[i]);
}
struct node {
  node *c[2];
  int size;
  ll v1, v2, key, x1, x2;
  node();
  void pushup();
} Tnull, *null = &Tnull, pl[M], *rt[N];
int pp;
node::node() {
  c[0] = c[1] = null;
  v1 = v2 = x1 = x2 = key = size = 0;
}
void node::pushup() {
  size = c[0]->size + c[1]->size + 1;
  x1 = v1, x2 = v2;
  if (c[0] != null)
    chkmax(x1, c[0]->x1), chkmax(x2, c[0]->x2);
  if (c[1] != null)
    chkmax(x1, c[1]->x1), chkmax(x2, c[1]->x2);
}
void rot(node *&x, bool f) {
  node *y = x->c[f];
  x->c[f] = y->c[f ^ 1], y->c[f ^ 1] = x;
  x->pushup(), x = y, x->pushup();
}
void insert(node *&x, node *y) {
  if (x == null) {
    x = y, x->c[0] = x->c[1] = null, x->pushup();
    return;
  }
  bool f = x->key < y->key;
  insert(x->c[f], y);
  if (x->c[f]->size > (x->size + 1) * 0.75)
    rot(x, f);
  else
    x->pushup();
}
ll ask(node *x, ll cut) {
  if (x == null)
    return -inf;
  ll res = -inf;
  if (x->key <= cut) {
    chkmax(res, x->v1 - cut);
    if (x->c[0] != null)
      chkmax(res, x->c[0]->x1 - cut);
    chkmax(res, ask(x->c[1], cut));
  } else {
    chkmax(res, x->v2 + cut);
    if (x->c[1] != null)
      chkmax(res, x->c[1]->x2 + cut);
    chkmax(res, ask(x->c[0], cut));
  }
  return res;
}
inline void calc(ll *f) {
  int i, x;
  for (m = 0, i = 1; i <= n; i++)
    b[++m] = s[i];
  std::sort(b + 1, b + m + 1), m = std::unique(b + 1, b + m + 1) - b - 1;
  for (i = 1; i <= n; i++)
    rk[i] = m - (std::lower_bound(b + 1, b + m + 1, s[i]) - b) + 1;
  pp = 0;
  for (i = 1; i <= m; i++)
    rt[i] = null;
  for (i = 1; i <= n; i += 2) {
    f[i] = abs(s[i]);
    for (x = rk[i] - 1; x; x -= lowbit(x))
      chkmax(f[i], ask(rt[x], s[i]));
    if (i < n) {
      for (x = rk[i + 1]; x <= m; x += lowbit(x)) {
        node *z = pl + (pp++);
        z->key = div2(2 * s[i + 1] + s[i] - f[i]);
        z->v1 = 2 * s[i + 1], z->v2 = f[i] - s[i];
        insert(rt[x], z);
      }
    }
  }
}
int main() {
  int i;
  scanf("%d", &n);
  ll dif = 0, sum = 0;
  for (i = 1; i <= n; i++)
    scanf("%lld", a + i), dif += (i & 1) ? a[i] : -a[i], sum += a[i];
  dif = abs(dif);
  if (n & 1) {
    std::reverse(a + 1, a + n + 1);
    init(), calc(f), init(), calc(g), init();
    std::reverse(g + 1, g + n + 1);
    for (i = 2; i < n; i += 2)
      chkmax(dif, a[i] + min(s[i - 1] + g[i + 1], f[i - 1] + s[n] - s[i]));
  }
  ll sigma = (sum - dif) >> 1, sugim = sigma + dif;
  printf("%lld %lld\n", sugim, sigma);
}