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

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
#define IO                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long

int main() {
  IO;
  // int n;
  // cin>>n;
  // while(n>0){
  //     int tmp = n/26;
  //     if(tmp>0){
  //         cout<<'a';
  //         n = tmp;
  //     }
  //     else{
  //         cout<<char(n+'a'-1);
  //         break;
  //     }

  // }
  int a, tmp;
  ll ans = 0;
  cin >> a;
  vector<int> v(100005, 0);
  for (int i = 0; i < a; i++) {
    cin >> tmp;
    v[tmp]++;
    ans += tmp;
  }

  int q;
  cin >> q;
  int b, c;
  vector<ll> qqq(q, 0);
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    ans = ans + v[b] * (c - b);

    v[c] += v[b];
    v[b] = 0;
    qqq[i] = ans;
  }

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

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
#define IO                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

int main() {
  IO;
  int a, ans = 0;
  ;
  cin >> a;
  vector<int> v(a);
  for (int i = 0; i < a; i++) {
    cin >> v[i];
    ans += v[i];
  }

  int q;
  cin >> q;
  int b, c;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    for (int j = 0; j < a; j++) {
      if (v[j] == b) {
        ans = ans - b + c;
        v[j] = c;
      }
    }
    cout << ans << endl;
  }

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

#include <bits/stdc++.h>
#define ll long long
#define endl "\\n"
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
const ll M = 1e9 + 7;
using namespace std;

void solve() {
  ll n, sum = 0;
  cin >> n;
  ll a[n];
  map<ll, ll> mp;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
    sum += a[i];
  }
  ll m;
  cin >> m;
  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    sum -= x * mp[x];
    sum += y * mp[x];
    ll temp = mp[x];
    mp[y] += temp;
    mp.erase(x);
    cout << sum << endl;
  }
}

int main() {
  FAST ll t = 1;
  // cin>>t;
  for (ll i = 0; i < t; i++)
    solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define endl "\\n"
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
const ll M = 1e9 + 7;
using namespace std;

ll arr[2000005] = {0}, tree[4 * 2000005] = {0};

void buildTree(ll start, ll end, ll index) {
  if (start == end)
    tree[index] = arr[start];
  else {
    ll mid = (start + end) / 2;
    buildTree(start, mid, 2 * index);
    buildTree(mid + 1, end, 2 * index + 1);
    tree[index] = tree[2 * index] + tree[2 * index + 1];
  }
}

void update(ll start, ll end, ll index, ll pos, ll val) {
  if (start == end) {
    arr[pos] = val;
    tree[index] = val;
  } else {
    ll mid = (start + end) / 2;
    if (pos >= start && pos <= mid)
      update(start, mid, 2 * index, pos, val);
    else
      update(mid + 1, end, 2 * index + 1, pos, val);
    tree[index] = tree[2 * index] + tree[2 * index + 1];
  }
}

ll query(ll start, ll end, ll index, ll l, ll r) {
  if (l <= start && r >= end)
    return tree[index];
  if (start > r || end < l)
    return 0;

  ll mid = (start + end) / 2;
  ll q1 = query(start, mid, 2 * index, l, r);
  ll q2 = query(mid + 1, end, 2 * index + 1, l, r);
  return (q1 + q2);
}

void solve() {
  ll n, m;
  cin >> n;
  for (ll i = 1; i <= n; i++)
    cin >> arr[i];
  buildTree(1, n, 1);
  cin >> m;
  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    for (ll j = 1; j <= n; j++)
      if (arr[j] == x)
        update(1, n, 1, j, y);
    cout << query(1, n, 1, 1, n) << endl;
  }
}

int main() {
  FAST ll t = 1;
  // cin>>t;
  for (ll i = 0; i < t; i++)
    solve();
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define newl "\\n"
using namespace std;
using lli = long long int;
using vvint = vector<vector<int>>;
const int mod = 1e9 + 7;

const int N = 2e5 + 1;

void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  map<int, int> mp;
  lli summ = 0;
  for (int i = 0; i < n; ++i)
    cin >> a[i], summ = summ + a[i], ++mp[a[i]];
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    auto itr = mp.find(b);
    if (itr != mp.end()) {
      lli count = itr->second;
      summ = summ - count * b + count * c;
      mp.erase(itr);
      mp[c] += count;
    }
    cout << summ << newl;
  }

  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(10);
