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

#include <bits/stdc++.h>

#define ll long long int
#define ld long double
#define pb push_back
#define pq priority_queue
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define vvll vector<vector<ll>>
#define vvi vector<vector<int>>
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
#define MODNUM 1000000007
#define lp(i, a, b) for (ll i = a; i < b; i++)

using namespace std;

ll mex(ll a, ll b, ll m) {
  if (b == 0)
    return 1;
  if (b % 2)
    return a * mex((a * a) % m, (b - 1) / 2, m) % m;
  else
    return mex((a * a) % m, b / 2, m);
}

int x, y;
void exteu(ll a, ll m) {
  if (m == 0) {
    x = 1;
    y = 0;
  } else {
    exteu(m, a % m);
    ll temp = x;
    x = y;
    y = temp - (a / m) * y;
  }
}

ll fact(ll n) {
  ll prod = 1;
  for (ll i = 2; i <= n; i++) {
    prod *= i;
  }
  return prod;
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

// bool isprime[100001];
// void makesieve() {
// 	memset(isprime, true, sizeof(isprime));
// 	for (int i = 2; i <= sqrt(200000); ++i)
// 	{
// 		if(isprime[i]) {
// 			for (int j = i*i; j <= 200000; j+=i)
// 			{
// 				isprime[j]=false;
// 			}
// 		}
// 	}
// }
//
// 1 2 4 5 8
void solve() {
  ll n;
  cin >> n;
  ll s = 0;
  vll hash(100001, 0);
  vll a(n);
  lp(i, 0, n) {
    cin >> a[i];
    s += a[i];
    hash[a[i]]++;
  }
  ll q;
  cin >> q;
  while (q--) {
    ll b, c;
    cin >> b >> c;
    s -= hash[b] * b;
    s += hash[b] * c;
    hash[c] += hash[b];
    hash[b] = 0;
    cout << s << endl;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll TESTS = 1;
  // cin >> TESTS;
  // makesieve();
  while (TESTS--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ll long long int
#define ld long double
#define pb push_back
#define pq priority_queue
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define vvll vector<vector<ll>>
#define vvi vector<vector<int>>
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
#define MODNUM 1000000007
#define lp(i, a, b) for (ll i = a; i < b; i++)

using namespace std;

ll mex(ll a, ll b, ll m) {
  if (b == 0)
    return 1;
  if (b % 2)
    return a * mex((a * a) % m, (b - 1) / 2, m) % m;
  else
    return mex((a * a) % m, b / 2, m);
}

int x, y;
void exteu(ll a, ll m) {
  if (m == 0) {
    x = 1;
    y = 0;
  } else {
    exteu(m, a % m);
    ll temp = x;
    x = y;
    y = temp - (a / m) * y;
  }
}

ll fact(ll n) {
  ll prod = 1;
  for (ll i = 2; i <= n; i++) {
    prod *= i;
  }
  return prod;
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

// bool isprime[100001];
// void makesieve() {
// 	memset(isprime, true, sizeof(isprime));
// 	for (int i = 2; i <= sqrt(200000); ++i)
// 	{
// 		if(isprime[i]) {
// 			for (int j = i*i; j <= 200000; j+=i)
// 			{
// 				isprime[j]=false;
// 			}
// 		}
// 	}
// }
//
// 1 2 4 5 8
void solve() {
  ll n;
  cin >> n;
  ll s = 0;
  vll hash(100001, 0);
  vll a(n);
  lp(i, 0, n) {
    cin >> a[i];
    s += a[i];
    hash[a[i]]++;
  }
  ll q;
  cin >> q;
  while (q--) {
    ll b, c;
    cin >> b >> c;
    while (hash[b]--) {
      s -= b;
      s += c;
      hash[c]++;
    }
    cout << s << endl;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll TESTS = 1;
  // cin >> TESTS;
  // makesieve();
  while (TESTS--) {
    solve();
  }
  return 0;
}