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

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ll long long
#define ull unsigned long long
#define si set<ll>
#define vi vector<ll>
#define popcount(x) __builtin_popcountll(x)
#define mii map<ll, ll>
#define vpi vector<pair<ll, ll>>
#define sz(c) (int)c.size()
#define fr first
#define ll long long
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sc second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define mem0(a) memset(a, 0, sizeof(a))
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define ld long double
#define rall(a) (a).rbegin(), (a).rend()

ll power(ll b, ll e, ll m) {
  if (e == 0)
    return 1;
  if (e & 1)
    return b * power(b * b % m, e / 2, m) % m;
  return power(b * b % m, e / 2, m);
}
ll power(ll b, ll e) {
  if (e == 0)
    return 1;
  if (e & 1)
    return b * power(b * b, e / 2);
  return power(b * b, e / 2);
}

bool isPowerOfTwo(ll x) {
  // x will check if x == 0 and !(x & (x - 1)) will check if x is a power of 2
  // or not
  return (x && !(x & (x - 1)));
}

ll ldd(ll n) {
  for (ll i = 3; i * i <= n; i += 2)
    if (n % i == 0)
      return i;
}

void solve() {
  ll n, x;
  cin >> n;
  map<ll, ll> mp;
  rep(i, 0, n) {
    cin >> x;
    mp[x]++;
  }
  ll ans = 0;
  for (auto i : mp)
    ans += i.fr * i.sc;
  ll q;
  cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    ans = ans - a * mp[a] + b * mp[a];
    if (mp.find(b) != mp.end())
      mp[b] += mp[a];
    else
      mp.insert({b, mp[a]});
    mp.erase(a);
    cout << ans << "\\n";
  }
}

int main() {
  fastio ll t = 1;
  while (t--)
    solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ll long long
#define ull unsigned long long
#define si set<ll>
#define vi vector<ll>
#define popcount(x) __builtin_popcountll(x)
#define mii map<ll, ll>
#define vpi vector<pair<ll, ll>>
#define sz(c) (int)c.size()
#define fr first
#define ll long long
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sc second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define mem0(a) memset(a, 0, sizeof(a))
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define ld long double
#define rall(a) (a).rbegin(), (a).rend()

ll power(ll b, ll e, ll m) {
  if (e == 0)
    return 1;
  if (e & 1)
    return b * power(b * b % m, e / 2, m) % m;
  return power(b * b % m, e / 2, m);
}
ll power(ll b, ll e) {
  if (e == 0)
    return 1;
  if (e & 1)
    return b * power(b * b, e / 2);
  return power(b * b, e / 2);
}

bool isPowerOfTwo(ll x) {
  // x will check if x == 0 and !(x & (x - 1)) will check if x is a power of 2
  // or not
  return (x && !(x & (x - 1)));
}

ll ldd(ll n) {
  for (ll i = 3; i * i <= n; i += 2)
    if (n % i == 0)
      return i;
}

void solve() {
  ll n, x, ans = 0;
  cin >> n;
  vi v;
  rep(i, 0, n) cin >> x, v.pb(x), ans += x;
  ll q;
  cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    ll c = count(all(v), a);
    ans = ans - c * a + b * c;
    cout << ans << "\\n";
    replace(all(v), a, b);
  }
}

int main() {
  fastio ll t = 1;
  while (t--)
    solve();
  return 0;
}