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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

using ld = long double;
using ll = long long int;
using ul = unsigned long long int;

namespace lamlib {
/* constant */
constexpr double epsilon = std::numeric_limits<double>::epsilon();

/* math */
template <class T> inline T abs(const T &a) { return (a > 0) ? a : -a; }
ul inline digit(const ul &num) {
  return static_cast<ul>(std::log10(num + epsilon)) + 1;
}

/* algorithm */
ul gcd(const ul &a, const ul &b) { return (!b) ? a : gcd(b, a % b); }
ul lcm(const ul &a, const ul &b) {
  return (a / gcd(a, b)) * b;
} // a/gcd(a,b)*b <=> (a*b)/gcd(a,b)
std::vector<bool> eratosthenes(const ul &n) {
  std::vector<bool> prime_candidate(n, true);
  prime_candidate[0] = prime_candidate[1] = false;
  for (ul i = 2; i * i < n; ++i) {
    if (!prime_candidate[i])
      continue;
    for (ul j = 2; i * j < n; ++j)
      prime_candidate[i * j] = false;
  }
  return prime_candidate;
}
ll rho_method(const ll &n) {
  auto f = [&](const ll &xi) { return (xi * xi + 1) % n; };
  ll x = 2, y = 2, d = 1;
  while (d == 1) {
    x = f(x);
    y = f(f(y));
    d = lamlib::gcd(lamlib::abs(x - y), n);
  }
  return d;
}
// 素因数分解(試し割)
std::vector<ll> prime_factorization_trial(const ll &n) {
  ll num = n;
  std::vector<ll> prime;
  for (ll i = 2; i * i <= n; ++i) {
    while ((num % i) == 0) {
      prime.emplace_back(i);
      num /= i;
    }
  }
  if (num > 1)
    prime.emplace_back(num);
  return prime;
}

/* string */
inline ul same_char_count(const std::string s, const char &ch) {
  return std::count(std::cbegin(s), std::cend(s), ch);
}
} // namespace lamlib

// std::cout << std::fixed << std::setprecision(15) << std::endl;

int main(int argc, char *argv[]) {
  ll n;
  std::cin >> n;

  ll ans = 0;

  std::vector<ll> a(100000, 0);
  for (ll i = 0; i < n; ++i) {
    ll tmp;
    std::cin >> tmp;
    ++a[tmp];

    ans += tmp;
  }

  ll q;
  std::cin >> q;
  std::vector<ll> b(q, 0), c(q, 0);
  for (ll i = 0; i < q; ++i)
    std::cin >> b[i] >> c[i];

  for (ll i = 0; i < q; ++i) {
    ll diff = c[i] - b[i];
    ll cnt = diff * a[b[i]];

    ans += cnt;
    std::cout << ans << std::endl;

    a[c[i]] += a[b[i]];
    a[b[i]] = 0;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

using ld = long double;
using ll = long long int;
using ul = unsigned long long int;

namespace lamlib {
/* constant */
constexpr double epsilon = std::numeric_limits<double>::epsilon();

/* math */
template <class T> inline T abs(const T &a) { return (a > 0) ? a : -a; }
ul inline digit(const ul &num) {
  return static_cast<ul>(std::log10(num + epsilon)) + 1;
}

/* algorithm */
ul gcd(const ul &a, const ul &b) { return (!b) ? a : gcd(b, a % b); }
ul lcm(const ul &a, const ul &b) {
  return (a / gcd(a, b)) * b;
} // a/gcd(a,b)*b <=> (a*b)/gcd(a,b)
std::vector<bool> eratosthenes(const ul &n) {
  std::vector<bool> prime_candidate(n, true);
  prime_candidate[0] = prime_candidate[1] = false;
  for (ul i = 2; i * i < n; ++i) {
    if (!prime_candidate[i])
      continue;
    for (ul j = 2; i * j < n; ++j)
      prime_candidate[i * j] = false;
  }
  return prime_candidate;
}
ll rho_method(const ll &n) {
  auto f = [&](const ll &xi) { return (xi * xi + 1) % n; };
  ll x = 2, y = 2, d = 1;
  while (d == 1) {
    x = f(x);
    y = f(f(y));
    d = lamlib::gcd(lamlib::abs(x - y), n);
  }
  return d;
}
// 素因数分解(試し割)
std::vector<ll> prime_factorization_trial(const ll &n) {
  ll num = n;
  std::vector<ll> prime;
  for (ll i = 2; i * i <= n; ++i) {
    while ((num % i) == 0) {
      prime.emplace_back(i);
      num /= i;
    }
  }
  if (num > 1)
    prime.emplace_back(num);
  return prime;
}

/* string */
inline ul same_char_count(const std::string s, const char &ch) {
  return std::count(std::cbegin(s), std::cend(s), ch);
}
} // namespace lamlib

// std::cout << std::fixed << std::setprecision(15) << std::endl;

int main(int argc, char *argv[]) {
  ll n;
  std::cin >> n;
  std::vector<ll> a(n, 0);
  for (ll i = 0; i < n; ++i)
    std::cin >> a[i];

  ll q;
  std::cin >> q;
  std::vector<ll> b(q, 0), c(q, 0);
  for (ll i = 0; i < q; ++i)
    std::cin >> b[i] >> c[i];

  for (ll i = 0; i < q; ++i) {
    for (ll j = 0; j < n; ++j)
      if (a[j] == b[i])
        a[j] = c[i];

    ll ans = 0;
    for (ll i = 0; i < n; ++i)
      ans += a[i];
    std::cout << ans << std::endl;
  }

  return 0;
}