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

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define fastIO                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long int
#define pb push_back
#define ff first
#define ss second
#define all(v) (v).begin(), (v).end()
#define mod (int)(998244353)
#define PI 3.14159265358979323846264338327950L
#define debug(vec)                                                             \\
  cout << (#vec) << ": ";                                                      \\
  for (auto &i : vec)                                                          \\
    cout << i << " ";                                                          \\
  cout << endl
#define trace(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
    cout << endl;                                                              \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << "[ " << *it << " = " << a << " ] ";
  err(++it, args...);
}

signed main() {

  fastIO

      int n;
  cin >> n;
  unordered_map<int, int> mp;
  int sum = 0;
  for (int i = 0, a; i < n; i++) {
    cin >> a;
    mp[a]++;
    sum += a;
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int a, b;
    cin >> a >> b;
    if (mp[a] != 0 && a != b) {
      sum -= mp[a] * a;
      mp[b] += mp[a];
      sum += mp[a] * b;
      mp[a] = 0;
    }
    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define fastIO                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long int
#define pb push_back
#define ff first
#define ss second
#define all(v) (v).begin(), (v).end()
#define mod (int)(998244353)
#define PI 3.14159265358979323846264338327950L
#define debug(vec)                                                             \\
  cout << (#vec) << ": ";                                                      \\
  for (auto &i : vec)                                                          \\
    cout << i << " ";                                                          \\
  cout << endl
#define trace(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
    cout << endl;                                                              \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << "[ " << *it << " = " << a << " ] ";
  err(++it, args...);
}
class BIT {
public:
  vector<int> bit;
  int N;
  /**
   * Creating tree is like updating Fenwick tree for every value in array
   */
  BIT(int arr[], int n) { // set the argument accordingly to the array type
    N = n;
    bit.assign(N + 1, 0);
    for (int i = 1; i <= N; i++) {
      updateBIT(arr[i - 1], i);
    }
  }
  /**
   * Start from index+1 if you updating index in original array. Keep adding
   * this value for next node till you reach outside range of tree
   */
  void updateBIT(int val, int idx) {
    while (idx < N + 1) {
      bit[idx] += val;
      idx = getNext(idx);
      // trace(idx)∫
    }
  }
  void update(int val, int idx) {
    int diff = val - bit[idx];
    // trace(diff)
    updateBIT(diff, idx);
  }

  /**
   * To get next
   * 1) 2's complement of get minus of index
   * 2) AND this with index
   * 3) Add it to index
   */
  int getNext(int idx) { return idx + (idx & -idx); }

  /**
   * Start from index+1 if you want prefix sum 0 to index. Keep adding value
   * till you reach 0
   */
  int getSum(int idx) {
    idx++;
    int sum = 0;
    while (idx > 0) {
      sum += bit[idx];
      idx = getParent(idx);
      // trace(idx, sum/)
    }
    return sum;
  }
  /**
   * To get parent
   * 1) 2's complement to get minus of index
   * 2) AND this with index
   * 3) Subtract that from index
   */
  int getParent(int idx) { return idx - (idx & -idx); }
};
signed main() {

  fastIO

      int t = 1;
  // cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    set<int> s;
    for (int &i : a) {
      cin >> i;
      s.insert(i);
    }
    // trace(n);
    BIT bit(a, n);
    // debug(bit.bit);
    int Q;
    cin >> Q;
    while (Q--) {
      int aa, b;
      cin >> aa >> b;
      for (int i = 0; i < n; i++) {
        if (a[i] == aa) {
          a[i] = b;
          // trace(aa);
          bit.updateBIT(b - aa, i + 1);
        }
      }
      // debug(a);
      int val = bit.getSum(n - 1);
      // trace(aa, b, val)
      cout << val << endl;
    }
  }

  return 0;
}