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

#include <algorithm>
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
const int INF = 0x7fffffff;
using lll = long long;
using ull = unsigned long long;
using namespace std;

int box[100001];

int main() {

  lll ii, jj, kk;
  vector<int> ret;
  lll n;

  cin >> n;

  vector<lll> a(n);
  lll sum;

  memset(box, 0, sizeof(box));
  sum = 0;

  for (ii = 0; ii < n; ii++) {

    cin >> a[ii];
    box[a[ii]]++;
    sum += a[ii];
  }

  lll q;

  cin >> q;

  lll b, c;

  // for(jj=1;jj<=4;jj++){
  //
  //   cout << box[jj] << endl;
  //
  // }

  for (ii = 0; ii < q; ii++) {

    cin >> b >> c;

    sum -= b * box[b];
    sum += c * box[b];

    box[c] += box[b];
    box[b] = 0;

    // cout << "=========" << endl;
    //
    // for(jj=1;jj<=4;jj++){
    //   cout << box[jj] << endl;
    // }
    //
    // cout << "=========" << endl;

    cout << sum << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
const int INF = 0x7fffffff;
using lll = long long;
using ull = unsigned long long;
using namespace std;

int main() {
  lll ii, jj, kk;
  vector<int> ret;
  lll n;

  cin >> n;

  vector<lll> a(n);

  for (ii = 0; ii < n; ii++) {

    cin >> a[ii];
  }

  lll q;

  cin >> q;

  lll b, c;
  lll sum;

  for (ii = 0; ii < q; ii++) {

    cin >> b >> c;

    sum = 0;

    for (jj = 0; jj < n; jj++) {

      if (a[jj] == b) {
        a[jj] = c;
      }

      sum += a[jj];
    }

    cout << sum << endl;
  }

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

#include <bits/stdc++.h>
#include <unordered_set>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
using P = pair<ll, ll>;
using V = vector<ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define MOD 1000000007
#define INF 1000000000

void solve_abc_d() {
  ll n, m;
  // ll a, b;
  // ll x, y;
  ll k;
  ll l_ans = 0;

  cin >> n;

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

  ll q;
  cin >> q;
  ll b, c;
  vector<ull> sum(q, 0);
  ull base = 0;
  for (auto itr = a.begin(); itr != a.end(); itr++) {
    base += itr->first * itr->second;
  }

  rep(i, q) {
    cin >> b >> c;
    base += (c - b) * a[b];
    a[c] += a[b];
    a[b] = 0;
    sum[i] = base;
  }

  rep(i, q) { cout << sum[i] << "\\n"; }

  return;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed;
  cout << setprecision(15);

  solve_abc_d();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_set>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
using P = pair<ll, ll>;
using V = vector<ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define MOD 1000000007
#define INF 1000000000

void solve_abc_d() {
  ll n, m;
  // ll a, b;
  // ll x, y;
  ll k;
  ll l_ans = 0;

  cin >> n;

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

  ll q;
  cin >> q;
  ll b, c;
  vector<ull> sum(q, 0);
  rep(i, q) {
    cin >> b >> c;
    a[c] += a[b];
    a[b] = 0;
    sum[i] = 0;
    for (auto itr = a.begin(); itr != a.end(); itr++) {
      sum[i] += itr->first * itr->second;
    }
  }

  rep(i, q) { cout << sum[i] << "\\n"; }

  return;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed;
  cout << setprecision(15);

  solve_abc_d();

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>
#define ran(x) (x).begin(), (x).end()
using namespace std;
int main() {
  int64_t n, sum = 0, a = 0;
  cin >> n;
  map<int64_t, int64_t> g;
  vector<int64_t> b;
  for (int i = 0; i < n; ++i) {
    cin >> a;
    sum += a;
    ++g[a];
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int x, y;
    cin >> x >> y;
    sum -= x * g[x];
    sum += y * g[x];
    g[y] += g[x];
    g[x] = 0;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES
#define inf 999999999999999999
#define rep(i, n, m) for (lli i = (lli)(n); i < (lli)(m); ++i)
#define reep(i, n, m) for (int i = (int)(n); i <= (int)(m); ++i)
#define per(i, m, n) for (lli i = (int)(m)-1; i >= (int)(n); --i)
#define st(n) sort(n.begin(), n.end())
#define rev(n) reverse(n.begin(), n.end())
#define ou(S) cout << S << endl
#define pb(n) push_back(n)
#define ue(N) N.erase(unique(N.begin(), N.end()), N.end());
#define nou(S) cout << S
#define hou(S) cout << setprecision(30) << S << endl
#define vec(K, L, N, S) vector<L> K(N, S)
#define dv(K, L, N, M, S) vector<vector<L>> K(N, vector<L>(M, S))
#define tv(K, L, N, M, R, S)                                                   \\
  vector<vector<vector<L>>> K(N, vector<vector<L>>(M, vector<L>(R, S)))
#define pint pair<lli, lli>
#define paf(L, R) pair<L, R>
#define maxx 5100000
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
#define endl "\\n"
typedef long long int lli;
typedef unsigned long long int uli;
const uli mod = 1e9 + 7;
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  uli n;
  cin >> n;
  vector<uli> a(n);
  for (uli i = 0; i < n; ++i) {
    cin >> a[i];
  }
  uli sum = 0;
  uli q;
  cin >> q;
  for (uli i = 0; i < q; ++i) {
    uli b, c;
    cin >> b >> c;
    for (uli j = 0; j < n; ++j) {
      if (a[j] == b)
        a[j] = c;
    }
    for (auto i : a) {
      sum += i;
    }
    cout << sum << endl;
    sum = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <complex>
#include <iostream>
#define FIXED_FLOAT(x) std::fixed << std::setprecision(7) << (x)
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
typedef pair<ll, ll> pairs;
typedef complex<ll> G;
const int N = 1e5 + 5;

const int INF = 1e9 + 5;

void done() {

  ll n;
  cin >> n;
  ll a[n];
  ll total = 0;
  vector<ll> frq(N, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    frq[a[i]]++;
    total += a[i];
  }
  ll q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    ll b, c;
    cin >> b >> c;

    ll p = total - (frq[b] * b) + (c * (frq[b]));

    frq[c] += frq[b];
    frq[b] = 0;

    total = p;

    /*for(int j=0; j< n;j++) {
        if(a[j]==b) {
            a[j] = c;
            frq[a[j]]--;
            frq[c]++;
        }
        total += a[j];
    }
    */
    cout << p << '\\n';
  }
}
void solve() {
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  int sum = 0;
  for (int i = 0; i < k; i++) {
    sum += a[i];
  }
  cout << sum << '\\n';
}

void another() {}
int main() {

  fast;
  done();
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <complex>
#include <iostream>
#define FIXED_FLOAT(x) std::fixed << std::setprecision(7) << (x)
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
typedef pair<ll, ll> pairs;
typedef complex<ll> G;
const int N = 1e5 + 5;

const int INF = 1e9 + 5;

vector<ll> frq(N, 0);
void done() {

  ll n;
  cin >> n;
  ll a[n];
  ll total = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    frq[a[i]]++;
    total += a[i];
  }
  ll q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    ll b, c;
    cin >> b >> c;
    total = 0;
    // ll p = total-(frq[b]*b) + (c*(frq[a]));
    for (int j = 0; j < n; j++) {
      if (a[j] == b) {
        a[j] = c;
        frq[a[j]]--;
        frq[c]++;
      }
      total += a[j];
    }

    cout << total << '\\n';
  }
}
void solve() {
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  int sum = 0;
  for (int i = 0; i < k; i++) {
    sum += a[i];
  }
  cout << sum << '\\n';
}

void another() {}
int main() {

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

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

int main(int argc, const char *argv[]) {
  int N;
  cin >> N;

  int *A = new int[N];
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  int Q;
  cin >> Q;

  // auto *S = new unsigned long int[Q];
  auto *B = new int[Q];
  auto *C = new int[Q];

  for (int qi = 0; qi < Q; ++qi) {
    cin >> B[qi] >> C[qi];
  }

  const int ND = 100001;
  int D[ND] = {0};

  long int S = 0;
  for (int ni = 0; ni < N; ++ni) {
    D[A[ni]]++;
    S += A[ni];
  }

  for (int qi = 0; qi < Q; ++qi) {
    S += D[B[qi]] * (C[qi] - B[qi]);
    D[C[qi]] += D[B[qi]];
    D[B[qi]] = 0;
    cout << S << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

int main(int argc, const char *argv[]) {
  int N;
  cin >> N;

  int *A = new int[N];
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  int Q;
  cin >> Q;

  auto *S = new unsigned long int[Q];
  auto *B = new int[Q];
  auto *C = new int[Q];

  for (int qi = 0; qi < Q; ++qi) {
    cin >> B[qi] >> C[qi];
  }

  for (int qi = 0; qi < Q; ++qi) {
    if (qi == 0) {
      S[0] = 0;
      for (int ni = 0; ni < N; ++ni) {
        S[0] += A[ni];
      }
    } else {
      S[qi] = S[qi - 1];
    }
    for (int ni = 0; ni < N; ++ni) {
      int diff = C[qi] - B[qi];
      if (A[ni] == B[qi]) {
        S[qi] += diff;
        A[ni] += diff;
      }
    }
  }

  for (int i = 0; i < Q; ++i) {
    cout << S[i] << endl;
  }

  delete[] A, B, C;
  delete[] S;

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

#include "bits/stdc++.h"

using namespace std;

void Main() {
  long long N;
  cin >> N;
  map<long long, long long> A;
  for (long long i = 0; i < N; ++i) {
    long long a;
    cin >> a;
    if (A.count(a) == 0) {
      A.insert(make_pair(a, 0));
    }
    A[a] += 1;
  }

  long long ans = 0;
  for (auto e : A) {
    ans += e.first * e.second;
  }

  long long Q;
  cin >> Q;
  for (long long i = 0; i < Q; ++i) {
    long long b, c;
    cin >> b >> c;

    if (A.count(b) > 0) {
      ans -= b * A[b];
      ans += c * A[b];
      if (A.count(c) == 0) {
        A.insert(make_pair(c, 0));
      }
      A[c] += A[b];
      A.erase(b);
    }

    cout << ans << endl;
  }
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

void Main() {
  long long N;
  cin >> N;
  map<long long, long long> A;
  for (long long i = 0; i < N; ++i) {
    long long a;
    cin >> a;
    if (A.count(a) == 0) {
      A.insert(make_pair(a, 0));
    }
    A[a] += 1;
  }

  long long Q;
  cin >> Q;
  for (long long i = 0; i < Q; ++i) {
    long long b, c;
    cin >> b >> c;

    if (A.count(b) > 0) {
      if (A.count(c) == 0) {
        A.insert(make_pair(c, 0));
      }
      A[c] += A[b];
      A.erase(b);
    }

    long long ans = 0;
    for (auto e : A) {
      ans += e.first * e.second;
    }
    cout << ans << endl;
  }
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
#define fi first
#define se second
const ll INFLL = LLONG_MAX;
const int INF = INT_MAX;
const ll MAXLL = 0x3f3f3f3f3f3f3f3f;
const int MAX = 0x3f3f3f3f;
#define eb emplace_back
#define emp emplace
#define mp(a, b) make_pair(a, b)
template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> void sort(vector<T> &v) { sort(v.begin(), v.end()); }
template <class T, class U> void sort(vector<T> &v, U func) {
  sort(v.begin(), v.end(), func);
}
template <class T> int lb_index(vector<T> &v, T k) {
  return lower_bound(v.begin(), v.end(), k) - v.begin();
}
template <class T> int ub_index(vector<T> &v, T k) {
  return upper_bound(v.begin(), v.end(), k) - v.begin();
}

void solve() {
  int n, q;
  cin >> n;
  vl v(100001);
  int k;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> k;
    ans += k;
    v[k]++;
  }
  cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    ans -= v[a] * a;
    ans += v[a] * b;
    v[b] += v[a];
    v[a] = 0;
    cout << ans << endl;
  }
}

int main() {
  ios::sync_with_stdio(0);
  int numberofsubtestcases = 1;
  //	cin>>numberofsubtestcases;
  while (numberofsubtestcases--) {
    solve();
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
#define fi first
#define se second
const ll INFLL = LLONG_MAX;
const int INF = INT_MAX;
const ll MAXLL = 0x3f3f3f3f3f3f3f3f;
const int MAX = 0x3f3f3f3f;
#define eb emplace_back
#define emp emplace
#define mp(a, b) make_pair(a, b)
template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> void sort(vector<T> &v) { sort(v.begin(), v.end()); }
template <class T, class U> void sort(vector<T> &v, U func) {
  sort(v.begin(), v.end(), func);
}
template <class T> int lb_index(vector<T> &v, T k) {
  return lower_bound(v.begin(), v.end(), k) - v.begin();
}
template <class T> int ub_index(vector<T> &v, T k) {
  return upper_bound(v.begin(), v.end(), k) - v.begin();
}

void solve() {
  int n, q;
  cin >> n;
  vl v(10001);
  int k;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> k;
    ans += k;
    v[k]++;
  }
  cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    ans -= v[a] * a;
    ans += v[a] * b;
    v[b] += v[a];
    v[a] = 0;
  }
  cout << ans;
}

int main() {
  ios::sync_with_stdio(0);
  int numberofsubtestcases = 1;
  //	cin>>numberofsubtestcases;
  while (numberofsubtestcases--) {
    solve();
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

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

  std::map<long long int, long long int> A;
  for (int i = 0; i < N; i++) {
    int in;
    std::cin >> in;
    if (A.find(in) == A.end()) {
      A[in] = 0;
    }
    A[in]++;
  }
  int Q;
  std::cin >> Q;
  long long int sum = 0;
  for (auto &&pair : A) {
    sum += pair.first * pair.second;
  }
  for (int i = 0; i < Q; i++) {
    int B, C;
    std::cin >> B >> C;
    if (A.find(C) == A.end()) {
      A[C] = 0;
    }
    if (A.find(B) != A.end()) {
      sum += (C - B) * A[B];
      A[C] += A[B];
      A.erase(B);
    }
    std::cout << sum << std::endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

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

  std::map<long long int, long long int> A;
  for (int i = 0; i < N; i++) {
    int in;
    std::cin >> in;
    if (A.find(in) == A.end()) {
      A[in] = 0;
    }
    A[in]++;
  }
  int Q;
  std::cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    std::cin >> B >> C;
    if (A.find(C) == A.end()) {
      A[C] = 0;
    }
    if (A.find(B) != A.end()) {
      A[C] += A[B];
      A.erase(B);
    }
    long long int sum = 0;
    for (auto &&pair : A) {
      sum += pair.first * pair.second;
    }
    std::cout << sum << std::endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  map<long long, long long> h;
  long long N;
  cin >> N;
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    long long a;
    cin >> a;
    sum += a;
    h[a]++;
  }
  int Q;
  cin >> Q;
  long long BC[Q][2];
  for (int i = 0; i < Q; i++) {
    cin >> BC[i][0] >> BC[i][1];
  }
  for (int i = 0; i < Q; i++) {
    long long B, C;
    B = BC[i][0];
    C = BC[i][1];
    // cout << h[B] << endl;
    // cout << h[C] << endl;
    sum += C * h[B] - B * h[B];
    h[C] += h[B];
    h[B] = 0;
    cout << sum << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  map<long long, long long> h;
  long long N;
  cin >> N;
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    long long a;
    cin >> a;
    sum += a;
    h[a]++;
  }
  int Q;
  cin >> Q;
  long long BC[N][2];

  for (int i = 0; i < Q; i++) {
    cin >> BC[i][0] >> BC[i][1];
  }
  for (int i = 0; i < Q; i++) {
    long long B, C;
    B = BC[i][0];
    C = BC[i][1];
    // cout << h[B] << endl;
    // cout << h[C] << endl;
    sum += C * h[B] - B * h[B];
    h[C] += h[B];
    h[B] = 0;
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <vector>

using namespace std;

int main() {
  long long int a[100005] = {};
  long long int n;
  cin >> n;
  long long int temp;
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    a[temp]++;
    ans += temp;
  }

  long long int q;
  cin >> q;
  vector<long long int> b;
  vector<long long int> c;
  for (int i = 0; i < q; i++) {
    cin >> temp;
    b.push_back(temp);
    cin >> temp;
    c.push_back(temp);
  }

  for (int i = 0; i < q; i++) {
    ans += a[b[i]] * (c[i] - b[i]);
    cout << ans;
    if (i != q - 1)
      cout << "\\n";
    a[c[i]] += a[b[i]];
    a[b[i]] = 0;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <vector>

using namespace std;

int main() {
  long long int a[10005] = {};
  long long int n;
  cin >> n;
  long long int temp;
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    a[temp]++;
    ans += temp;
  }

  long long int q;
  cin >> q;
  vector<long long int> b;
  vector<long long int> c;
  for (int i = 0; i < q; i++) {
    cin >> temp;
    b.push_back(temp);
    cin >> temp;
    c.push_back(temp);
  }

  for (int i = 0; i < q; i++) {
    ans += a[b[i]] * (c[i] - b[i]);
    cout << ans;
    if (i != q - 1)
      cout << "\\n";
    a[c[i]] += a[b[i]];
    a[b[i]] = 0;
  }

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

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

#define pi 3.1415926535

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

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

  int q;
  cin >> q;

  vector<ll> ans(q, 0);

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

    a[c] += a[b];
    sum += a[b] * (c - b);
    a[b] = 0;

    ans[i] = sum;
  }

  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

#define pi 3.1415926535

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

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

  int q;
  cin >> q;

  vector<ll> ans(q, 0);

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

    a[c] += a[b];
    sum += a[b] * (c - b);
    a[b] = 0;

    ans[i] = sum;
  }

  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<ll> buc(101010);
  ll ans = 0;
  rep(i, n) {
    ll x;
    cin >> x;
    buc[x]++;
    ans += x;
  }
  int q;
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    ans += c * buc[b] - buc[b] * b;
    buc[c] += buc[b];
    buc[b] = 0;
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<ll> buc(10101);
  ll ans = 0;
  rep(i, n) {
    ll x;
    cin >> x;
    buc[x]++;
    ans += x;
  }
  int q;
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    ans += c * buc[b] - buc[b] * b;
    buc[c] += buc[b];
    buc[b] = 0;
    cout << ans << endl;
  }
}