Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
const ll MOD = 1e9 + 7;

int main() {
  ll n, b[111111];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  sort(b, b + n);
  vector<ll> a;
  vector<ll> cnt;
  for (int i = 0; i < n;) {
    int k = 0;
    while (1) {
      if (i + k >= n)
        break;
      if (b[i] != b[i + k])
        break;
      k++;
    }
    a.push_back(b[i]);
    cnt.push_back(k);
    i += k;
  }
  n = a.size();
  ll ans = 0;
  for (ll i = n - 1; i >= 0; i--) {
    // cout<<a[i]<<" "<<cnt[i]<<endl;
    if (cnt[i] == 0)
      continue;
    vector<ll> v;
    ll t = a[i];
    // cout<<"t "<<t<<endl;
    while (t) {
      v.push_back(t % 2);
      t /= 2;
    }
    // for(int j=0;j<v.size();j++)cout<<"v "<<v[j]<<endl;
    int l = v.size();
    for (int j = (int)v.size() - 1; j >= 0; j--) {
      if (v[j] == 0)
        break;
      l--;
    }
    ll k = 0;
    for (ll j = 0; j < l; j++) {
      if (k == 0 && v[j])
        k += pow(2LL, j);
      if (k && v[j] == 0)
        k += pow(2LL, j);
    }
    // cout<<k<<endl;
    if (k == 0 && v.size() == l + 1) {
      ans += cnt[i] / 2;
      continue;
    }
    if (k == 0)
      k = pow(2LL, l);
    // cout<<k<<endl;
    ll ok = 0, ng = n, mid;
    while (ng - ok > 1) {
      mid = (ok + ng) / 2;
      if (a[mid] <= k)
        ok = mid;
      else
        ng = mid;
    }
    if (a[ok] == k) {
      ans += min(cnt[ok], cnt[i]);
      cnt[ok] = max(0LL, cnt[ok] - cnt[i]);
    }
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
const ll MOD = 1e9 + 7;

int main() {
  ll n, b[222222];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  sort(b, b + n);
  vector<ll> a;
  vector<ll> cnt;
  for (int i = 0; i < n;) {
    int k = 0;
    while (1) {
      if (i + k >= n)
        break;
      if (b[i] != b[i + k])
        break;
      k++;
    }
    a.push_back(b[i]);
    cnt.push_back(k);
    i += k;
  }
  n = a.size();
  ll ans = 0;
  for (ll i = n - 1; i >= 0; i--) {
    // cout<<a[i]<<" "<<cnt[i]<<endl;
    if (cnt[i] == 0)
      continue;
    vector<ll> v;
    ll t = a[i];
    // cout<<"t "<<t<<endl;
    while (t) {
      v.push_back(t % 2);
      t /= 2;
    }
    // for(int j=0;j<v.size();j++)cout<<"v "<<v[j]<<endl;
    int l = v.size();
    for (int j = (int)v.size() - 1; j >= 0; j--) {
      if (v[j] == 0)
        break;
      l--;
    }
    ll k = 0;
    for (ll j = 0; j < l; j++) {
      if (k == 0 && v[j])
        k += pow(2LL, j);
      if (k && v[j] == 0)
        k += pow(2LL, j);
    }
    // cout<<k<<endl;
    if (k == 0 && v.size() == l + 1) {
      ans += cnt[i] / 2;
      continue;
    }
    if (k == 0)
      k = pow(2LL, l);
    // cout<<k<<endl;
    ll ok = 0, ng = n, mid;
    while (ng - ok > 1) {
      mid = (ok + ng) / 2;
      if (a[mid] <= k)
        ok = mid;
      else
        ng = mid;
    }
    if (a[ok] == k) {
      ans += min(cnt[ok], cnt[i]);
      cnt[ok] = max(0LL, cnt[ok] - cnt[i]);
    }
  }

  cout << ans << endl;
  return 0;
}