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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPR(i, n) for (ll(i) = (n); (i) >= 0; (i)--)
#define FOR(i, m, n) for (ll(i) = (m); (i) < (n); (i)++)
#define INF 1e9
#define ALL(v) v.begin(), v.end()
#define _GLIBCXX_DEBUG

template <uint MD> struct ModInt {
  using M = ModInt;
  const static M G;
  uint v;
  ModInt(ll _v = 0) { set_v(uint(_v % MD + MD)); }
  M &set_v(uint _v) {
    v = (_v < MD) ? _v : _v - MD;
    return *this;
  }
  explicit operator bool() const { return v != 0; }
  M operator-() const { return M() - *this; }
  M operator+(const M &r) const { return M().set_v(v + r.v); }
  M operator-(const M &r) const { return M().set_v(v + MD - r.v); }
  M operator*(const M &r) const { return M().set_v(uint(ull(v) * r.v % MD)); }
  M operator/(const M &r) const { return *this * r.inv(); }
  M &operator+=(const M &r) { return *this = *this + r; }
  M &operator-=(const M &r) { return *this = *this - r; }
  M &operator*=(const M &r) { return *this = *this * r; }
  M &operator/=(const M &r) { return *this = *this / r; }
  bool operator==(const M &r) const { return v == r.v; }
  M pow(ll n) const {
    M x = *this, r = 1;
    while (n) {
      if (n & 1)
        r *= x;
      x *= x;
      n >>= 1;
    }
    return r;
  }
  M inv() const { return pow(MD - 2); }
  friend ostream &operator<<(ostream &os, const M &r) { return os << r.v; }
};
using Mint = ModInt<1000000007>;

const double PI = 3.14159265358979323846;

bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }

ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

int main() {
  ll n, q;
  cin >> n;
  map<ll, ll> mp;
  REP(i, n) {
    ll a;
    cin >> a;
    mp[a]++;
  }
  cin >> q;
  ll allsum = 0;
  for (auto p : mp)
    allsum += p.first * p.second;
  ll sum = allsum;

  REP(i, q) {
    ll b, c;
    cin >> b >> c;
    sum = sum - mp[b] * b + mp[b] * c;
    mp[c] += mp[b];
    mp[b] = 0;
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPR(i, n) for (ll(i) = (n); (i) >= 0; (i)--)
#define FOR(i, m, n) for (ll(i) = (m); (i) < (n); (i)++)
#define INF 1e9
#define ALL(v) v.begin(), v.end()
#define _GLIBCXX_DEBUG

template <uint MD> struct ModInt {
  using M = ModInt;
  const static M G;
  uint v;
  ModInt(ll _v = 0) { set_v(uint(_v % MD + MD)); }
  M &set_v(uint _v) {
    v = (_v < MD) ? _v : _v - MD;
    return *this;
  }
  explicit operator bool() const { return v != 0; }
  M operator-() const { return M() - *this; }
  M operator+(const M &r) const { return M().set_v(v + r.v); }
  M operator-(const M &r) const { return M().set_v(v + MD - r.v); }
  M operator*(const M &r) const { return M().set_v(uint(ull(v) * r.v % MD)); }
  M operator/(const M &r) const { return *this * r.inv(); }
  M &operator+=(const M &r) { return *this = *this + r; }
  M &operator-=(const M &r) { return *this = *this - r; }
  M &operator*=(const M &r) { return *this = *this * r; }
  M &operator/=(const M &r) { return *this = *this / r; }
  bool operator==(const M &r) const { return v == r.v; }
  M pow(ll n) const {
    M x = *this, r = 1;
    while (n) {
      if (n & 1)
        r *= x;
      x *= x;
      n >>= 1;
    }
    return r;
  }
  M inv() const { return pow(MD - 2); }
  friend ostream &operator<<(ostream &os, const M &r) { return os << r.v; }
};
using Mint = ModInt<1000000007>;

const double PI = 3.14159265358979323846;

bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }

ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

int main() {
  ll n, q;
  cin >> n;
  map<ll, ll> mp;
  REP(i, n) {
    ll a;
    cin >> a;
    mp[a]++;
  }
  cin >> q;

  REP(i, q) {
    ll b, c;
    cin >> b >> c;
    ll sum = 0;
    ll tmp = mp[b];
    mp[b] = 0;
    mp[c] += tmp;
    for (auto p : mp)
      sum += p.first * p.second;
    cout << sum << endl;
  }
}