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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ll int64_t
#define str string
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, n) for (ll i = 1; i <= n; i++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mod 1000000007
#define PI 3.141592653589793
#define vec vector
#define dump(x) cerr << #x << "=" << x << endl
using namespace std;

#define INF 2000000000
#define MAX_V 10
const int MAX_N = 10000000;

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

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

ll modpow(ll a, ll n, ll mod1) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod1;
    a = a * a % mod1;
    n >>= 1;
  }
  return res;
}

ll factorial(ll n) {
  ll x = 1;
  rrep(i, n) x *= i;
  return x;
}

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

ll func(ll a, ll b, ll &ans_x, ll &ans_y) {
  if (b == 0) {
    ans_x = 1;
    ans_y = 0;
    return a;
  } else {
    ll ret = func(b, (a % b), ans_y, ans_x);
    ans_y -= (a / b) * ans_x;
    return ret;
  }
}

ll par[MAX_N], ranking[MAX_N];

void init(ll n) {
  rep(i, n) {
    par[i] = i;
    ranking[i] = 0;
  }
}

ll find(ll x) {
  if (par[x] == x)
    return x;
  else
    return par[x] = find(par[x]);
}

void unite(ll x, ll y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (ranking[x] < ranking[y])
    par[x] = y;
  else {
    par[y] = x;
    if (ranking[x] == ranking[y])
      ranking[x]++;
  }
}

bool same(ll x, ll y) { return find(x) == find(y); }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, sum = 0;
  cin >> n;

  vector<ll> a(100000, 0);
  rep(i, n) {
    ll b;
    cin >> b;
    sum += b;
    a[b]++;
  }
  ll q;
  cin >> q;
  rep(i, q) {
    ll f, b;
    cin >> f >> b;
    sum += (b - f) * a[f];
    a[b] += a[f];
    a[f] = 0;
    cout << sum << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ll int64_t
#define str string
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, n) for (ll i = 1; i <= n; i++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mod 1000000007
#define PI 3.141592653589793
#define vec vector
#define dump(x) cerr << #x << "=" << x << endl
using namespace std;

#define INF 2000000000
#define MAX_V 10
const int MAX_N = 10000000;

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

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

ll modpow(ll a, ll n, ll mod1) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod1;
    a = a * a % mod1;
    n >>= 1;
  }
  return res;
}

ll factorial(ll n) {
  ll x = 1;
  rrep(i, n) x *= i;
  return x;
}

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

ll func(ll a, ll b, ll &ans_x, ll &ans_y) {
  if (b == 0) {
    ans_x = 1;
    ans_y = 0;
    return a;
  } else {
    ll ret = func(b, (a % b), ans_y, ans_x);
    ans_y -= (a / b) * ans_x;
    return ret;
  }
}

ll par[MAX_N], ranking[MAX_N];

void init(ll n) {
  rep(i, n) {
    par[i] = i;
    ranking[i] = 0;
  }
}

ll find(ll x) {
  if (par[x] == x)
    return x;
  else
    return par[x] = find(par[x]);
}

void unite(ll x, ll y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (ranking[x] < ranking[y])
    par[x] = y;
  else {
    par[y] = x;
    if (ranking[x] == ranking[y])
      ranking[x]++;
  }
}

bool same(ll x, ll y) { return find(x) == find(y); }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, sum = 0;
  cin >> n;

  map<ll, ll> a;
  rep(i, n) {
    ll b;
    cin >> b;
    sum += b;
    a[b]++;
  }
  ll q;
  cin >> q;
  rep(i, q) {
    ll f, b;
    cin >> f >> b;

    if (a.count(f) == 0) {
      cout << sum << endl;
      continue;
    }
    for (auto p : a) {
      if (p.first == f) {
        sum += (b - f) * p.second;
        a.erase(f);
        a[b] += p.second;
        break;
      }
    }
    cout << sum << endl;
  }

  return 0;
}