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

#include <bits/stdc++.h>
#define MAX 1000000
#define MOD 1e9
#define pb push_back
#define pairs pair<int, int>
#define vi vector<int>
#define vb vector<bool>
#define vii vector<pairs>
#define lb lower_bound
#define ub upper_bound
#define ll long long
#define endl '\\n'
using namespace std;
/* -----------------------------------------------------------------------------------
 */

#define FastInput ios_base::sync_with_stdio(false), cin.tie(NULL);
#define all(x) x.begin(), x.end()
#define read(x) freopen(x, "r", stdin)
#define write(x) freopen(x, "w", stdout)
#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, a, n) for (int i = a; i <= n; i++)
#define inputArray(a, n) rep(i, 0, n) cin >> a[i];
#define copyArray(a, temp, n) rep(i, 0, n) temp[i] = a[i];
#define printArray(a, n)                                                       \\
  rep(i, 0, n) cout << a[i] << " ";                                            \\
  cout << endl;

/* -----------------------------------------------------------------------------------
 */

#define Cases cout << "Case " << ++Case << ": ";
#define __test                                                                 \\
  int tt;                                                                      \\
  int Case = 0;                                                                \\
  cin >> tt;                                                                   \\
  while (tt--)

#define dbgA2(A, n, m)                                                         \\
  {                                                                            \\
    cout << "--> " << #A << " = \\n";                                           \\
    rep(i, 0, n) {                                                             \\
      rep(j, 0, n) { cout << A[i][j] << " "; }                                 \\
      cout << "\\n";                                                            \\
    }                                                                          \\
    cout << "\\n";                                                              \\
  }
#define dbgA(A, n)                                                             \\
  {                                                                            \\
    cout << " --> " << #A << " = (";                                           \\
    rep(i, 0, n) cout << A[i] << " ";                                          \\
    cout << ")\\n";                                                             \\
  }
#define dbg(args...)                                                           \\
  {                                                                            \\
    string sss(#args);                                                         \\
    sss += ',';                                                                \\
    cout << " --> ";                                                           \\
    debugger::call(all(sss), args);                                            \\
    cout << "\\n";                                                              \\
  }

/* -----------------------------------------------------------------------------------
 */

ll gcd(ll n, ll m) { return m ? gcd(m, n % m) : n; }
ll lcm(ll n, ll m) { return n / gcd(n, m) * m; }

struct debugger {
  typedef string::iterator si;
  static void call(si it, si ed) {}
  template <typename T, typename... aT>
  static void call(si it, si ed, T a, aT... rest) {
    string b;
    for (; *it != ','; ++it)
      if (*it != ' ')
        b += *it;
    cout << b << "=" << a << " ";
    call(++it, ed, rest...);
  }
};

/* -----------------------------------------------------------------------------------
 */

vector<ll> arr(100005);

void solve() {
  int n;
  cin >> n;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    ll k;
    cin >> k;
    sum += k;
    arr[k]++;
  }

  int test;
  cin >> test;
  while (test--) {
    ll a, b;
    cin >> a >> b;
    sum -= arr[a] * a;
    sum += arr[a] * b;
    arr[b] += arr[a];
    arr[a] = 0;
    cout << sum << endl;
  }
}

int main() {
  solve();

  // __test {
  // 	solve();
  // }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define MAX 100000000000000000
#define INF 1000000
#define MOD 1e9
#define pb push_back
#define pairs pair<int, int>
#define vi vector<int>
#define vb vector<bool>
#define vii vector<pairs>
#define lb lower_bound
#define ub upper_bound
#define ll long long
#define endl '\\n'
using namespace std;
/* -----------------------------------------------------------------------------------
 */

#define FastInput ios_base::sync_with_stdio(false), cin.tie(NULL);
#define all(x) x.begin(), x.end()
#define read(x) freopen(x, "r", stdin)
#define write(x) freopen(x, "w", stdout)
#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, a, n) for (int i = a; i <= n; i++)
#define inputArray(a, n) rep(i, 0, n) cin >> a[i];
#define copyArray(a, temp, n) rep(i, 0, n) temp[i] = a[i];
#define printArray(a, n)                                                       \\
  rep(i, 0, n) cout << a[i] << " ";                                            \\
  cout << endl;

/* -----------------------------------------------------------------------------------
 */

#define Cases cout << "Case " << ++Case << ": ";
#define __test                                                                 \\
  int tt;                                                                      \\
  int Case = 0;                                                                \\
  cin >> tt;                                                                   \\
  while (tt--)

#define dbgA2(A, n, m)                                                         \\
  {                                                                            \\
    cout << "--> " << #A << " = \\n";                                           \\
    rep(i, 0, n) {                                                             \\
      rep(j, 0, n) { cout << A[i][j] << " "; }                                 \\
      cout << "\\n";                                                            \\
    }                                                                          \\
    cout << "\\n";                                                              \\
  }
#define dbgA(A, n)                                                             \\
  {                                                                            \\
    cout << " --> " << #A << " = (";                                           \\
    rep(i, 0, n) cout << A[i] << " ";                                          \\
    cout << ")\\n";                                                             \\
  }
#define dbg(args...)                                                           \\
  {                                                                            \\
    string sss(#args);                                                         \\
    sss += ',';                                                                \\
    cout << " --> ";                                                           \\
    debugger::call(all(sss), args);                                            \\
    cout << "\\n";                                                              \\
  }

/* -----------------------------------------------------------------------------------
 */

ll gcd(ll n, ll m) { return m ? gcd(m, n % m) : n; }
ll lcm(ll n, ll m) { return n / gcd(n, m) * m; }

struct debugger {
  typedef string::iterator si;
  static void call(si it, si ed) {}
  template <typename T, typename... aT>
  static void call(si it, si ed, T a, aT... rest) {
    string b;
    for (; *it != ','; ++it)
      if (*it != ' ')
        b += *it;
    cout << b << "=" << a << " ";
    call(++it, ed, rest...);
  }
};

/* -----------------------------------------------------------------------------------
 */

int arr[100006];

ll sum = 0;

void solve(int n) {
  int a, b;
  cin >> a >> b;
  sort(arr, arr + n);

  int low = lower_bound(arr, arr + n, a) - arr;
  int up = upper_bound(arr, arr + n, a) - arr;
  REP(i, low, up) {
    if (arr[i] == a) {
      sum -= a;
      arr[i] = b;
      sum += b;
    }
  }
  cout << sum << endl;
}

int main() {
  // solve();

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  int test;
  cin >> test;
  for (int i = 0; i < test; i++) {
    solve(n);
  }
}