Problem p02609 (C++)

Buggy submission (status: Runtime Error):
/*
 this->author = Fuad Ashraful Mehmet, CSE-UAP
  ToDo--
  uva 1223
*/
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < (int)n; ++i)
#define all(o) (o).begin(), (o).end()
#define dbg(o) cerr << "at " << __LINE__ << " response = " << o << endl;
using namespace std;

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>,
rb_tree_tag,tree_order_statistics_node_update> #define ordered_multiset
tree<pair<int, int>, null_type,less<pair<int, int> >,
rb_tree_tag,tree_order_statistics_node_update>
*/

const int N = 2e5 + 5;
typedef long long ll;
int n, m;
string s;
ll a[2][N]; // idx 0 for up and 1 for down
ll tree[2][N * 4];
int res[N];

ll bigMod(ll b, ll p, ll mod) {
  if (p == 0)
    return 1LL;
  if (p & 1)
    return (b * bigMod(b, p - 1, mod) % mod);

  ll r = bigMod(b, p >> 1, mod) % mod;
  return (r * r) % mod;
}
void calMod(int idx, int mod) {
  for (int i = 0; i < n; ++i) {
    if (s[i] == '0')
      continue;
    a[idx][i + 1] = bigMod(2, i, mod);
  }
} // end func

void buildTree(int idx, int l, int r, ll mod, int nd = 1) {
  if (l == r) {
    tree[idx][nd] = a[idx][l];
    return;
  }

  int mid = (l + r) >> 1;
  buildTree(idx, l, mid, mod, nd << 1);
  buildTree(idx, mid + 1, r, mod, nd << 1 | 1);
  tree[idx][nd] = (tree[idx][nd << 1] + tree[idx][nd << 1 | 1]) % mod;
  return;
}

ll query(int idx, int l, int r, int x, int y, ll mod, int nd = 1) {
  if (x > y)
    return 0LL;
  if (l > y or r < x)
    return 0LL;
  if (l >= x and r <= y) {
    return tree[idx][nd];
  }
  int mid = (l + r) >> 1;

  ll r1 = query(idx, l, mid, x, y, mod, nd << 1) % mod;
  ll r2 = query(idx, mid + 1, r, x, y, mod, nd << 1 | 1) % mod;
  return (r1 + r2) % mod;
}

int find_ans(int num) {
  int cnt = 0;
  while (num > 0) {
    cnt++;
    int bit = __builtin_popcount(num);
    num = num % bit;
  }
  return cnt;
}
void HalfDead() {
  cin >> n;
  cin >> s;
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    cnt += (s[i] == '1');
  }
  int up = cnt + 1;
  int down = cnt - 1;
  reverse(all(s));
  calMod(0, up);
  buildTree(0, 1, n, up);
  if (down >= 0) {
    calMod(1, down);
    buildTree(1, 1, n, down);
  }

  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') // one means take down answer
    {
      if (down == 0) {
        res[i + 1] = -1;
        continue;
      }
      ll ret =
          (query(1, 1, n, 1, i, down) + query(1, 1, n, i + 2, n, down)) % down;
      res[i + 1] = find_ans(ret);
    } else {
      ll ret = (query(0, 1, n, 1, i, up) + query(0, 1, n, i + 2, n, up) +
                bigMod(2, i, up)) %
               up;
      res[i + 1] = find_ans(ret);
    }
  }

  for (int i = n; i >= 1; --i) {
    cout << res[i] + 1 << endl;
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  int tc = 1;
  // cin>>tc;
  while (tc--)
    HalfDead();
  return 0;
}

Fixed submission:
/*
 this->author = Fuad Ashraful Mehmet, CSE-UAP
  ToDo--
  uva 1223
*/
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < (int)n; ++i)
#define all(o) (o).begin(), (o).end()
#define dbg(o) cerr << "at " << __LINE__ << " response = " << o << endl;
using namespace std;

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>,
rb_tree_tag,tree_order_statistics_node_update> #define ordered_multiset
tree<pair<int, int>, null_type,less<pair<int, int> >,
rb_tree_tag,tree_order_statistics_node_update>
*/

const int N = 2e5 + 5;
typedef long long ll;
int n, m;
string s;
ll a[2][N]; // idx 0 for up and 1 for down
ll tree[2][N * 4];
int res[N];

ll bigMod(ll b, ll p, ll mod) {
  if (p == 0)
    return 1LL;
  if (p & 1)
    return (b * bigMod(b, p - 1, mod) % mod);

  ll r = bigMod(b, p >> 1, mod) % mod;
  return (r * r) % mod;
}
void calMod(int idx, int mod) {
  for (int i = 0; i < n; ++i) {
    if (s[i] == '0')
      continue;
    a[idx][i + 1] = bigMod(2, i, mod);
  }
} // end func

void buildTree(int idx, int l, int r, ll mod, int nd = 1) {
  if (l == r) {
    tree[idx][nd] = a[idx][l];
    return;
  }

  int mid = (l + r) >> 1;
  buildTree(idx, l, mid, mod, nd << 1);
  buildTree(idx, mid + 1, r, mod, nd << 1 | 1);
  tree[idx][nd] = (tree[idx][nd << 1] + tree[idx][nd << 1 | 1]) % mod;
  return;
}

ll query(int idx, int l, int r, int x, int y, ll mod, int nd = 1) {
  if (x > y)
    return 0LL;
  if (l > y or r < x)
    return 0LL;
  if (l >= x and r <= y) {
    return tree[idx][nd];
  }
  int mid = (l + r) >> 1;

  ll r1 = query(idx, l, mid, x, y, mod, nd << 1) % mod;
  ll r2 = query(idx, mid + 1, r, x, y, mod, nd << 1 | 1) % mod;
  return (r1 + r2) % mod;
}

int find_ans(int num) {
  int cnt = 0;
  while (num > 0) {
    cnt++;
    int bit = __builtin_popcount(num);
    num = num % bit;
  }
  return cnt;
}
void HalfDead() {
  cin >> n;
  cin >> s;
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    cnt += (s[i] == '1');
  }
  int up = cnt + 1;
  int down = cnt - 1;
  reverse(all(s));
  calMod(0, up);
  buildTree(0, 1, n, up);
  if (down > 0) {
    calMod(1, down);
    buildTree(1, 1, n, down);
  }

  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') // one means take down answer
    {
      if (down == 0) {
        res[i + 1] = -1;
        continue;
      }
      ll ret =
          (query(1, 1, n, 1, i, down) + query(1, 1, n, i + 2, n, down)) % down;
      res[i + 1] = find_ans(ret);
    } else {
      ll ret = (query(0, 1, n, 1, i, up) + query(0, 1, n, i + 2, n, up) +
                bigMod(2, i, up)) %
               up;
      res[i + 1] = find_ans(ret);
    }
  }

  for (int i = n; i >= 1; --i) {
    cout << res[i] + 1 << endl;
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  int tc = 1;
  // cin>>tc;
  while (tc--)
    HalfDead();
  return 0;
}