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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double EPS = 1e-9;
const double INF = 2147483647;
#define rep(i, n) rep3(i, 0, n)
#define rep2(i, n) rep3(i, 1, n + 1)
#define rep3(i, x, n) for (ll i = x; i < (ll)(n); i++)

template <class T> void printv(vector<T> v) {
  for (auto it = v.begin(); it != v.end(); it++) {
    cout << *it << ",";
  }
  cout << endl;
}
template <class T> void printp(vector<pair<T, T>> v) {
  for (auto it = v.begin(); it != v.end(); it++) {
    cout << (*it).first << "," << (*it).second << endl;
  }
  cout << endl;
}
template <class T> void printp(map<T, T> v) {
  for (auto it = v.begin(); it != v.end(); it++) {
    cout << (*it).first << "," << (*it).second << endl;
  }
  cout << endl;
}

vector<pair<long long, long long>> prime_factorize(long long N) {
  vector<pair<long long, long long>> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;
    long long ex = 0; // 指数

    // 割れる限り割り続ける
    while (N % a == 0) {
      ++ex;
      N /= a;
    }

    // その結果を push
    res.push_back({a, ex});
  }

  // 最後に残った数について
  if (N != 1)
    res.push_back({N, 1});
  return res;
}

int main(void) {
  ll N, Q;
  cin >> N;
  map<ll, ll> A;
  ll s = 0;
  rep(i, N) {
    ll tmp;
    cin >> tmp;
    A[tmp]++;
    s += tmp;
  }
  cin >> Q;

  rep(i, Q) {
    ll b, c;
    cin >> b >> c;
    // for(auto kv : A){
    //     cout << kv.first << " " << kv.second << endl;
    //     // s+=kv.first*kv.second;
    // }
    s = s + A[b] * c - A[b] * b;
    A[c] += A[b];
    A[b] = 0;
    cout << s << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double EPS = 1e-9;
const double INF = 2147483647;
#define rep(i, n) rep3(i, 0, n)
#define rep2(i, n) rep3(i, 1, n + 1)
#define rep3(i, x, n) for (ll i = x; i < (ll)(n); i++)

template <class T> void printv(vector<T> v) {
  for (auto it = v.begin(); it != v.end(); it++) {
    cout << *it << ",";
  }
  cout << endl;
}
template <class T> void printp(vector<pair<T, T>> v) {
  for (auto it = v.begin(); it != v.end(); it++) {
    cout << (*it).first << "," << (*it).second << endl;
  }
  cout << endl;
}
template <class T> void printp(map<T, T> v) {
  for (auto it = v.begin(); it != v.end(); it++) {
    cout << (*it).first << "," << (*it).second << endl;
  }
  cout << endl;
}

vector<pair<long long, long long>> prime_factorize(long long N) {
  vector<pair<long long, long long>> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;
    long long ex = 0; // 指数

    // 割れる限り割り続ける
    while (N % a == 0) {
      ++ex;
      N /= a;
    }

    // その結果を push
    res.push_back({a, ex});
  }

  // 最後に残った数について
  if (N != 1)
    res.push_back({N, 1});
  return res;
}

int main(void) {
  ll N, Q;
  cin >> N;
  map<ll, ll> A;
  rep(i, N) {
    ll tmp;
    cin >> tmp;
    A[tmp]++;
  }
  cin >> Q;

  rep(i, Q) {
    ll b, c;
    cin >> b >> c;
    A[c] += A[b];
    A[b] = 0;
    ll s = 0;
    for (auto kv : A) {
      // cout << j << " " << A[j] << endl;
      s += kv.first * kv.second;
    }
    cout << s << endl;
  }

  return 0;
}