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

#include <bits/stdc++.h>

#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC target("avx")

#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define mpq priority_queue<int, vector<int>, greater<int>>
#define debug(a) cerr << #a << ": " << a << endl
#define umii unordered_map<int64_t, int64_t>
#define ftt                                                                    \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  for (int tt = 1; tt <= t; ++tt)
#define forr(i, n) for ((i) = 1; (i) <= (n); (i)++)
#define Rev(v) reverse(v.begin(), v.end());
#define Sort(v) sort(v.begin(), v.end());
#define mem(a, b) memset(a, b, sizeof(a))
#define deb(x) cout << "\\nhi" << x << "\\n"
#define pq priority_queue<int>
#define dbg cout << "\\nhi\\n"
#define sayy cout << "YES\\n"
#define sayn cout << "NO\\n"
#define int long long
#define double long double
#define pii pair<int, int>
#define mii map<int, int>
#define vb vector<bool>
#define vi vector<int>
#define eb emplace_back
#define nl cout << "\\n"
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define sp << " " <<
#define se second
#define fi first

const int MOD = 1000000007;
const double PI = 3.1415926535898;
const int LIM = 100005;

using namespace std;

int fpow(int x, int y) {
  int temp;
  if (y == 0)
    return 1;
  temp = fpow(x, y / 2);
  if (y % 2 == 0)
    return temp * temp;
  else
    return x * temp * temp;
}

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

void sieve(int n) {
  bool prime[5 * LIM];
  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p <= n; p++)
    if (prime[p] == true)
      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
  prime[1] = 0;
}

inline int read() {
  int p = 1, ans = 0;
  char ch = getchar();
  while (ch < 48 || ch > 57) {
    if (ch == '-')
      p = -1;
    ch = getchar();
  }
  while (ch >= 48 && ch <= 57) {
    ans = (ans << 3) + (ans << 1) + ch - 48;
    ch = getchar();
  }
  return ans * p;
}

map<int, int> primeFactors(int n) {
  map<int, int> mp;
  while (n % 2 == 0) {
    if (mp.count(2) == 1)
      mp[2]++;
    else
      mp.insert({2, 1});
    n = n / 2;
  }
  for (int i = 3; i <= sqrt(n); i = i + 2) {
    while (n % i == 0) {
      if (mp.count(i) == 1)
        mp[i]++;
      else
        mp.insert({i, 1});
      n = n / i;
    }
  }
  if (n > 2) {
    if (mp.count(n) == 1)
      mp[n]++;
    else
      mp.insert({n, 1});
  }
  return mp;
}

//*************************************************************************************************************************

int32_t main() {
  fio;

  int n, sum = 0;
  cin >> n;
  vi v(100000);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    sum += x;
    v[x - 1]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c, cnt = 0;
    cin >> b >> c;
    cnt = v[b - 1];
    v[b - 1] = 0;
    v[c - 1] += cnt;
    sum = sum + cnt * c - cnt * b;
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC target("avx")

#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define mpq priority_queue<int, vector<int>, greater<int>>
#define debug(a) cerr << #a << ": " << a << endl
#define umii unordered_map<int64_t, int64_t>
#define ftt                                                                    \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  for (int tt = 1; tt <= t; ++tt)
#define forr(i, n) for ((i) = 1; (i) <= (n); (i)++)
#define Rev(v) reverse(v.begin(), v.end());
#define Sort(v) sort(v.begin(), v.end());
#define mem(a, b) memset(a, b, sizeof(a))
#define deb(x) cout << "\\nhi" << x << "\\n"
#define pq priority_queue<int>
#define dbg cout << "\\nhi\\n"
#define sayy cout << "YES\\n"
#define sayn cout << "NO\\n"
#define int long long
#define double long double
#define pii pair<int, int>
#define mii map<int, int>
#define vb vector<bool>
#define vi vector<int>
#define eb emplace_back
#define nl cout << "\\n"
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define sp << " " <<
#define se second
#define fi first

const int MOD = 1000000007;
const double PI = 3.1415926535898;
const int LIM = 100005;

using namespace std;

int fpow(int x, int y) {
  int temp;
  if (y == 0)
    return 1;
  temp = fpow(x, y / 2);
  if (y % 2 == 0)
    return temp * temp;
  else
    return x * temp * temp;
}

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

void sieve(int n) {
  bool prime[5 * LIM];
  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p <= n; p++)
    if (prime[p] == true)
      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
  prime[1] = 0;
}

inline int read() {
  int p = 1, ans = 0;
  char ch = getchar();
  while (ch < 48 || ch > 57) {
    if (ch == '-')
      p = -1;
    ch = getchar();
  }
  while (ch >= 48 && ch <= 57) {
    ans = (ans << 3) + (ans << 1) + ch - 48;
    ch = getchar();
  }
  return ans * p;
}

map<int, int> primeFactors(int n) {
  map<int, int> mp;
  while (n % 2 == 0) {
    if (mp.count(2) == 1)
      mp[2]++;
    else
      mp.insert({2, 1});
    n = n / 2;
  }
  for (int i = 3; i <= sqrt(n); i = i + 2) {
    while (n % i == 0) {
      if (mp.count(i) == 1)
        mp[i]++;
      else
        mp.insert({i, 1});
      n = n / i;
    }
  }
  if (n > 2) {
    if (mp.count(n) == 1)
      mp[n]++;
    else
      mp.insert({n, 1});
  }
  return mp;
}

//*************************************************************************************************************************

int32_t main() {
  fio;

  int n, sum = 0;
  cin >> n;
  vi v(n);
  for (int &x : v) {
    cin >> x;
    sum += x;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c, cnt = 0;
    cin >> b >> c;
    for (int i = 0; i < n; i++) {
      if (v[i] == b) {
        v[i] = c;
        cnt++;
      }
    }
    sum = sum + cnt * c - cnt * b;
    cout << sum << endl;
  }
}