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

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
const double PI = acos(-1.0);
#define MOD 1000000007LL
#define endl "\\n"

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N;
  cin >> N;
  vll fact(0);
  if (N == 1) {
    cout << 0 << endl;
    return 0;
  }
  for (ll i = 2; i * i <= N; i++) {
    if (N % i == 0) {
      ll count = 0LL;
      while (N % i == 0) {
        N /= i;
        count++;
      }
      fact.push_back(count);
    }
  }
  if (N != 1)
    fact.push_back(1);
  sort(ALL(fact));
  vector<P> num(0);
  for (int i = 0; i < sz(fact); i++) {
    if (i == 0)
      num.push_back(P(fact.at(i), 1));
    else {
      int siz = num.size() - 1;
      if (num.at(siz).first == fact.at(i))
        num.at(siz).second++;
      else
        num.push_back(P(fact.at(i), 1));
    }
  }
  // for(auto x: mp) cout << x.first << ' ' << x.second << endl;
  vll box(0);
  ll sum = 0LL;
  for (int i = 1; i < 1000; i++) {
    sum += i;
    box.push_back(sum);
  }
  ll ans = 0LL;
  for (int i = 0; i < sz(num); i++) {
    ll n = num.at(i).first;
    ll c = num.at(i).second;
    ll x = upper_bound(ALL(box), n) - box.begin();
    ans += x * c;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
const double PI = acos(-1.0);
#define MOD 1000000007LL
#define endl "\\n"

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N;
  cin >> N;
  vll fact(0);
  if (N == 1) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 2; i * i <= N; i++) {
    if (N % i == 0) {
      ll count = 0LL;
      while (N % i == 0) {
        N /= i;
        count++;
      }
      fact.push_back(count);
    }
  }
  if (N != 1)
    fact.push_back(1);
  sort(ALL(fact));
  vector<P> num(0);
  for (int i = 0; i < sz(fact); i++) {
    if (i == 0)
      num.push_back(P(fact.at(i), 1));
    else {
      int siz = num.size() - 1;
      if (num.at(siz).first == fact.at(i))
        num.at(siz).second++;
      else
        num.push_back(P(fact.at(i), 1));
    }
  }
  // for(auto x: mp) cout << x.first << ' ' << x.second << endl;
  vll box(0);
  ll sum = 0LL;
  for (int i = 1; i < 1000; i++) {
    sum += i;
    box.push_back(sum);
  }
  ll ans = 0LL;
  for (int i = 0; i < sz(num); i++) {
    ll n = num.at(i).first;
    ll c = num.at(i).second;
    ll x = upper_bound(ALL(box), n) - box.begin();
    ans += x * c;
  }
  cout << ans << endl;
}