Problem p04022 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define N 1000005
#define ll long long int
#define MP make_pair
#define pb push_back
#define ppb pop_back
#define sp " "
#define endl "\n"
#define fi first
#define se second
#define ii pair<int, int>
#define lli pair<ll, ll>
#define fast                                                                   \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios_base::sync_with_stdio(false)
#define fast2                                                                  \
  freopen("badhair.gir", "r", stdin);                                          \
  freopen("badhair.cik", "w", stdout);
#define mod 1000000007
#define fs(x, y)                                                               \
  for (ll i = 1; i <= y; i++)                                                  \
  cin >> x[i]
#define fo(i, x, y) for (ll i = x; i <= y; i++)
#define INF 1000000000005
using namespace std;

ll n, m, ar[N], sum, t, p[N], tt[N];

ll tut[N];
map<ll, int> cnt;
// ll cnt[N];

ll fastpow(ll x, ll y) {
  if (y == 0)
    return 1;
  if (y == 1)
    return x;
  if (y == 2)
    return x * x;
  if (y == 3)
    return x * x * x;
}

void sieve(ll ln) {
  for (ll i = 2; i <= ln; i++) {
    if (tt[i])
      continue;
    p[++m] = i;
    for (ll j = i * i; j <= ln; j += i)
      tt[j] = 1;
  }
}
void f(int ind) {
  ll x = ar[ind];
  ll tutmac = 1;
  ar[ind] = 1;

  // int l=0;
  for (int i = 1; i <= m; i++) {
    if (x % p[i] != 0)
      continue;
    int l = 0;
    while (x % p[i] == 0) {
      l++;
      x /= p[i];
    }
    // break;
    l %= 3;
    // cout<<ind<<sp<<flag<<sp<<l<<endl;
    ar[ind] *= fastpow(p[i], l);
    // if(l%3!=0)
    tutmac *= fastpow(p[i], (3 - l) % 3);
  }

  if (x != 1) {
    ll y = sqrt(x);
    if (y * y == x)
      tutmac *= y;
    else
      tutmac *= x * x;
  }
  ar[ind] *= x;

  cnt[ar[ind]]++;
  tut[ind] = tutmac;
}

int main() {
  fast;
  cin >> n;
  sieve(2155);
  // cout<<m<<endl;
  fs(ar, n);
  fo(i, 1, n) f(i);
  fo(i, 1, n)
      // cout<<ar[i]<<sp<<tut[ar[i]]<<endl;
      for (int i = 1; i <= n; i++) {
    if (ar[i] == 1)
      continue;
    sum += max(cnt[ar[i]], cnt[tut[i]]);
    cnt[ar[i]] = 0;
    cnt[tut[i]] = 0;
  }
  // if(!sum)
  // 	return cout<<0,0;
  if (cnt[1])
    sum++;
  cout << sum;
}

/*     cd onedrive\desktop\kod
cls

Sinav:21-22 aralik
Aciklama: Muhtemelen 25 aralik
*/

Fixed submission:
#include <bits/stdc++.h>
#define N 1000005
#define ll long long int
#define MP make_pair
#define pb push_back
#define ppb pop_back
#define sp " "
#define endl "\n"
#define fi first
#define se second
#define ii pair<int, int>
#define lli pair<ll, ll>
#define fast                                                                   \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios_base::sync_with_stdio(false)
#define fast2                                                                  \
  freopen("badhair.gir", "r", stdin);                                          \
  freopen("badhair.cik", "w", stdout);
#define mod 1000000007
#define fs(x, y)                                                               \
  for (ll i = 1; i <= y; i++)                                                  \
  cin >> x[i]
#define fo(i, x, y) for (ll i = x; i <= y; i++)
#define INF 1000000000005
using namespace std;

ll n, m, ar[N], sum, t, p[N], tt[N];

ll tut[N];
map<ll, int> cnt;
// ll cnt[N];

ll fastpow(ll x, ll y) {
  if (y == 0)
    return 1;
  if (y == 1)
    return x;
  if (y == 2)
    return x * x;
  if (y == 3)
    return x * x * x;
}

void sieve(ll ln) {
  for (ll i = 2; i <= ln; i++) {
    if (tt[i])
      continue;
    p[++m] = i;
    for (ll j = i * i; j <= ln; j += i)
      tt[j] = 1;
  }
}
void f(int ind) {
  ll x = ar[ind];
  ll tutmac = 1;
  ar[ind] = 1;

  // int l=0;
  for (int i = 1; i <= m; i++) {
    if (x % p[i] != 0)
      continue;
    int l = 0;
    while (x % p[i] == 0) {
      l++;
      x /= p[i];
    }
    // break;
    l %= 3;
    // cout<<ind<<sp<<flag<<sp<<l<<endl;
    ar[ind] *= fastpow(p[i], l);
    // if(l%3!=0)
    tutmac *= fastpow(p[i], (3 - l) % 3);
  }

  if (x != 1) {
    ll y = sqrt(x);
    if (y * y == x)
      tutmac *= y;
    else
      tutmac *= x * x;
  }
  ar[ind] *= x;

  cnt[ar[ind]]++;
  tut[ind] = tutmac;
}

int main() {
  fast;
  cin >> n;
  sieve(2155);
  // cout<<m<<endl;
  fs(ar, n);
  fo(i, 1, n) f(i);
  // fo(i,1,n)
  // cout<<ar[i]<<sp<<tut[ar[i]]<<endl;
  for (int i = 1; i <= n; i++) {
    if (ar[i] == 1)
      continue;
    sum += max(cnt[ar[i]], cnt[tut[i]]);
    cnt[ar[i]] = 0;
    cnt[tut[i]] = 0;
  }
  // if(!sum)
  // 	return cout<<0,0;
  if (cnt[1])
    sum++;
  cout << sum;
}

/*     cd onedrive\desktop\kod
cls

Sinav:21-22 aralik
Aciklama: Muhtemelen 25 aralik
*/