#ifdef EXECUTE_LOCALLY
  freopen("in.txt", "r", stdin);
#endif
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
#ifdef EXECUTE_LOCALLY
  cout << setprecision(3)
       << "program time: " << (double)clock() / CLOCKS_PER_SEC << newl;
#endif
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define newl "\\n"
using namespace std;
using lli = long long int;
using vvint = vector<vector<int>>;
const int mod = 1e9 + 7;

const int N = 2e5 + 1;

void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  multiset<int> s;
  lli summ = 0;
  for (int i = 0; i < n; ++i)
    cin >> a[i], summ = summ + a[i], s.insert(a[i]);
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    auto itr = s.find(b);
    while (itr != s.end()) {
      summ = summ - b + c;
      s.erase(itr);
      s.insert(c);
      itr = s.find(b);
    }
    cout << summ << newl;
  }

  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(10);
#ifdef EXECUTE_LOCALLY
  freopen("in.txt", "r", stdin);
#endif
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
#ifdef EXECUTE_LOCALLY
  cout << setprecision(3)
       << "program time: " << (double)clock() / CLOCKS_PER_SEC << newl;
#endif
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long n, q, a, b, c, i, f[100007] = {0}, s = 0;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a;
    s = s + a;
    f[a]++;
  }
  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> b >> c;
    s = s + f[b] * c;
    s = s - f[b] * b;
    f[c] = f[c] + f[b];
    f[b] = 0;
    cout << s << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  long long n, q, b, c, i, a;
  cin >> n;
  vector<long long> v;
  for (i = 0; i < n; i++) {
    cin >> a;
    v.push_back(a);
  }
  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> b >> c;
    replace(v.begin(), v.end(), b, c);
    cout << accumulate(v.begin(), v.end(), 0) << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define RREP(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
typedef long long ll;

int main(void) {
  int n;
  cin >> n;
  vector<int> v(100000 + 1);
  ll sum = 0;
  REP(i, n) {
    int a;
    cin >> a;
    v[a]++;
    sum += a;
  }

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

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define RREP(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
typedef long long ll;

int main(void) {
  int n;
  cin >> n;
  vector<int> v(10000 + 1);
  ll sum = 0;
  REP(i, n) {
    int a;
    cin >> a;
    v[a]++;
    sum += a;
  }

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

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

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;

#define P105 100000

signed main() {
  vector<int> count(P105 * 11);
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    count[a[i]]++;
  }
  int q;
  cin >> q;
  vector<int> b(q), c(q);
  rep(i, q) { cin >> b[i] >> c[i]; }

  ll sum = 0;
  rep(i, n) sum += a[i];
  rep(j, q) {
    int &bj = b[j];
    int &cj = c[j];
    int c = count[bj];
    sum -= c * (bj - cj);
    count[bj] = 0;
    count[cj] += c;
    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;

// #define INF 0x3fffFFFF
// #define INF_NEG (INF * -1)
// #define INF_LONG 0x3fffFFFFffffFFFFL
// #define INF_LONG_NEG (INF_LONG * -1L)

// int isFirst = 1;
// template <class T> void print(T t) { if (isFirst) cout << t, isFirst = 0;
// else cout << " " << t; }

signed main() {
  ll n;
  cin >> n;
  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 sum = 0;
  rep(i, n) sum += a[i];

  rep(j, q) {
    rep(i, n) {
      if (a[i] == b[j]) {
        a[i] = c[j];
        sum -= b[j];
        sum += c[j];
      }
    }
    cout << sum << endl;
  }

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

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

int main() {
  ll N;
  vector<ll> A(100010, 0);

  cin >> N;
  ll sum = 0;
  rep(i, N) {
    ll x;
    cin >> x;
    A[x]++;
    sum += x;
  }

  ll Q;
  cin >> Q;

  rep(z, Q) {
    ll s, t;
    cin >> s >> t;
    ll v;

    // 個数の移動
    v = A[s];
    A[s] = 0;
    A[t] += v;

    // 数の変化
    ll ret = (t - s) * v;
    sum = sum + ret;

    cout << sum << endl;
  }
}


Error: 0

Fixed submission:

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

int main() {
  ll N;
  vector<ll> A(10011, 0);

  cin >> N;
  ll sum = 0;
  rep(i, N) {
    ll x;
    cin >> x;
    A[x]++;
    sum += x;
  }

  ll Q;
  cin >> Q;

  rep(z, Q) {
    ll s, t;
    cin >> s >> t;
    ll v;

    // 個数の移動
    v = A[s];
    A[t] += v;

    // 数の変化
    ll ret = (t - s) * v;
    sum = sum + ret;

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

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

typedef long long ll;

int main() {
  ll N;
  std::cin >> N;
  std::map<ll, ll> A;
  for (ll i = 0; i < N; i++) {
    ll temp;
    std::cin >> temp;
    A[temp]++;
  }
  ll Q;
  std::cin >> Q;
  std::vector<ll> B(Q);
  std::vector<ll> C(Q);
  for (ll i = 0; i < Q; i++) {
    std::cin >> B[i] >> C[i];
  }
  ll sum = 0;
  for (auto it = A.begin(); it != A.end(); it++) {
    sum += (it->second) * (it->first);
  }
  for (int i = 0; i < Q; i++) {
    if (A[B[i]] != 0) {
      A[C[i]] += A[B[i]];
      sum += (C[i] - B[i]) * A[B[i]];
      A[B[i]] = 0;
      // for(auto it = A.begin(); it != A.end() ; it++){
      // 	std::cout << "f : " << it -> first << " s : " << it -> second <<
      // std::endl;
      // }
    }
    std::cout << sum << std::endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <vector>

typedef long long ll;

int main() {
  ll N;
  std::cin >> N;
  std::vector<ll> A(N);
  for (ll i = 0; i < N; i++) {
    std::cin >> A[i];
  }
  ll Q;
  std::cin >> Q;
  std::vector<ll> B(Q);
  std::vector<ll> C(Q);
  for (ll i = 0; i < Q; i++) {
    std::cin >> B[i] >> C[i];
  }
  std::sort(A.begin(), A.end());
  ll sum = std::accumulate(A.begin(), A.end(), 0.0,
                           [](ll acc, ll i) { return acc + i; });
  for (int i = 0; i < Q; i++) {
    if (std::binary_search(A.begin(), A.end(), B[i])) {
      ll low = std::lower_bound(A.begin(), A.end(), B[i]) - A.begin();
      ll up = std::upper_bound(A.begin(), A.end(), B[i]) - A.begin();
      std::transform(A.begin() + low, A.begin() + up, A.begin() + low,
                     [&](ll a) { return C[i]; });
      std::sort(A.begin(), A.end());
      sum += (C[i] - B[i]) * (up - low);
    }
    // for(auto && r : A){
    //   std::cout << r << " ";
    // }
    // std::cout << std::endl;
    std::cout << sum << std::endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mxe max_element
#define mne min_element
#define bg begin()
#define nd end()
#define u unsigned

void swap(int &a, int &b) {
  int t = a;
  a = b;
  b = t;
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll t, n, m;
  cin >> n;

  u ll sum = 0;

  vector<u ll> v(n), mp(100069, 0);
  // unordered_map<u ll,u ll> mp;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    mp[v[i]]++;
    sum += v[i];
  }

  cin >> m;

  for (int i = 0; i < m; i++) {
    u ll a, b;
    cin >> a >> b;
    // v[mp[a]]=b;
    // for(auto x:mp[a])
    sum += b * (mp[a]);
    sum -= a * mp[a];
    mp[b] += mp[a];
    mp[a] = 0;

    cout << sum << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mxe max_element
#define mne min_element
#define bg begin()
#define nd end()
#define u unsigned

void swap(int &a, int &b) {
  int t = a;
  a = b;
  b = t;
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll t, n, m;
  cin >> n;

  vector<u ll> v(n), mp(10069, 0);
  // unordered_map<u ll,u ll> mp;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    mp[v[i]]++;
  }

  cin >> m;

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

  for (int i = 0; i < m; i++) {
    u ll a, b;
    cin >> a >> b;
    // v[mp[a]]=b;
    // for(auto x:mp[a])
    sum += b * (mp[a]);
    sum -= a * mp[a];
    mp[b] += mp[a];
    mp[a] = 0;

    cout << sum << endl;
  }

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

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;
int cnt[N], a[N];

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

  int n, q;
  cin >> n;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
    sum += 1ll * a[i];
  }
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    sum = sum - 1ll * cnt[b] * b + 1ll * cnt[b] * c;
    cnt[c] += cnt[b];
    cnt[b] = 0;
    cout << sum << '\\n';
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

typedef long long ll;
using namespace std;

int main() {
  int N;
  cin >> N;
  int anums[10005] = {};
  int as[10005] = {};
  ll sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> as[i];
    anums[as[i]]++;
    sum += 1ll * as[i];
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int b, c;
    cin >> b >> c;
    sum = sum - 1ll * anums[b] * b + 1ll * anums[b] * c;
    anums[c] += anums[b];
    anums[b] = 0;
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;

  vector<int> v;
  unordered_map<int, int> mp;
  int x, sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
    mp[x]++;
    sum += x;
  }
  int q, b, c;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    sum -= b * mp[b];
    sum += c * mp[b];
    mp[c] += mp[b];
    mp[b] = 0;
    cout << sum << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

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

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;

  vector<int> v;
  int x;
  for (int i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
  }
  int q, b, c, index;
  cin >> q;

  std::vector<int>::iterator it;

  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    int ct = count(begin(v), end(v), b);

    while (ct > 0) {
      it = find(begin(v), end(v), b);
      index = it - v.begin();
      v[index] = c;
      ct--;
    }
    int sum = accumulate(begin(v), end(v), 0);
    cout << sum << "\\n";
  }

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

/*Bismillahir Rahmanir Rahim
->>Code by Aimon
->>CUET,CSE
*/
#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
typedef unsigned long long ull;
typedef vector<int> vi;
const int MX = 2e5 + 5;
#define f(i, x, n) for (int i = x; i < n; i++)
#define pb push_back
#define endl "\\n"
#define yes cout << "YES" << endl
#define no cout << "NO" << endl

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  int a[n], d[MX];
  ll s{0};
  f(i, 0, n) {
    cin >> a[i];
    s += a[i];
    d[a[i]]++;
  }
  int q, b, c;
  cin >> q;
  while (q--) {
    int h{0};
    cin >> b >> c;

    h = d[b];
    d[b] = 0;
    d[c] += h;
    s += h * (c - b);

    cout << s << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m[100001];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> m[i];
  }
  long long int a, p, q;
  cin >> a;
  for (long long int i = 0; i < a; i++) {
    int d, b, s = 0;
    cin >> d >> b;
    for (long long int i = 0; i < n; i++) {
      long long int p = d;
      long long int q = b;
      if (p == m[i]) {
        swap(q, m[i]);
      }
      s = s + m[i];
    }
    cout << s << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <unordered_map>

using namespace std;

int main() {
  unordered_map<int, int> AMap;

  int N;
  cin >> N;

  long long ans = 0;

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

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; ++i) {
    int B, C;
    cin >> B >> C;

    int bval = AMap[B];
    AMap[B] -= bval;
    AMap[C] += bval;

    ans += bval * (C - B);

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

const int maxnum = 100001;
int sum(const vector<int> &vec) {
  int sum = 0;
  for (int i = 0; i < maxnum; ++i) {
    sum += vec.at(i) * i;
  }
  return sum;
}

int main() {
  vector<int> vec(maxnum);

  int N, A;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> A;
    vec.at(A)++;
  }

  int Q, B, C;
  cin >> Q;
  for (int i = 0; i < Q; ++i) {
    cin >> B >> C;
    int &b = vec.at(B);
    int &c = vec.at(C);
    if (b > 0) {
      c += b;
      b = 0;
    }
    cout << sum(vec) << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int n, q, a, b, c;
  long long int sum = 0;
  cin >> n;

  vector<int> d(100000, 0);
  rep(i, n) {
    cin >> a;
    sum += a;
    d[a - 1] += 1;
  }
  cin >> q;
  vector<pair<int, int>> bc(q);
  rep(i, q) {
    cin >> b >> c;
    bc[i] = make_pair(b, c);
  }

  rep(i, q) {
    d[bc[i].second - 1] += d[bc[i].first - 1];
    sum += (bc[i].second - bc[i].first) * d[bc[i].first - 1];
    d[bc[i].first - 1] = 0;
    cout << sum << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int n, q, b, c;
  long long int sum = 0;
  cin >> n;
  vector<int> a(n);
  vector<int> d(10000, 0);
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
    d[a[i] - 1] += 1;
  }
  cin >> q;
  vector<pair<int, int>> bc(q);
  rep(i, q) {
    cin >> b >> c;
    bc[i] = make_pair(b, c);
  }

  rep(i, q) {
    d[bc[i].second - 1] += d[bc[i].first - 1];
    sum += (bc[i].second - bc[i].first) * d[bc[i].first - 1];
    d[bc[i].first - 1] = 0;
    cout << sum << endl;
  }

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

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

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

  ll n;
  cin >> n;
  vector<ll> a(n, 0);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll sum = 0;
  sum = accumulate(a.begin(), a.end(), 0LL);
  ll q;
  cin >> q;
  vector<ll> b(q, 0), c(q, 0);
  for (ll i = 0; i < q; i++)
    cin >> b[i] >> c[i];

  vector<ll> dp(1000000, 0);
  for (ll i = 0; i < n; i++) {
    dp[a[i]] += 1;
  }

  ll ans = sum;
  for (ll i = 0; i < q; i++) {
    ans = ans - (b[i] * dp[b[i]]) + (c[i] * dp[b[i]]);
    dp[c[i]] = dp[c[i]] + dp[b[i]];
    dp[b[i]] = 0;
    cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

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

  ll n;
  cin >> n;
  vector<ll> a(n, 0);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll sum = 0;
  sum = accumulate(a.begin(), a.end(), 0LL);
  ll q;
  cin >> q;
  vector<ll> b(n, 0), c(n, 0);
  for (ll i = 0; i < q; i++)
    cin >> b[i] >> c[i];

  vector<ll> dp(1000000, 0);
  for (ll i = 0; i < n; i++) {
    dp[a[i]] += 1;
  }

  ll ans = sum;
  for (ll i = 0; i < q; i++) {
    ans = ans - (b[i] * dp[b[i]]) + (c[i] * dp[b[i]]);
    dp[c[i]] = dp[c[i]] + dp[b[i]];
    dp[b[i]] = 0;
    cout << ans << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int a, b, n, t, sum = 0, x;
  vector<int> count(100005, 0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    count[x]++;
    sum += x;
  }
  cin >> t;
  while (t--) {
    cin >> a >> b;
    sum -= a * count[a];
    sum += b * count[a];
    count[b] += count[a];
    count[a] = 0;
    cout << sum << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, n, t, sum = 0, x;
  cin >> n;
  vector<int> count = {0};
  for (int i = 0; i < n; i++) {
    cin >> x;
    count[x]++;
    sum += x;
  }
  cin >> t;
  while (t--) {
    cin >> a >> b;
    sum -= a * count[a];
    sum += b * count[a];
    count[b] += count[a];
    count[a] = 0;
    cout << sum << endl;
  }

  return 0;
}