Problem p02660 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define mem(x, y)                                                              \\
  memset(x, y,                                                                 \\
         sizeof(x)) // memset only use to initialize 0 or -1 no other number.
#define pii pair<int, int>
#define pll pair<ll, ll>
#define INF 1e9
#define INFL 1e18
#define mod 1000000007
// #define mod 998244353
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    os;
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    os_pair;

ll power(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n % 2 == 1) {
      res = res * x;
    }
    x = x * x;
    n = n / 2;
  }
  return res;
}
ll powm(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}

// cout<< fixed << setprecision(10)

int main() {

  fast;
  int T;
  T = 1;
  // cin>>T;
  while (T--) {
    ll n, i, j;
    cin >> n;
    vector<pair<int, int>> v;

    for (ll i = 2; i * i <= n; ++i) {
      if (n % i == 0) {
        int count = 0;
        while (n % i == 0) {
          n /= i;
          count += 1;
        }

        // For every prime factor we are storing
        // count of it's occurenceand itself.
        v.push_back({i, count});
      }
    }

    // If n is prime
    if (n > 1) {
      v.push_back({n, 1});
    }
    vector<int> a;
    a.pb(0);
    int x = 0;
    for (i = 1; i < 64; i++) {
      x += i;
      a.pb(x);
    }
    vector<int>::iterator it;
    int ans = 0;
    for (i = 0; i < (int)v.size(); i++) {
      x = v[i].se;
      if (x == 1) {
        ans = ans + 1;
      } else {
        it = lower_bound(all(a), x);
        if (*it == x) {
          ans = ans + it - a.begin();
        } else {
          it--;
          int y = x - *it;
          int z = it - a.begin();
          if (z < y) {
            ans += z + 1;
          } else {
            ans += z;
          }
        }
      }
    }
    cout << ans;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define mem(x, y)                                                              \\
  memset(x, y,                                                                 \\
         sizeof(x)) // memset only use to initialize 0 or -1 no other number.
#define pii pair<int, int>
#define pll pair<ll, ll>
#define INF 1e9
#define INFL 1e18
#define mod 1000000007
// #define mod 998244353
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    os;
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    os_pair;

ll power(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n % 2 == 1) {
      res = res * x;
    }
    x = x * x;
    n = n / 2;
  }
  return res;
}
ll powm(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}

// cout<< fixed << setprecision(10)

int main() {

  fast;
  int T;
  T = 1;
  // cin>>T;
  while (T--) {
    ll n, i, j;
    cin >> n;
    vector<pair<int, int>> v;

    // Finding prime factorization of n
    for (int i = 2; i * i <= n; ++i) {
      if (n % i == 0) {
        int count = 0;
        while (n % i == 0) {
          n /= i;
          count += 1;
        }

        // For every prime factor we are storing
        // count of it's occurenceand itself.
        v.push_back({i, count});
      }
    }

    // If n is prime
    if (n > 1) {
      v.push_back({n, 1});
    }
    vector<int> a;
    a.pb(0);
    int x = 0;
    for (i = 1; i < 64; i++) {
      x += i;
      a.pb(x);
    }
    vector<int>::iterator it;
    int ans = 0;
    for (i = 0; i < v.size(); i++) {
      x = v[i].se;
      if (x == 1) {
        ans = ans + 1;
      } else {
        it = lower_bound(all(a), x);
        if (*it == x) {
          ans = ans + it - a.begin();
        } else {
          it--;
          int y = x - *it;
          int z = it - a.begin();
          if (z < y) {
            ans += z + 1;
          } else {
            ans += z;
          }
        }
      }
    }
    cout << ans;
  }
}