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

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
#define PI 3.1415926535897932384626
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;

#define Rep(i, a, b) for (int i = a; i < (b); i++)
#define rep(i, a) for (int i = 0; i < (a); i++)
#define Dep(i, a, b) for (int i = (b)-1; i >= a; i--)
#define dep(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert

mt19937
    rng(chrono::steady_clock::now().time_since_epoch().count()); // Random No.

const int MOD = 1000000007;
const char nl = '\\n';

void sol() {
  int N;
  cin >> N;
  int ar[N];
  ll sum = 0;
  set<int> s;
  rep(i, N) {
    cin >> ar[i];
    sum += ar[i];
    s.insert(ar[i]);
  }

  map<ll, ll> m;
  trav(x, s) {
    int countt = count(ar, ar + N, x);
    m[x] = countt;
  }

  int Q;
  cin >> Q;
  while (Q--) {
    int B, C;
    cin >> B >> C;
    sum += (C - B) * (m[B]);

    auto it = m.find(C);
    if (it != m.end()) {
      m[C] = m[C] + m[B];
      m.erase(B);
    } else {
      m[C] = m[B];
      m.erase(B);
    }
    cout << sum << nl;
  }
}

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

#ifndef ONLINE_JUDGE
  freopen("inputD.txt", "r", stdin);
#endif

  int T = 1;
  while (T--) {
    sol();
  }

  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
#define PI 3.1415926535897932384626
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;

#define Rep(i, a, b) for (int i = a; i < (b); i++)
#define rep(i, a) for (int i = 0; i < (a); i++)
#define Dep(i, a, b) for (int i = (b)-1; i >= a; i--)
#define dep(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert

mt19937
    rng(chrono::steady_clock::now().time_since_epoch().count()); // Random No.

const int MOD = 1000000007;
const char nl = '\\n';

void sol() {
  int N;
  cin >> N;
  int ar[N];
  rep(i, N) { cin >> ar[i]; }
  int Q;
  cin >> Q;
  while (Q--) {
    int B, C;
    cin >> B >> C;
    ll sum = 0;
    rep(i, N) {

      if (ar[i] == B) {
        ar[i] = C;
      }
      sum += ar[i];
    }
    cout << sum << endl;
  }
}

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

#ifndef ONLINE_JUDGE
  freopen("inputD.txt", "r", stdin);
#endif

  int T = 1;
  while (T--) {
    sol();
  }

  return 0;
}