alexjercan/bugnet huggingface dataset:
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;
using p = pair<int, int>;
using ll = long long;

int main() {
  int n;
  cin >> n;

  vector<int> a(n);
  vector<ll> A(100005);

  ll sum = 0;
  rep(i, n) {
    cin >> a.at(i);
    A.at(a.at(i))++;
    sum += a.at(i);
  }

  int q, b, c;
  cin >> q;
  rep(i, q) {
    cin >> b >> c;

    ll tmp;
    tmp = A.at(b);
    A.at(b) = 0;
    A.at(c) += tmp;

    sum -= b * tmp;
    sum += c * tmp;
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;
using p = pair<int, int>;
using ll = long long;

int main() {
  int n;
  cin >> n;

  vector<int> a(n);
  vector<int> A(100005);

  int max1 = 0;
  rep(i, n) {
    cin >> a.at(i);
    A.at(a.at(i))++;
    max1 = max(max1, a.at(i));
  }

  int q, b, c;
  cin >> q;
  int max2;
  rep(i, q) {
    cin >> b >> c;
    max2 = max(max1, b);
    max2 = max(max2, c);

    int tmp;
    tmp = A.at(b);
    A.at(b) = 0;
    A.at(c) += tmp;

    int sum = 0;
    rep(k, max2 + 1) { sum += k * A.at(k); }
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iomanip> // std::setprecision()
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define yes cout << "Yes" << endl;
#define no cout << "No" << endl;
#define pai 3.14159265358979323846
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
/* map<string,int> */
/* 大文字を小文字に変換 tolower*/
/* 小文字を大文字に変換 toupper*/
/* 辞書順 next_permutation(a.begin(),a.end()) */

int main() {
  ll n;
  ll b;
  map<ll, ll> a;
  cin >> n;
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    cin >> b;
    a[b]++;
    sum += b;
  }
  ll q;
  cin >> q;
  pair<ll, ll> c;
  vector<ll> ans(q);
  for (ll i = 0; i < q; i++) {
    cin >> c.first >> c.second;
    decltype(a)::iterator it = a.find(c.first);
    if (it != a.end()) {
      ll f = a[c.first];
      a[c.first] = 0;
      a[c.second] += f;
      sum -= c.first * f;
      sum += c.second * f;
      ans[i] = sum;
    } else {
      ans[i] = sum;
    }
  }
  for (ll i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip> // std::setprecision()
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define yes cout << "Yes" << endl;
#define no cout << "No" << endl;
#define pai 3.14159265358979323846
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
/* map<string,int> */
/* 大文字を小文字に変換 tolower*/
/* 小文字を大文字に変換 toupper*/
/* 辞書順 next_permutation(a.begin(),a.end()) */

int main() {
  ll n;
  ll b;
  map<ll, ll> a;
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> b;
    a[b]++;
  }
  ll q;
  cin >> q;
  pair<ll, ll> c;
  map<ll, ll> ans;
  for (ll i = 0; i < q; i++) {
    cin >> c.first >> c.second;
    decltype(a)::iterator it = a.find(c.first);
    if (it != a.end()) {
      ll f = a[c.first];
      a[c.first] = 0;
      a[c.second] += f;
    }
    ans[i] = 0;
    for (auto j = a.begin(); j != a.end(); j++) {
      // cout << j->first << ':' << j->second << endl;
      ans[i] += j->first * j->second;
    }
    // return 0;
  }
  for (ll i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

// Code for D - Replacing
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, x, y) for (ll i = x; i < (ll)(y); i++)

#define debug(var)                                                             \\
  do {                                                                         \\
    std::cout << #var << " : ";                                                \\
    view(var);                                                                 \\
  } while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}

int main() {
  ll N;
  cin >> N;

  map<ll, ll> m;
  vector<ll> A(N);
  REP(i, N) { cin >> A[i]; }

  ll Q;
  cin >> Q;
  vector<ll> B(Q), C(Q);
  REP(i, Q) { cin >> B[i] >> C[i]; }

  ll total = 0;
  REP(i, N) {
    total += A[i];
    m[A[i]] += 1;
  }

  REP(i, Q) {
    total += (C[i] - B[i]) * m[B[i]];
    m[C[i]] += m[B[i]];
    m[B[i]] = 0;
    cout << total << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

// Code for D - Replacing
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, x, y) for (ll i = x; i < (ll)(y); i++)

#define debug(var)                                                             \\
  do {                                                                         \\
    std::cout << #var << " : ";                                                \\
    view(var);                                                                 \\
  } while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}

int main() {
  ll N;
  cin >> N;

  map<ll, ll> m;
  vector<ll> A(N);
  REP(i, N) { cin >> A[i]; }

  ll Q;
  cin >> Q;
  vector<ll> B(Q), C(Q);
  REP(i, Q) { cin >> B[i] >> C[i]; }

  REP(i, N) { m[A[i]] += 1; }

  REP(i, Q) {
    m[C[i]] += m[B[i]];
    m[B[i]] = 0;
    ll total = 0;
    for (auto i = m.begin(); i != m.end(); ++i) {
      total += i->first * i->second;
    }
    cout << total << endl;
  }

  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#define ll long long

int main() {
  ll N;
  ll sum = 0;
  std::cin >> N;

  std::map<ll, ll> A;

  for (ll i = 0; i < N; i++) {
    ll x;
    std::cin >> x;
    sum += x;
    A[x]++;
  }

  ll Q;
  std::cin >> Q;
  ll q = Q;
  std::vector<ll> sum_list(Q);
  ll i = 0;

  while (q > 0) {
    // std::cout << "!??" << std::endl;
    ll B, C;
    std::cin >> B;
    std::cin >> C;
    ll diff = C - B;
    sum += diff * A[B];
    A[C] += A[B];
    A[B] = 0;
    sum_list[i] = sum;
    i++;
    q--;
  }

  for (ll j = 0; j < Q; j++) {
    std::cout << sum_list[j] << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define ll long long

int main() {
  ll N;
  std::cin >> N;

  std::vector<ll> A(N);
  for (ll i = 0; i < N; i++) {
    std::cin >> A[i];
  }

  ll sum = 0;
  for (ll i = 0; i < N; i++) {
    sum += A[i];
  }

  ll Q;
  std::cin >> Q;
  std::vector<ll> sum_list(Q);

  for (ll i = 0; i < Q; i++) {
    std::sort(A.begin(), A.end(), std::greater<ll>());

    ll B, C;
    int value_check = 0;
    std::cin >> B;
    std::cin >> C;

    ll diff = C - B;

    ll start = 0;
    ll end = N - 1;
    ll point = -1;

    while (start <= end) {
      ll middle = (start + end) / 2;
      if (B > A[middle]) {
        end = middle - 1;
      } else if (B < A[middle]) {
        start = middle + 1;
      } else {
        point = middle;
        break;
      }
    }

    if (point == -1) { // Bの値が存在しない時
      sum_list[i] = sum;
    } else {
      while (true) {
        if ((point - 1) < 0) {
          break;
        } else if (B != A[point - 1]) {
          break;
        } else {
          point--;
        }
      }
      while (true) {
        if (A[point] == B) {
          sum += diff;
          A[point] = C;
          point++;
        } else {
          sum_list[i] = sum;
          break;
        }
      }
    }
  }

  for (ll i = 0; i < Q; i++) {
    std::cout << sum_list[i] << std::endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  ll arr[n], sum = 0;
  int ar[100001] = {0};
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
    ar[arr[i]]++;
  }

  int q;
  cin >> q;
  ll b, c;
  while (q--) {
    cin >> b >> c;
    if (ar[b] == 0) {
      cout << sum << '\\n';
      continue;
    }
    ll val = ar[b];
    ll total = val * b;
    sum -= total;
    ar[b] -= val;
    ar[c] += val;
    sum += (c * val);
    cout << sum << '\\n';
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  ll arr[n], sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }

  int q;
  cin >> q;
  ll b, c;
  while (q--) {
    cin >> b >> c;
    sort(arr, arr + n);
    int index = lower_bound(arr, arr + n, b) - arr;
    if (index > n) {
      cout << sum << '\\n';
      continue;
    }
    if (arr[index] != b) {
      cout << sum << '\\n';
      continue;
    }
    int up = upper_bound(arr, arr + n, b) - arr;
    ll total = (up - index) * b;
    sum -= total;
    sum += ((up - index) * c);
    for (int i = index; i < up; i++)
      arr[i] = c;
    cout << sum << '\\n';
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
using ll = long long;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> cnt(100010);
  ll ans = 0;
  ll b[100010], c[100010];
  ll q;
  rep(i, n) {
    cin >> a[i];
    ans += a[i];
    a[i]--;
    cnt[a[i]]++;
  }
  cin >> q;
  rep(i, q) cin >> b[i] >> c[i];

  rep(i, q) {
    ans += (c[i] - b[i]) * cnt[b[i] - 1];
    cnt[c[i] - 1] += cnt[b[i] - 1];
    cnt[b[i] - 1] = 0;
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
using ll = long long;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> cnt(n);
  ll ans = 0;
  ll b[100010], c[100010];
  ll q;
  rep(i, n) {
    cin >> a[i];
    ans += a[i];
    a[i]--;
    cnt[a[i]]++;
  }
  cin >> q;
  rep(i, q) cin >> b[i] >> c[i];

  rep(i, q) {
    ans += (c[i] - b[i]) * cnt[b[i] - 1];
    cnt[c[i] - 1] += cnt[b[i] - 1];
    cnt[b[i] - 1] = 0;
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  ll n, q;
  cin >> n;
  vector<ll> v;
  vector<ll> f(100005, 0);
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    ll temp;
    cin >> temp;
    sum += temp;
    f[temp]++;
    v.push_back(temp);
  }
  cin >> q;
  bool flag = false;
  while (q--) {
    ll a, b, s = sum;
    cin >> a >> b;
    if (f[a]) {
      flag = true;
      s = s - f[a] * a;
      s = s + f[a] * b;
      f[b] += f[a];
      f[a] = 0;
      sum = s;
      cout << sum << endl;
    } else
      cout << s << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  ll n, q;
  cin >> n;
  vector<ll> v;
  for (ll i = 0; i < n; i++) {
    ll temp;
    cin >> temp;
    v.push_back(temp);
  }
  cin >> q;
  while (q--) {
    ll a, b, sum = 0, prev_sum = 0;
    bool flag = false;
    cin >> a >> b;
    int pos = find(v.begin(), v.end(), a) - v.begin();
    if (pos < v.size()) {
      flag = true;
      for (ll i = 0; i < v.size(); i++)
        if (v[i] == a)
          v[i] = b;
      for (ll i = 0; i < v.size(); i++)
        sum += v[i];
      prev_sum = sum;
    } else {
      if (flag)
        sum = prev_sum;
      else {
        for (ll i = 0; i < v.size(); i++)
          sum += v[i];
        prev_sum = sum;
      }
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

unordered_map<ll, int> count_values(const vector<ll> &xs) {
  unordered_map<ll, int> ans;
  for (const auto &x : xs)
    ++ans[x];
  return ans;
}

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

  int sz;
  cin >> sz;
  vector<ll> xs(sz, 0LL);
  for (auto &x : xs)
    cin >> x;
  ll s = accumulate(xs.cbegin(), xs.cend(), 0LL);

  auto cnt = count_values(xs);

  int q;
  cin >> q;

  while (q--) {
    int b, c;
    cin >> b >> c;
    const int d = b - c;
    s -= cnt[b] * d;
    cnt[c] += cnt[b];
    cnt[b] = 0;

    cout << s << '\\n';
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

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

  int sz;
  cin >> sz;
  vector<ll> xs(sz, 0LL);
  for (auto &x : xs)
    cin >> x;
  ll s = accumulate(xs.cbegin(), xs.cend(), 0LL);

  unordered_map<ll, vector<int>> indices_by_value;
  for (int i = 0; i < sz; ++i) {
    indices_by_value[xs[i]].push_back(i);
  }

  int q;
  cin >> q;

  while (q--) {
    int b, c;
    cin >> b >> c;
    int d = b - c;

    for (auto i : indices_by_value[b]) {
      s -= d;
      xs[i] = c;
      indices_by_value[c].push_back(i);
    }
    indices_by_value[b] = {};

    cout << s << '\\n';
  }

  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define F first
#define S second
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define sizeOf(A) (sizeof(A)) / (sizeof(A[0]))
#define odd(x) ((x) % (2))
#define fastio ios_base::sync_with_stdio(false), cin.tie(0);
#define fileio                                                                 \\
  freopen("template.in", "r", stdin), freopen("template.out", "w", stdout);
typedef long long ll;
const ll MOD = 1e9 + 7;

int main() {
  fastio;
  // fileio;

  ll n;
  cin >> n;
  unordered_map<ll, ll> B;
  ll sum = 0;
  FOR(i, 0, 1e5) { B[i + 1] = 0; }
  FOR(i, 0, n) {
    ll x;
    cin >> x;
    B[x]++;
    sum += x;
  }
  ll Q;
  cin >> Q;
  FOR(i, 0, Q) {
    ll a, b;
    cin >> a >> b;
    ll x = B[a];
    B[a] = 0;
    sum += (b - a) * x;
    B[b] += x;
    cout << sum << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define F first
#define S second
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define sizeOf(A) (sizeof(A)) / (sizeof(A[0]))
#define odd(x) ((x) % (2))
#define fastio ios_base::sync_with_stdio(false), cin.tie(0);
#define fileio                                                                 \\
  freopen("template.in", "r", stdin), freopen("template.out", "w", stdout);
typedef long long ll;
const ll MOD = 1e9 + 7;

int main() {
  fastio;
  // fileio;

  ll n;
  cin >> n;
  int A[n];
  ll sum = 0;
  FOR(i, 0, n) {
    cin >> A[i];
    sum += A[i];
  }
  ll Q;
  cin >> Q;
  FOR(i, 0, Q) {
    ll a, b;
    cin >> a >> b;
    FOR(j, 0, n) {
      if (A[j] == a) {
        sum += (b - a);
        A[j] = b;
      }
    }
    cout << sum << "\\n";
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int N;
  cin >> N;
  long long sum = 0;
  vector<long long> number(10000001, 0);
  rep(i, N) {
    long long a;
    cin >> a;
    sum += a;
    number.at(a)++;
  }
  long long q;
  cin >> q;
  rep(i, q) {
    long long a, b;
    cin >> a >> b;
    sum += (b - a) * number.at(a);
    number.at(b) += number.at(a);
    number.at(a) = 0;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int N;
  cin >> N;
  long long sum = 0;
  vector<long long> number(N);
  rep(i, N) { cin >> number.at(i); }
  rep(i, N) sum += number.at(i);
  long long q;
  cin >> q;
  rep(i, q) {
    long long a, b;
    cin >> a >> b;
    for (int j = 0; j < N; j++) {
      if (number.at(j) == a) {
        sum = sum - a;
        sum = sum + b;
        number.at(j) = b;
      }
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int a[n];
  int cnt[100001] = {0};
  long s = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
    s += a[i];
  }
  int q;
  cin >> q;
  int b, c;
  while (q--) {
    cin >> b >> c;
    s += ((c - b) * cnt[b]);
    cout << s << endl;
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int a[n];
  vector<set<int>> idx(100000 + 1);
  long s = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    idx[a[i]].insert(i);
    s += a[i];
  }
  int q;
  cin >> q;
  int b, c;
  while (q--) {
    cin >> b >> c;
    if (idx[b].empty())
      cout << s << "\\n";
    else {
      s += ((c - b) * (idx[b].size()));
      cout << s << "\\n";
      // for(int i=0;i<idx[b].size();i++)
      for (int x : idx[b]) {
        a[x] = c;
        idx[c].insert(x);
      }
      idx[b].clear();
    }
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int q;
  cin >> q;
  vector<int> b(q);
  vector<int> c(q);
  for (int i = 0; i < q; i++) {
    cin >> b[i] >> c[i];
  }
  vector<int> d(q);
  for (int i = 0; i < q; i++) {
    d[i] = c[i] - b[i];
  }

  sort(a.begin(), a.end());
  vector<int> k(100000);
  for (int i = 0; i < n; i++) {
    k[a[i] - 1]++;
  }

  long long s = 0;
  for (int i = 0; i < n; i++) {
    s += a[i];
  }

  for (int i = 0; i < q; i++) {
    s += k[b[i] - 1] * d[i];
    cout << s << endl;
    k[c[i] - 1] += k[b[i] - 1];
    k[b[i] - 1] = 0;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int q;
  cin >> q;
  vector<int> b(q);
  vector<int> c(q);
  for (int i = 0; i < q; i++) {
    cin >> b[i] >> c[i];
  }
  vector<int> d(q);
  for (int i = 0; i < q; i++) {
    d[i] = c[i] - b[i];
  }

  sort(a.begin(), a.end());
  vector<int> k(a[n - 1]);
  for (int i = 0; i < n; i++) {
    k[a[i] - 1]++;
  }

  long long s = 0;
  for (int i = 0; i < n; i++) {
    s += a[i];
  }

  for (int i = 0; i < q; i++) {
    s += k[b[i] - 1] * d[i];
    cout << s << endl;
    k[c[i] - 1] += k[b[i] - 1];
    k[b[i] - 1] = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

// A.cpp : This file contains the 'main' function. Program execution begins and
// ends there.
//

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {
  unsigned long long N;
  cin >> N;

  map<unsigned long long, unsigned long long> am;
  unsigned long long sum = 0u;
  for (unsigned long long i = 0; i < N; ++i) {
    unsigned long long a;
    cin >> a;
    if (am.find(a) == am.end()) {
      am[a] = 1;
    } else {
      am[a]++;
    }
    sum += a;
  }

  unsigned long long Q;
  cin >> Q;

  vector<unsigned long long> bv;
  vector<unsigned long long> cv;
  for (unsigned long long i = 0; i < Q; ++i) {
    unsigned long long b;
    cin >> b;
    bv.emplace_back(b);

    unsigned long long c;
    cin >> c;
    cv.emplace_back(c);
  }

  unsigned long long currentSum = sum;
  for (unsigned long long i = 0; i < Q; ++i) {
    if (am.find(bv[i]) != am.end()) {
      auto changedCount = am[bv[i]];
      if (am.find(cv[i]) == am.end()) {
        am[cv[i]] = am[bv[i]];
        am.erase(bv[i]);
      } else {
        am[cv[i]] += am[bv[i]];
        am.erase(bv[i]);
      }
      currentSum += (changedCount * (cv[i] - bv[i]));
    }

    cout << currentSum << endl;
    // for (unsigned long long i = 0; i < N; ++i)
    //{
    //	cout << av[i] << ", ";
    // }
    // cout << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  unsigned long long N;
  cin >> N;

  vector<unsigned long long> av;
  unsigned long long sum = 0u;
  for (unsigned long long i = 0; i < N; ++i) {
    unsigned long long a;
    cin >> a;
    av.emplace_back(a);
    sum += a;
  }

  unsigned long long Q;
  cin >> Q;

  vector<unsigned long long> bv;
  vector<unsigned long long> cv;
  for (unsigned long long i = 0; i < Q; ++i) {
    unsigned long long b;
    cin >> b;
    bv.emplace_back(b);

    unsigned long long c;
    cin >> c;
    cv.emplace_back(c);
  }

  sort(av.begin(), av.end());

  unsigned long long currentSum = sum;
  for (unsigned long long i = 0; i < Q; ++i) {
    unsigned long long j = 0u;
    unsigned long long changedCount = 0u;
    bool isFound = false;
    while (j < N && !isFound) {
      if (av[j] == bv[i]) {
        while ((j < N) && (av[j] == bv[i])) {
          av[j] = cv[i];
          ++j;
          ++changedCount;
          isFound = true;
        }
        break;
      } else {
        ++j;
      }
    }

    // for (unsigned long long i = 0; i < N; ++i)
    //{
    //	cout << av[i] << ", ";
    // }
    // cout << endl;

    currentSum += (changedCount * (cv[i] - bv[i]));
    cout << currentSum << endl;

    sort(av.begin(), av.end());
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
  ll n, k;
  ll a[1000000], b, c, cun[1000000] = {};
  ll sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    cun[a[i]]++;
  }
  ll sum2 = sum;

  cin >> k;
  for (int i = 0; i < k; i++) {
    cin >> b >> c;
    sum -= cun[b] * b;
    sum += cun[b] * c;
    cout << sum << "\\n";
    cun[c] += cun[b];
    cun[b] = 0;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
  ll n, k;
  ll a[1000000], b, c;
  ll sum;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> k;
  for (int i = 0; i < k; i++) {
    cin >> b >> c;
    sum = 0;
    for (int j = 0; j < n; j++) {
      if (b == a[j]) {
        a[j] = c;
      }
      sum += a[j];
    }
    cout << sum << "\\n";
  }

  return 0;
}