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

#include <bits/stdc++.h>
#define ll long long
#define endl "\\n"

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define mi map<int, int>
#define pi pair<int, int>

#define Fori(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i >= b; i--)

#define pb push_back
#define mp make_pair

#define read(a, n)                                                             \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];
#define print(x) cout << "X: == " << x << endl;
#define printMap(a)                                                            \\
  for (auto it = a.begin(); it != a.end(); it++) {                             \\
    cout << it->first << "  " << it->second << endl;                           \\
  }
#define printVector(a)                                                         \\
  for (auto it = a.begin(); it != a.end(); it++) {                             \\
    cout << *it << endl;                                                       \\
  }

using namespace std;
int main() {
  IOS ll n;
  cin >> n;
  map<ll, ll> a;
  Fori(i, 0, n) {
    ll x;
    cin >> x;
    a[x]++;
  }
  ll q;
  cin >> q;
  ll sum = 0;
  for (auto it = a.begin(); it != a.end(); it++) {
    sum += (it->second) * (it->first);
  }
  Fori(i, 0, q) {
    ll b, c;
    cin >> b >> c;
    sum = sum - (a[b] * b) + (a[b] * c);
    a[c] += a[b];
    a[b] = 0;
    // for(auto it=a.begin(); it!=a.end(); it++) {
    //     sum += (it->second) * (it->first);
    // }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define endl "\\n"

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define mi map<int, int>
#define pi pair<int, int>

#define Fori(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i >= b; i--)

#define pb push_back
#define mp make_pair

#define read(a, n)                                                             \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];
#define print(x) cout << "X: == " << x << endl;
#define printMap(a)                                                            \\
  for (auto it = a.begin(); it != a.end(); it++) {                             \\
    cout << it->first << "  " << it->second << endl;                           \\
  }
#define printVector(a)                                                         \\
  for (auto it = a.begin(); it != a.end(); it++) {                             \\
    cout << *it << endl;                                                       \\
  }

using namespace std;
int main() {
  IOS ll n;
  cin >> n;
  map<ll, ll> a;
  Fori(i, 0, n) {
    ll x;
    cin >> x;
    a[x]++;
  }
  ll q;
  cin >> q;
  Fori(i, 0, q) {
    ll b, c;
    cin >> b >> c;
    a[c] += a[b];
    a[b] = 0;
    ll sum = 0;
    for (auto it = a.begin(); it != a.end(); it++) {
      sum += (it->second) * (it->first);
    }
    cout << sum << endl;
  }
  return 0;
}