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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define DEBUG(x) cout << #x << "=" << x << endl
#define DEBUG2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
typedef long long ll;
// #define LOCAL
const int MAXN = 1e5 + 10;
const ll MOD = 10;
ll _gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return _gcd(b, a % b);
}
ll gcd(ll a, ll b) {
  a = abs(a), b = abs(b);
  if (a < b)
    swap(a, b);
  return _gcd(a, b);
}
ll qpow(ll a, ll n) {
  ll rt = 1;
  while (n) {
    if (n & 1)
      rt = (rt * a) % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return rt;
}
ll factor[MAXN];
void cal_factor() {
  factor[0] = 1;
  for (int u = 1; u < MAXN; u++) {
    factor[u] = (factor[u - 1] * u) % MOD;
  }
}
ll C(ll n, ll k) {
  return factor[n] * qpow(factor[n - k], MOD - 2) % MOD *
         qpow(factor[k], MOD - 2) % MOD;
}
int N;
int pos[MAXN];
void solve() {
  scanf("%d", &N);
  ll sum = 0;
  for (int u = 1; u <= N; u++) {
    ll t;
    scanf("%lld", &t);
    pos[t]++;
    sum += t;
  }
  int Q;
  scanf("%d", &Q);
  while (Q--) {
    int b, c;
    scanf("%d %d", &b, &c);
    int sz = pos[b];
    sum += -(ll)b * sz + (ll)c * sz;
    printf("%lld\\n", sum);
    pos[c] += pos[b];
    pos[b] = 0;
  }
}
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif
  solve();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define DEBUG(x) cout << #x << "=" << x << endl
#define DEBUG2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
typedef long long ll;
// #define LOCAL
const int MAXN = 1e5 + 10;
const ll MOD = 10;
ll _gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return _gcd(b, a % b);
}
ll gcd(ll a, ll b) {
  a = abs(a), b = abs(b);
  if (a < b)
    swap(a, b);
  return _gcd(a, b);
}
ll qpow(ll a, ll n) {
  ll rt = 1;
  while (n) {
    if (n & 1)
      rt = (rt * a) % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return rt;
}
ll factor[MAXN];
void cal_factor() {
  factor[0] = 1;
  for (int u = 1; u < MAXN; u++) {
    factor[u] = (factor[u - 1] * u) % MOD;
  }
}
ll C(ll n, ll k) {
  return factor[n] * qpow(factor[n - k], MOD - 2) % MOD *
         qpow(factor[k], MOD - 2) % MOD;
}
int N;
unordered_set<int> pos[MAXN];
void solve() {
  scanf("%d", &N);
  ll sum = 0;
  for (int u = 1; u <= N; u++) {
    ll t;
    scanf("%lld", &t);
    pos[t].insert(u);
    sum += t;
  }
  int Q;
  scanf("%d", &Q);
  while (Q--) {
    int b, c;
    scanf("%d %d", &b, &c);
    int sz = pos[b].size();
    sum += -(ll)b * sz + (ll)c * sz;
    printf("%lld\\n", sum);
    for (int ps : pos[b]) {
      pos[c].insert(ps);
    }
    pos[b].clear();
  }
}
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif
  solve();
}