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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <ctype.h>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;
using PAIR = pair<int, int>;
using PAIRLL = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;

#define TR ","
#define fi first
#define se second
#define rep(i, N) for (ll i = 0; i < (ll)N; ++i)
#define repe(i, a, b) for (ll i = a; i < (ll)b; ++i)
#define all(v) v.begin(), v.end()
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define INF 100000000

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

ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

ll waz = 76543217;

ll inv(ll x) {
  if (x == 1) {
    return 1;
  }
  if (x == 0) {
    return 0;
  } else {
    ll a = (waz - inv(waz % x) * (waz / x)) % waz;
    if (a >= 0) {
      return a;
    } else {
      return waz + a;
    }
  }
}

void printVector(const vector<int> &vec) {
  for (int value : vec) {
    cout << value << " ";
  }
  cout << endl;
}

vector<ll> p;

ll phi(ll x) {
  ll a = x;
  ll b = x;
  for (ll k = 0; k != p.size(); ++k) {
    if (a % p[k] == 0) {
      while (a % p[k] == 0) {
        a /= p[k];
      }
      b = b / p[k] * (p[k] - 1);
    }
    if (a == 1) {
      break;
    }
  }
  return b;
}

bool my_compare(pair<string, int> a, pair<string, int> b) {
  if (a.first != b.first) {
    return a.first < b.first;
  }

  if (a.second != b.second) {
    return a.second > b.second;
  } else {
    return true;
  }
}

int main() {
  IO;

  ll N, Q;
  cin >> N;
  deque<ll> A(N);
  rep(i, N) cin >> A[i];
  sort(all(A));
  cin >> Q;

  ll B, C;
  vector<PAIRLL> PA(Q);

  map<ll, ll> ma;

  rep(i, Q) {
    cin >> B >> C;
    PA[i] = make_pair(B, C);
  }

  ll ans = 0;

  rep(i, N) { ans += A[i]; }

  rep(i, N) {
    ++ma[A[0]];
    A.pop_front();
  }

  rep(i, Q) {
    ans += ma[PA[i].first] * (PA[i].second - PA[i].first);
    ma[PA[i].second] += ma[PA[i].first];
    ma[PA[i].first] = 0;
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <ctype.h>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;
using PAIR = pair<int, int>;
using PAIRLL = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;

#define TR ","
#define fi first
#define se second
#define rep(i, N) for (ll i = 0; i < (ll)N; ++i)
#define repe(i, a, b) for (ll i = a; i < (ll)b; ++i)
#define all(v) v.begin(), v.end()
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define INF 100000000

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

ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

ll waz = 76543217;

ll inv(ll x) {
  if (x == 1) {
    return 1;
  }
  if (x == 0) {
    return 0;
  } else {
    ll a = (waz - inv(waz % x) * (waz / x)) % waz;
    if (a >= 0) {
      return a;
    } else {
      return waz + a;
    }
  }
}

void printVector(const vector<int> &vec) {
  for (int value : vec) {
    cout << value << " ";
  }
  cout << endl;
}

vector<ll> p;

ll phi(ll x) {
  ll a = x;
  ll b = x;
  for (ll k = 0; k != p.size(); ++k) {
    if (a % p[k] == 0) {
      while (a % p[k] == 0) {
        a /= p[k];
      }
      b = b / p[k] * (p[k] - 1);
    }
    if (a == 1) {
      break;
    }
  }
  return b;
}

bool my_compare(pair<string, int> a, pair<string, int> b) {
  if (a.first != b.first) {
    return a.first < b.first;
  }

  if (a.second != b.second) {
    return a.second > b.second;
  } else {
    return true;
  }
}

int main() {
  IO;

  ll N, Q;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  sort(all(A));
  cin >> Q;

  ll B, C;
  vector<PAIRLL> PA(Q);

  map<ll, ll> ma;

  rep(i, Q) {
    cin >> B >> C;
    ma[B] = count(all(A), B);
    PA[i] = make_pair(B, C);
  }

  ll ans = 0;

  rep(i, N) { ans += A[i]; }

  rep(i, Q) {
    ans += ma[PA[i].first] * (PA[i].second - PA[i].first);
    ma[PA[i].second] += ma[PA[i].first];
    ma[PA[i].first] = 0;
    cout << ans << endl;
  }
}