Problem p02660 (C++)

Buggy submission (status: Runtime Error):
/*** akshaykumar99 ***/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using id_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define f(i, x, n) for (i = x; i < n; i++)
#define fr(i, x, n) for (i = x; i >= n; i--)
#define pu push_back
#define pp pop_back
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define SP(x) fixed << setprecision(x)
#define IOS()                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

const long long MOD = 1e9 + 7;
const long double PI = acos(-1);
const long long INF = INT_MAX;
const long long LINF = LLONG_MAX;
const long long N = 1e18;

typedef long long ll;
typedef long double ld;
using u128 = __uint128_t;

void test_case() {
  ll n, i, j;
  cin >> n;
  if (n == 1) {
    cout << 0 << '\n';
  }
  set<pair<ll, ll>> s;
  ll zz = n;
  ll res = 0;
  if (n % 2 == 0) {
    s.insert({2, 2});
    while (n % 2 == 0)
      n /= 2;
  }
  for (i = 3; i * i <= n; i += 2) {
    if (n % i == 0) {
      s.insert({i, i});
      while (n % i == 0)
        n /= i;
    }
  }
  if (n > 1) {
    s.insert({n, n});
  }
  n = zz;
  while (n >= (*s.begin()).F) {
    auto it = s.begin();
    ll x = (*it).F, y = (*it).S;
    if (n % x == 0) {
      res++;
      n /= x;
    }
    ll z = x * y;
    s.insert({z, y});
    s.erase(it);
  }
  cout << res << '\n';
}

int32_t main() {
  IOS();
  int tc = 1;
  // cin>>tc;
  while (tc--)
    test_case();
  return 0;
}


Fixed submission:
/*** akshaykumar99 ***/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using id_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define f(i, x, n) for (i = x; i < n; i++)
#define fr(i, x, n) for (i = x; i >= n; i--)
#define pu push_back
#define pp pop_back
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define SP(x) fixed << setprecision(x)
#define IOS()                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

const long long MOD = 1e9 + 7;
const long double PI = acos(-1);
const long long INF = INT_MAX;
const long long LINF = LLONG_MAX;
const long long N = 1e18;

typedef long long ll;
typedef long double ld;
using u128 = __uint128_t;

void test_case() {
  ll n, i, j;
  cin >> n;
  if (n == 1) {
    cout << 0 << '\n';
    return;
  }
  set<pair<ll, ll>> s;
  ll zz = n;
  ll res = 0;
  if (n % 2 == 0) {
    s.insert({2, 2});
    while (n % 2 == 0)
      n /= 2;
  }
  for (i = 3; i * i <= n; i += 2) {
    if (n % i == 0) {
      s.insert({i, i});
      while (n % i == 0)
        n /= i;
    }
  }
  if (n > 1) {
    s.insert({n, n});
  }
  n = zz;
  while (n >= (*s.begin()).F) {
    auto it = s.begin();
    ll x = (*it).F, y = (*it).S;
    if (n % x == 0) {
      res++;
      n /= x;
    }
    ll z = x * y;
    s.insert({z, y});
    s.erase(it);
  }
  cout << res << '\n';
}

int32_t main() {
  IOS();
  int tc = 1;
  // cin>>tc;
  while (tc--)
    test_case();
  return 0;
}