alexjercan/bugnet huggingface dataset:
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

ll cnt[100005];

int main() {
  // your code goes here
  int n;
  memset(cnt, 0, sizeof(cnt));
  cin >> n;
  int a[n + 1];
  ll sum = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    sum += a[i];
    ++cnt[a[i]];
  }

  int q;
  cin >> q;
  while (q-- > 0) {
    int idx, re;
    cin >> idx >> re;
    sum += cnt[idx] * re;
    sum -= cnt[idx] * idx;
    cnt[re] += cnt[idx];
    cnt[idx] = 0;
    cout << sum << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

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

ll cnt[100005];

int main() {
  // your code goes here
  int n;
  memset(cnt, 0, sizeof(cnt));
  int a[n + 1];
  cin >> n;
  ll sum = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    sum += a[i];
    ++cnt[a[i]];
  }

  int q;
  cin >> q;
  while (q-- > 0) {
    int idx, re;
    cin >> idx >> re;
    sum += cnt[idx] * re;
    sum -= cnt[idx] * idx;
    cnt[re] += cnt[idx];
    cnt[idx] = 0;
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long i, j, n, k = 0, x, y, q;
  cin >> n;
  long long a[n];
  unordered_map<long long, long long> mp;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
    k += a[i];
  }
  cin >> q;

  while (q--) {
    cin >> x >> y;
    if (mp.find(x) != mp.end()) {
      j = mp[x];
      mp.erase(x);
      mp[y] += j;
      k -= x * j;
      k += y * j;
    }
    cout << k << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long i, j, n, k = 0, x, y, q;
  cin >> n;
  long long a[n];
  unordered_map<long long, long long> mp;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
    k += a[i];
  }
  cin >> q;

  while (q--) {
    cin >> x >> y;
    if (mp.find(x) != mp.end()) {
      j = mp[x];
      while (mp[x]--)
        mp[y]++;
      k -= x * j;
      k += y * j;
    }
    cout << k << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
#define ll long long int
using namespace std;

int main()

{
  ll n, sum = 0;
  cin >> n;
  int lim = 1e5 + 90;
  ll cnt[lim] = {};

  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    sum = sum + x;
    cnt[x]++;
  }

  ll m;
  cin >> m;

  for (int i = 0; i < m; i++) {
    ll b, c;
    cin >> b >> c;

    if (cnt[b]) {
      sum += (c - b) * cnt[b];
      ll p = cnt[b];
      cnt[c] += p;
      cnt[b] = 0;
    }
    cout << sum << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

int main()

{
  ll n, sum = 0;
  cin >> n;

  ll arr[n + 1], cnt[n + 1];

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

  ll m;
  cin >> m;

  for (int i = 0; i < m; i++) {
    ll b, c, cnt = 0;
    cin >> b >> c;
    for (ll i = 0; i < n; i++) {
      if (b == arr[i]) {
        cnt++;
        arr[i] = c;
      }
      //	else cnt = 1;
    }

    if (b <= c)
      sum = sum + (c - b) * cnt;
    else
      sum = sum - (b - c) * cnt;
    cout << sum << endl;
  }

  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll n, q;
  cin >> n;
  ll ans = 0;
  vector<ll> cnt(110000, 0);
  for (ll i = 0; i < n; i++) {
    ll tmp;
    cin >> tmp;
    ans += tmp;
    cnt[tmp]++;
  }
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll x, y;
    cin >> x >> y;
    ans += (y - x) * cnt[x];
    cnt[y] += cnt[x];
    cnt[x] = 0;
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll n, q;
  cin >> n;
  ll ans = 0;
  vector<ll> cnt(11000, 0);
  for (ll i = 0; i < n; i++) {
    ll tmp;
    cin >> tmp;
    ans += tmp;
    cnt[tmp]++;
  }
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll x, y;
    cin >> x >> y;
    ans += (y - x) * cnt[x];
    cnt[y] += cnt[x];
    cnt[x] = 0;
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
using namespace std;
int main() {
  long n, sum = 0, q;
  map<long, long> x;
  cin >> n;
  for (long i = 0; i < n; i++) {
    int y;
    cin >> y;
    x[y]++;
    sum += y;
  }
  cin >> q;
  while (q--) {
    long a, b;
    cin >> a >> b;
    sum -= a * x[a];
    sum += b * x[a];
    x[b] += x[a];
    x[a] = 0;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long x, Q;
  cin >> x;
  long y[x];
  for (long i = 0; i < x; i++) {
    cin >> y[i];
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    long a, b, sum = 0;
    cin >> a >> b;
    for (int j = 0; j < x; j++) {
      if (a == y[j]) {
        y[j] = b;
      }
      sum += y[j];
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iomanip>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep_1 (i, j, n) for (int i = j; i < n; i++)
typedef long long ll;
typedef long l;
const ll l_INF = 101010101000;
const int INF = 1010101010;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;

int main() {
  int n;
  cin >> n;
  map<ll, ll> a;
  rep(i, n) {
    ll x;
    cin >> x;
    a[x] += x;
  }
  ll ans = 0;
  for (auto x : a) {
    ans += x.second;
  }
  int q;
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    a[c] += a[b] / b * c;
    ans += a[b] / b * c - a[b];
    a[b] = 0;
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iomanip>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep_1 (i, j, n) for (int i = j; i < n; i++)
typedef long long ll;
typedef long l;
const ll l_INF = 101010101000;
const int INF = 1010101010;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;

int main() {
  int n;
  cin >> n;
  map<ll, ll> a;
  rep(i, n) {
    ll x;
    cin >> x;
    a[x] += x;
  }
  int q;
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    a[c] += a[b] / b * c;
    a.erase(b);
    ll ans = 0;
    for (auto x : a) {
      ans += x.second;
    }
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded


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

#define ll long long
#define pb push_back
#define mp make_pair

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  ll int a[n], s = 0;
  map<ll int, ll int> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
    s += a[i];
  }
  int tt;
  cin >> tt;
  while (tt--) {
    ll b, c;
    cin >> b >> c;
    ll q = m[b];
    s = s - q * b + q * c;
    m[b] = 0;
    m[c] += q;
    cout << s << "\\n";
  }
}


Error: TLE

Fixed submission:


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

#define ll long long
#define pb push_back
#define mp make_pair

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int tt;
  cin >> tt;
  while (tt--) {
    ll s = 0;
    ll b, c;
    cin >> b >> c;
    for (int i = 0; i < n; i++) {
      if (a[i] == b)
        a[i] = c;

      s += a[i];
    }
    cout << s << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define pb push_back
using namespace std;
int main() {
  int n;
  cin >> n;
  long long int sum = 0;
  unordered_map<int, int> mp;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    sum += x;
    mp[x]++;
  }
  int q;
  cin >> q; // segment tree ka use karna hi hoga//
  while (q--) {
    int bi, ci;
    cin >> bi >> ci;
    auto it = mp.find(bi);
    if (it != mp.end()) {
      int z = ci - it->first;
      sum += (z * it->second);

      int dd = it->second;
      mp.erase(it);
      if (mp.find(ci) != mp.end()) {
        mp[ci] = mp[ci] + dd;
      } else
        mp[ci] = dd;
      cout << sum << endl;
    } else
      cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  int q;
  cin >> q;
  long long int sum = 0; // segment tree ka use karna hi hoga//
  sum = accumulate(v.begin(), v.end(), 0);
  while (q--) {
    int bi, ci;
    cin >> bi >> ci;
    for (int i = 0; i < n; i++) {
      if (v[i] == bi) {
        int z = ci - v[i];
        sum += z;

        v[i] = ci;
      }
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

int main() {
  vector<int> num(pow(10, 5) + 1);

  int n, q;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; i < pow(10, 5) + 1; j++) {
      if (a[i] == j) {
        num[j]++;
        break;
      }
    }
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    long int b, c;
    cin >> b >> c;
    sum = sum + (c - b) * num[b];
    cout << sum << endl;
    num[c] = num[c] + num[b];
    num[b] = 0;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

int main() {
  vector<int> num(10001);

  int n, q;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  long int sum = accumulate(a.begin(), a.end(), 0);

  for (int i = 0; i < n; i++) {
    for (int j = 0; i < 100001; j++) {
      if (a[i] == j) {
        num[j]++;
        break;
      }
    } // forjend
  }   // foriend
  vector<long int> answer(0);
  cin >> q;
  for (int i = 0; i < q; i++) {
    long int b = 0, c = 0;
    cin >> b >> c;
    sum = sum + (c - b) * num[b];
    answer.push_back(sum);
    num[c] = num[c] + num[b];
    num[b] = 0;
  }
  for (int i = 0; i < q; i++) {
    cout << answer[i] << endl;
  }

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

#include <bits/stdc++.h>
#define rep2(i, x, y) for (int i = x; i < (y); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
int INF = 100100101;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  map<int, int> mp;
  ll sum = 0;
  int a;
  rep(i, n) {
    cin >> a;
    sum += a;
    mp[a]++;
  }
  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * mp[b];
    cout << sum << endl;
    mp[c] += mp[b];
    mp[b] = 0;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep2(i, x, y) for (int i = x; i < (y); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
int INF = 100100101;

int vector_finder(std::vector<int> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 1;
  } else { // 発見できなかったとき
    return 0;
  }
}

vector<int> vector_fixer(vector<int> a, int target, int fixer) {
  while (vector_finder(a, target)) {
    vector<int>::iterator itr;
    itr = find(a.begin(), a.end(), target);
    int index = distance(a.begin(), itr);
    a[index] = fixer;
  }
  return a;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  int sum = 0;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
  }
  int q;
  cin >> q;
  vector<int> b(q);
  vector<int> c(q);
  rep(i, q) {
    cin >> b[i] >> c[i];
    int num = count(a.begin(), a.end(), b[i]);
    int gap = c[i] - b[i];
    sum += gap * num;
    cout << sum << endl;
    replace(a.begin(), a.end(), b[i], c[i]);
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> d(100000);
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i];
    d[a[i] - 1]++;
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * d[b - 1];
    d[c - 1] += d[b - 1];
    d[b - 1] = 0;

    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans += a[i];
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    auto x = lower_bound(a.begin(), a.end(), b);
    int y = *x;
    if (y == b) {
      while (a[x - a.begin()] == b) {
        a[x - a.begin()] = c;
        x++;
        ans -= b;
        ans += c;
      }
      sort(a.begin(), a.end());
    }

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

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < n; i++)
#define Rep(i, n) for (long long i = 1; i < n; i++)
#define ll long long
#include <cmath>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;
#define PI acos(-1)
using P = pair<int, int>;

ll ketasu(ll a) {
  ll num = 1;
  while (a / 10) {
    num++;
    a /= 10;
  }
  return num;
}

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll kosuu(ll a) {
  ll sum = 0;
  for (ll i = 1; i * i <= a; i++) {
    if (a % i == 0) {
      if (a != 1 && i * i != a) {
        sum += 2;
      } else {
        sum++;
      }
    }
  }
  return sum;
}

int main() {
  ll n;
  cin >> n;
  map<ll, ll> mp;
  ll a;
  rep(i, n) {
    cin >> a;
    mp[a]++;
  }

  ll q;
  cin >> q;
  ll b;
  ll sum = 0;
  for (auto m : mp) {
    sum += m.first * m.second;
  }
  rep(i, q) {
    cin >> a >> b;
    sum += b * mp[a];
    sum -= a * mp[a];
    mp[b] += mp[a];
    mp[a] = 0;
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < n; i++)
#define Rep(i, n) for (long long i = 1; i < n; i++)
#define ll long long
#include <cmath>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;
#define PI acos(-1)
using P = pair<int, int>;

ll ketasu(ll a) {
  ll num = 1;
  while (a / 10) {
    num++;
    a /= 10;
  }
  return num;
}

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll kosuu(ll a) {
  ll sum = 0;
  for (ll i = 1; i * i <= a; i++) {
    if (a % i == 0) {
      if (a != 1 && i * i != a) {
        sum += 2;
      } else {
        sum++;
      }
    }
  }
  return sum;
}

int main() {
  ll n;
  cin >> n;
  map<ll, ll> mp;
  ll a;
  rep(i, n) {
    cin >> a;
    mp[a]++;
  }

  ll q;
  cin >> q;
  ll b;
  rep(i, q) {
    cin >> a >> b;
    mp[b] += mp[a];
    mp[a] = 0;
    ll sum = 0;
    for (auto m : mp) {
      sum += m.first * m.second;
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int cnt[100000 + 7] = {};
int N, Q;

void solve() {
  cin >> N;

  ll sum = 0;

  for (int i = 0; i < N; i++) {
    int inp;
    cin >> inp;
    sum += inp;
    cnt[inp]++;
  }

  cin >> Q;

  while (Q--) {
    int B, C;
    cin >> B >> C;

    sum = sum + (C - B) * cnt[B];
    cnt[C] += cnt[B];
    cnt[B] = 0;

    cout << sum << endl;
  }
}

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

  solve();

  return 0;
}

Error: TLE

Fixed submission:

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

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

  ll N;
  cin >> N;

  vector<ll> V;

  for (ll i = 0; i < N; i++) {
    ll inp;
    cin >> inp;
    V.pb(inp);
  }

  ll Q;
  cin >> Q;

  while (Q--) {
    ll A, B;
    cin >> A >> B;

    replace(V.begin(), V.end(), A, B);

    ll sum = accumulate(V.begin(), V.end(), 0);

    cout << sum << endl;
  }

  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)

using namespace std;

int main() {
  int n;
  int i;
  long long sum = 0;
  cin >> n;
  vector<long long> cnt(100002);
  for (i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    cnt[tmp]++;
    sum += tmp;
  }
  int q;
  cin >> q;
  for (i = 0; i < q; i++) {
    long long t, f;
    cin >> t >> f;
    sum -= t * cnt[t];
    sum += f * cnt[t];
    cnt[f] += cnt[t];
    cnt[t] = 0;
    cout << sum << "\\n";
  }
}

Error: 0

Fixed submission:

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

#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)

using namespace std;

int main() {
  int n;
  int i;
  long long sum = 0;
  cin >> n;
  vector<long long> cnt(n + 1);
  for (i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    cnt[tmp]++;
    sum += tmp;
  }
  int q;
  cin >> q;
  for (i = 0; i < q; i++) {
    long long t, f;
    cin >> t >> f;
    sum -= t * cnt[t];
    sum += f * cnt[t];
    cnt[f] += cnt[t];
    cnt[t] = 0;
    cout << sum << "\\n";
  }
}
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);
  vector<long long> f(1e5 + 1, 0);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    sum += A[i];
    f[A[i]]++;
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int b, c;
    cin >> b >> c;
    int diff = c - b;
    sum += diff * f[b];
    f[c] += f[b];
    f[b] = 0;
    cout << sum << "\\n";
  }
}


Error: 0

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> A(n);
  vector<long long> f(n, 0);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    sum += A[i];
    f[A[i]]++;
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int b, c;
    cin >> b >> c;
    int diff = c - b;
    sum += diff * f[b];
    f[c] += f[b];
    f[b] = 0;
    cout << sum << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;
long long A[N];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;
  map<int, long long> qtd;
  long long sum = 0LL;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    sum += A[i];
    qtd[A[i]]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    long long qtd_ = qtd[b];
    qtd[b] = 0, qtd[c] += qtd_;
    long long b_ = b * qtd_;
    long long c_ = c * qtd_;
    long long ans = sum - b_;
    ans += c_;
    sum = ans;
    cout << ans << "\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;
long long A[N], tree[4 * N];

void build(int node, int l, int r) {
  if (l == r) {
    tree[node] = A[l];
  } else {
    int mid = (l + r) / 2;
    build(2 * node, l, mid);
    build(2 * node + 1, mid + 1, r);
    tree[node] = tree[2 * node] + tree[2 * node + 1];
  }
}

void update(int node, int l, int r, int pos, int value) {
  if (l == r) {
    tree[node] = value;
  } else {
    int mid = (l + r) / 2;
    if (pos <= mid) {
      update(2 * node, l, mid, pos, value);
    } else {
      update(2 * node + 1, mid + 1, r, pos, value);
    }
    tree[node] = tree[2 * node] + tree[2 * node + 1];
  }
}

long long query(int node, int tl, int tr, int l, int r) {
  if (r < tl || tr < l)
    return 0; // se ta fora do range
  if (tl >= l && tr <= r) {
    return tree[node]; // completamente dentro do range
  }
  int mid = (tl + tr) / 2;
  long long p1 = query(2 * node, tl, mid, l, r);
  long long p2 = query(2 * node + 1, mid + 1, tr, l, r);
  return (p1 + p2);
}

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

  int n;
  cin >> n;
  set<int> pos[N];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    pos[A[i]].insert(i);
  }
  build(1, 0, n - 1);
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    while (pos[b].size()) {
      int pos_ = *pos[b].begin();
      pos[b].erase(pos_);
      // cout << "Pos = "<< pos_ << "\\n";
      update(1, 0, n - 1, pos_, c);
      pos[c].insert(pos_);
    }
    long long ans = query(1, 0, n - 1, 0, n - 1);
    cout << ans << "\\n";
  }

  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N;
  cin >> N;
  vector<int> backet(100000, 0);
  long long sum = 0LL;
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    sum += A;
    backet.at(A - 1)++;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    sum += backet.at(B - 1) * (C - B);
    backet.at(C - 1) += backet.at(B - 1);
    backet.at(B - 1) = 0;
    cout << sum << endl;
  }
}


Error: 0

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> backet(N, 0);
  long long sum = 0LL;
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    sum += A;
    backet.at(A - 1)++;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    sum += backet.at(B - 1) * (C - B);
    backet.at(C - 1) += backet.at(B - 1);
    backet.at(B - 1) = 0;
    cout << sum << endl;
  }
}