Problem p03786 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#define rep(i, j, k) for (register int i = j; i <= k; i++)
#define dow(i, j, k) for (register int i = j; i >= k; i--)
#define ull unsigned long long
#define ll long long
#define eps 1e-9
using namespace std;

inline int read() {
  int s = 0, t = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      t = -1;
    c = getchar();
  }
  while (isdigit(c))
    s = s * 10 + c - 48, c = getchar();
  return s * t;
}

const int N = 100005, M = N * 25, inf = 1e9;
ll sum[M], ql;
int v[N], c[M][2], rt, tot = 0;
#define lc c[k][0]
#define rc c[k][1]
#define lpc c[pre][0]
#define rpc c[pre][1]
inline void upd(int l, int r, int &k, int pre) {
  sum[k = ++tot] = sum[pre] + ql;
  if (l == r)
    return;
  int mid = (l + r) >> 1;
  if (ql <= mid)
    upd(l, mid, lc, lpc), rc = rpc;
  else
    upd(mid + 1, r, rc, rpc), lc = lpc;
}

inline ll query(int l, int r, int k) {
  if (r <= ql)
    return sum[k];
  if (ql < l || !k)
    return 0;
  int mid = (l + r) >> 1;
  if (ql <= mid)
    return query(l, mid, lc);
  else
    return sum[lc] + query(mid + 1, r, rc);
}

int main() {
  int n = read();
  ll tot = 0, to;
  rep(i, 1, n) v[i] = read();
  rep(i, 1, n) ql = v[i], upd(1, inf, rt, rt), tot += v[i];
  int ans = 0;
  rep(i, 1, n) {
    ql = v[i] * 2;
    while ((to = query(1, inf, rt)) > (ql >> 1))
      ql = to << 1;
    if (to == tot)
      ans++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#define rep(i, j, k) for (register int i = j; i <= k; i++)
#define dow(i, j, k) for (register int i = j; i >= k; i--)
#define ull unsigned long long
#define ll long long
#define eps 1e-9
using namespace std;

inline int read() {
  int s = 0, t = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      t = -1;
    c = getchar();
  }
  while (isdigit(c))
    s = s * 10 + c - 48, c = getchar();
  return s * t;
}

const int N = 100005, M = N * 32, inf = 1e9;
ll sum[M], ql;
int v[N], c[M][2], rt, tot = 0;
#define lc c[k][0]
#define rc c[k][1]
#define lpc c[pre][0]
#define rpc c[pre][1]
inline void upd(int l, int r, int &k, int pre) {
  sum[k = ++tot] = sum[pre] + ql;
  if (l == r)
    return;
  int mid = (l + r) >> 1;
  if (ql <= mid)
    upd(l, mid, lc, lpc), rc = rpc;
  else
    upd(mid + 1, r, rc, rpc), lc = lpc;
}

inline ll query(int l, int r, int k) {
  if (r <= ql)
    return sum[k];
  if (ql < l || !k)
    return 0;
  int mid = (l + r) >> 1;
  if (ql <= mid)
    return query(l, mid, lc);
  else
    return sum[lc] + query(mid + 1, r, rc);
}

int main() {
  int n = read();
  ll tot = 0, to;
  rep(i, 1, n) v[i] = read();
  rep(i, 1, n) ql = v[i], upd(1, inf, rt, rt), tot += v[i];
  int ans = 0;
  rep(i, 1, n) {
    ql = v[i] * 2;
    while ((to = query(1, inf, rt)) > (ql >> 1))
      ql = to << 1;
    if (to == tot)
      ans++;
  }
  cout << ans << endl;
  return 0;
}