Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#include <bits/stdc++.h>

using namespace std;

#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(10);
#define f first
#define s second
#define endl "\\n"
#define pb push_back
#define mset(a, x) memset(a, x, sizeof(a))
#define debv(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << it << " ";                                                         \\
  cout << endl;
#define deb1(a) cout << a << endl;
#define deb2(a, b) cout << a << " " << b << endl;
#define deb3(a, b, c) cout << a << " " << b << " " << c << endl;
#define deb4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef unordered_map<ll, ll> um;
typedef vector<pll> vpll;

const ll N = 3e5 + 5;
const ll mod = 1e9 + 7;
const ll INF = 1e18;
const int INFi = 0x7f7f7f7f;

ll test = 1, n, ans[N], p2[N];
string x;

void calc(ll f, ll mod) {
  p2[0] = 1;
  for (int i = 1; i <= n; i++)
    p2[i] = (2 * p2[i - 1]) % mod;
  ll res = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1')
      res = (res + p2[i]) % mod;
  }
  for (int i = 0; i < n; i++) {
    if (x[i] == f + '0') {
      ll res1, ct = 1;
      if (f == 0)
        res1 = (res + p2[i]) % mod;
      else
        res1 = (res - p2[i] + mod) % mod;
      while (res1) {
        res1 = res1 % (__builtin_popcount(res1));
        ct++;
      }
      ans[i] = ct;
    }
  }
}

void solve() {
  cin >> n >> x;
  reverse(x.begin(), x.end());
  ll ct = 0;
  for (int i = 0; i < n; i++)
    ct += x[i] - '0';
  if (ct + 1)
    calc(0, ct + 1);
  if (ct - 1)
    calc(1, ct - 1);
  for (int i = n - 1; i >= 0; i--)
    deb1(ans[i])
}

int main() {
  fastio;
  // cin>>test;
  for (int i = 1; i <= test; i++) {
    solve();
  }
}

Error: -11

Fixed submission:

#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#include <bits/stdc++.h>

using namespace std;

#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(10);
#define f first
#define s second
#define endl "\\n"
#define pb push_back
#define mset(a, x) memset(a, x, sizeof(a))
#define debv(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << it << " ";                                                         \\
  cout << endl;
#define deb1(a) cout << a << endl;
#define deb2(a, b) cout << a << " " << b << endl;
#define deb3(a, b, c) cout << a << " " << b << " " << c << endl;
#define deb4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef unordered_map<ll, ll> um;
typedef vector<pll> vpll;

const ll N = 3e5 + 5;
const ll mod = 1e9 + 7;
const ll INF = 1e18;
const int INFi = 0x7f7f7f7f;

ll test = 1, n;
multiset<pair<ll, pll>, greater<>> s;
vector<pair<ll, pll>> val[N];

void solve() {
  s.clear();
  cin >> n;
  for (int i = 1; i <= n; i++) {
    ll l, r, k;
    cin >> k >> l >> r;
    val[k].pb({l - r, {l, k}});
  }
  ll ans = 0, ct = 0;
  for (int i = n; i > 0; i--) {
    for (auto it : val[i])
      s.insert(it);
    if (s.size() == 0)
      continue;
    if ((*s.begin()).f < 0)
      continue;
    ans += (*s.begin()).s.f;
    s.erase(s.begin());
    ct++;
  }
  for (int i = 1; i <= n; i++)
    val[i].clear();
  for (auto it : s) {
    val[it.s.s + 1].pb({-it.f, {it.s.f - it.f, it.s.s}});
  }
  s.clear();
  for (int i = 1; i <= n; i++) {
    for (auto it : val[i])
      s.insert(it);
    if (i <= ct)
      continue;
    if (s.size() == 0)
      continue;
    ans += (*s.begin()).s.f;
    s.erase(s.begin());
  }
  for (auto it : val[n + 1])
    s.insert(it);
  for (auto it : s)
    ans += it.s.f - it.f;
  deb1(ans) for (int i = 1; i <= n + 1; i++) val[i].clear();
}

int main() {
  fastio;
  cin >> test;
  for (int i = 1; i <= test; i++) {
    solve();
  }
}