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

#include <stdio.h>
#include <string.h>

using ll = long long;

int main() {
  int N;
  scanf("%d", &N);
  static ll dat[100001];
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ll A;
    scanf("%lld", &A);
    dat[A]++;
    ans += A;
  }

  int Q;
  scanf("%d", &Q);
  for (int q = 0; q < Q; q++) {
    int B, C;
    scanf("%d%d", &B, &C);
    ll diff = dat[B] * (C - B);
    ans += diff;
    if (B != C) {
      dat[C] += dat[B];
      dat[B] = 0;
    }
    printf("%lld\\n", ans);
  }

  return 0;
}


Error: WA

Fixed submission:

#include <stdio.h>
#include <string.h>

using ll = long long;

int main() {
  int N;
  scanf("%d", &N);
  static ll dat[100001];
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    int A;
    scanf("%lld", &A);
    dat[A]++;
    ans += A;
  }

  int Q;
  scanf("%d", &Q);
  for (int q = 0; q < Q; q++) {
    int B, C;
    scanf("%d%d", &B, &C);
    ll diff = dat[B] * (C - B);
    ans += diff;
    if (B != C) {
      dat[C] += dat[B];
      dat[B] = 0;
    }
    printf("%lld\\n", ans);
  }

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

#include <bits/stdc++.h>
// #include<iostream>
// #include<vector>
// #include<cmath>
// #include<iomanip>
using namespace std;

const double PI = acos(-1.0);

int gcd_func(int a, int b) { return b ? gcd_func(b, a % b) : a; }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  int n;
  cin >> n;
  map<long long, long long> mp;
  long long sm = 0;
  for (int i = 0; i < n; i++) {
    long long ipt;
    cin >> ipt;
    if (mp.find(ipt) != mp.end()) {
      mp[ipt]++;
    } else {
      mp[ipt] = 1;
    }
    sm += ipt;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    long long b, c;
    cin >> b >> c;
    long long diff = c - b;
    long long occ = 0;
    if (mp.find(b) != mp.end()) {
      occ = mp[b];
      mp[b] = 0;
    }
    if (mp.find(c) != mp.end()) {
      mp[c] += occ;
    } else {
      mp[c] = occ;
    }
    sm += occ * diff;
    cout << sm << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include<iostream>
// #include<vector>
// #include<cmath>
// #include<iomanip>
using namespace std;

const double PI = acos(-1.0);

int gcd_func(int a, int b) { return b ? gcd_func(b, a % b) : a; }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  int n;
  cin >> n;
  vector<long long> a(n, 0);
  long long sm = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sm += a[i];
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    long long b, c;
    cin >> b >> c;
    long long diff = c - b;
    int occ = count(a.begin(), a.end(), b);
    sm += occ * diff;
    cout << sm << endl;
    a.erase(remove(a.begin(), a.end(), b), a.end());
    while (occ--) {
      a.push_back(c);
    }
  }
}
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 rep2(i, x, n) for (int i = x; i < (n); i++)
#define pb push_back
#define ALL(v) v.begin(), v.end()
const long long INF = 1LL << 60; // 仮想的な無限大の値;
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;
int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  map<ll, ll> cnt;
  ll MAX = 0;
  ll sum = 0;
  rep(i, N) {
    cin >> A[i];
    ++cnt[A[i]];
    chmax(MAX, A[i]);
    sum += A[i];
  }
  int Q;
  cin >> Q;
  vector<P> S;
  rep(i, Q) {
    ll a, b;
    cin >> a >> b;
    S.emplace_back(P(a, b));
  }
  rep(i, Q) {
    ll a = S[i].first;
    ll b = S[i].second;
    // rep(k, cnt[a]) sum -= a;
    // rep(k, cnt[b]) sum += b;
    sum -= (ll)(a * cnt[a]);
    sum += (ll)(b * cnt[a]);
    cnt[b] += cnt[a];
    cnt[a] = 0;
    cout << sum << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define pb push_back
#define ALL(v) v.begin(), v.end()
const long long INF = 1LL << 60; // 仮想的な無限大の値;
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;
int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  vector<ll> cnt(N);
  ll MAX = 0;
  ll sum = 0;
  rep(i, N) {
    cin >> A[i];
    ++cnt[A[i]];
    chmax(MAX, A[i]);
    sum += A[i];
  }
  int Q;
  cin >> Q;
  vector<P> S;
  rep(i, Q) {
    ll a, b;
    cin >> a >> b;
    S.emplace_back(P(a, b));
  }
  rep(i, Q) {
    ll a = S[i].first;
    ll b = S[i].second;
    rep(k, cnt[a]) sum -= a;
    rep(k, cnt[b]) sum += b;
    sum -= (ll)(a * cnt[a]);
    sum += (ll)(b * cnt[a]);
    cnt[b] += cnt[a];
    cnt[a] = 0;
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

// Replacing

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define rep(i, x, y) for (ll i = x; i < y; i++)
#define irep(i, a) for (auto i = a.begin(); i != a.end(); ++i)
#define nvarep(n, a)                                                           \\
  ll n;                                                                        \\
  cin >> n;                                                                    \\
  vector<ll> a(n);                                                             \\
  rep(i, 0, n) cin >> a[i]
#define vecrep(n, a, type)                                                     \\
  vector<type> a(n);                                                           \\
  rep(i, 0, n) cin >> a[i]
#define lcm(a, b) (a / __gcd(a, b) * b)
#define range(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define nnn "\\n"
#define spa " "
using p = pair<ll, ll>;
using garph = vector<vector<ll>>;
const int inf = 2147483647;         // 2*10^9
const ll INF = 9223372036854775807; // 9*10^18

signed main() {
  nvarep(n, a);
  int q;
  cin >> q;
  vector<int> m(100001);
  vector<int> p(100001);
  rep(i, 0, n) {
    m[a[i]] += a[i];
    p[a[i]]++;
  }
  int ans = accumulate(range(m), 0LL);
  rep(i, 0, q) {
    int b, c;
    cin >> b >> c;
    ans -= m[b];
    m[c] += p[b] * c;
    ans += p[b] * c;
    p[c] += p[b];
    m[b] = p[b] = 0;
    cout << ans << nnn;
  }
  return 0;
}

Error: TLE

Fixed submission:

// Replacing

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define rep(i, x, y) for (ll i = x; i < y; i++)
#define irep(i, a) for (auto i = a.begin(); i != a.end(); ++i)
#define nvarep(n, a)                                                           \\
  ll n;                                                                        \\
  cin >> n;                                                                    \\
  vector<ll> a(n);                                                             \\
  rep(i, 0, n) cin >> a[i]
#define vecrep(n, a, type)                                                     \\
  vector<type> a(n);                                                           \\
  rep(i, 0, n) cin >> a[i]
#define lcm(a, b) (a / __gcd(a, b) * b)
#define range(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define nnn "\\n"
#define spa " "
using p = pair<ll, ll>;
using garph = vector<vector<ll>>;
const int inf = 2147483647;         // 2*10^9
const ll INF = 9223372036854775807; // 9*10^18

signed main() {
  nvarep(n, a);
  int q;
  cin >> q;
  vector<int> m(100001);
  vector<int> p(100001);
  rep(i, 0, n) {
    m[a[i]] += a[i];
    p[a[i]]++;
  }
  rep(i, 0, q) {
    int b, c;
    cin >> b >> c;
    m[c] += p[b] * c;
    p[c] += p[b];
    m[b] = p[b] = 0;
    cout << accumulate(range(m), 0LL) << nnn;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long int n, q, sum = 0, j = 0;
  cin >> n;
  vector<long long int> arr(100001, 0);
  for (int i = 0; i < n; i++) {
    long long int a;
    cin >> a;
    sum += a;
    arr[a]++;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    long long int b, c;
    cin >> b >> c;
    sum += (c - b) * arr[b];
    arr[c] += arr[b];
    arr[b] = 0;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>

#define F first
#define S second
#define int long long
#define pb push_back
#define endl '\\n'

using namespace std;

int32_t main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  map<int, int> m;
  int sum = 0;
  for (int i = 0, tmp; i < n; i++) {
    cin >> tmp;
    sum += tmp;
    m[tmp]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    sum += m[a] * (b - a);
    cout << sum << endl;
    m[b] += m[a];
    m[a] = 0;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define F first
#define S second
#define ll long long
#define pb push_back
#define endl '\\n'

using namespace std;

const int N = 1e+5 + 1;

ll n, q, say, a[N], t[4 * N], sum;

void build(int v, int tl, int tr) {
  if (tl > tr)
    return;
  if (tl == tr) {
    t[v] = a[tl];
    return;
  }
  int tm = (tl + tr) / 2;
  build(v * 2, tl, tm);
  build(v * 2 + 1, tm + 1, tr);
  t[v] = t[v * 2] + t[v * 2 + 1] * 1LL;
}

void sums(int v, int tl, int tr, int old_value, int new_value) {
  if (tl > tr)
    return;
  if (tl == tr) {
    if (t[v] == old_value)
      t[v] = new_value, say++;
    return;
  }
  int tm = (tl + tr) / 2;
  sums(v * 2, tl, tm, old_value, new_value);
  sums(v * 2 + 1, tm + 1, tr, old_value, new_value);
}

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  build(1, 0, n - 1);
  sum = t[1];
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    say = 0;
    sums(1, 0, n - 1, l, r);
    sum += say * (r - l) * 1LL;
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const long long MAX = 100005;

int main(void) {
  long long N;
  cin >> N;
  vector<long long> bucket(MAX);
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    long long tmp;
    cin >> tmp;
    sum += tmp;
    bucket[tmp]++;
  }

  long long Q;
  cin >> Q;

  long long B, C;
  for (int i = 0; i < Q; i++) {
    cin >> B >> C;
    long long diff = C - B;
    sum += bucket[B] * diff;
    bucket[C] += bucket[B];
    bucket[B] = 0;
    cout << sum << "\\n";
  } // for

} // main


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const long long MAX = 100005;

int main(void) {
  long long N;
  cin >> N;
  vector<long long> bucket(N);
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    long long tmp;
    cin >> tmp;
    sum += tmp;
    bucket[tmp]++;
  }

  long long Q;
  cin >> Q;

  long long B, C;
  for (int i = 0; i < Q; i++) {
    cin >> B >> C;
    long long diff = C - B;
    sum += bucket[B] * diff;
    bucket[C] += bucket[B];
    bucket[B] = 0;
    cout << sum << "\\n";
  } // for

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

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

int main() {
  int N;
  int A;
  map<int, int> counter;
  cin >> N;
  long long s = 0;
  for (int i = 0; i < N; i++) {
    cin >> A;
    counter[A]++;
    s += A;
  }

  int Q;
  cin >> Q;
  int B, C;
  for (int i = 0; i < Q; i++) {
    cin >> B >> C;
    s += (counter[B] * (C - B));
    counter[C] += counter[B];
    counter[B] = 0;
    cout << s << endl;
  }
}

Error: TLE

Fixed submission:

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

int get_sum(map<int, int> counter) {
  int s = 0;
  for (map<int, int>::iterator it = counter.begin(); it != counter.end();
       ++it) {
    s += it->first * it->second;
  }
  return s;
}

int main() {
  int N;
  int A;
  map<int, int> counter;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A;
    counter[A]++;
  }
  int s = get_sum(counter);

  int Q;
  cin >> Q;
  int B, C;
  for (int i = 0; i < Q; i++) {
    cin >> B >> C;
    counter[C] += counter[B];
    counter[B] = 0;
    cout << get_sum(counter) << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <string>

using namespace std;
int main() {

  int n;
  scanf("%d", &n);

  int arr[100000];

  fill_n(arr, 100000, 0); // set all index value to 0
  int i;

  int input;
  long long int sum = 0;
  for (i = 0; i < n; i++) {
    scanf("%d", &input);
    arr[input - 1] = arr[input - 1] + 1;
    sum = sum + input;
  }

  int q;
  scanf("%d", &q);
  int old_val, new_val;

  while (q--) {
    scanf("%d", &old_val);
    scanf("%d", &new_val);

    sum = ((new_val - old_val) * arr[old_val - 1]) + sum;
    printf("%lld\\n", sum);

    arr[new_val - 1] = arr[new_val - 1] + arr[old_val - 1];
    arr[old_val - 1] = 0;
  }
}


Error: TLE

Fixed submission:

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

int i, j;
int main() {
  int n;
  scanf("%d", &n);

  int arr[n];

  for (i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
  }

  int q;
  scanf("%d", &q);
  int old_val, new_val;

  // replacing
  while (q--) {
    long long int sum = 0;
    scanf("%d", &old_val);
    scanf("%d", &new_val);

    replace(arr, arr + n, old_val, new_val);

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

    printf("%d\\n", sum);
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;
using ll = long long;

int main() {
  ll n;
  cin >> n;
  vector<ll> vec(1e5 + 1, 0);
  for (ll i = 0; i < n; ++i) {
    ll a;
    cin >> a;
    ++vec[a];
  }

  ll q;
  cin >> q;

  ll vsum = 0;
  for (ll j = 1; j <= 1e5 + 1; ++j)
    vsum += j * vec[j];

  for (ll i = 0; i < q; ++i) {
    ll b, c;
    cin >> b >> c;
    vsum -= vec[b] * b;
    vsum += vec[b] * c;
    vec[c] += vec[b];
    vec[b] = 0;
    cout << vsum << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> vec(1e5 + 1);
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    ++vec[a];
  }

  int q;
  cin >> q;

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

    long long vsum = 0;
    for (int j = 1; j <= 1e5 + 1; ++j)
      vsum += j * vec[j];
    cout << vsum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long N, Q;
  cin >> N;
  vector<long long> A(N);
  vector<long long> num(100000 + 1, 0);
  for (long long i = 0; i < N; i++) {
    cin >> A.at(i);
    num.at(A.at(i))++;
  }
  cin >> Q;
  vector<long long> ans(Q + 1, 0);
  for (long long i = 0; i < N; i++)
    ans.at(0) += A.at(i);
  //    ans.at(0) = accumulate(A.begin(), A.end(), 0);
  for (long long i = 1; i <= Q; i++) {
    ans.at(i) = ans.at(i - 1);
    long long B, C;
    cin >> B >> C;
    ans.at(i) += (num.at(B) * (C - B));
    num.at(C) += num.at(B);
    num.at(B) = 0;
  }
  for (long long i = 1; i <= Q; i++)
    cout << ans.at(i) << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  long long N, Q;
  cin >> N;
  vector<long long> A(N);
  for (long long i = 0; i < N; i++)
    cin >> A.at(i);
  cin >> Q;
  vector<long long> ans(Q + 1);
  ans.at(0) = accumulate(A.begin(), A.end(), 0);
  for (long long i = 1; i <= Q; i++) {
    sort(A.begin(), A.end());
    ans.at(i) = ans.at(i - 1);
    long long B, C;
    cin >> B >> C;
    long long j = 0;
    while (j < N) {
      if (A.at(j) < B)
        j++;
      else if (A.at(j) == B) {
        ans.at(i) = ans.at(i) - B + C;
        A.at(j) = C;
        j++;
      } else
        break;
    }
  }
  for (long long i = 1; i < Q + 1; i++)
    cout << ans.at(i) << endl;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int size_max = 100001;
int v[size_max];
int kolvo[size_max] = {0};
int main() {
  int n;
  cin >> n;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    kolvo[v[i]]++;
    sum += v[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    sum = sum + (kolvo[b] * (c - b));
    kolvo[c] += kolvo[b];
    kolvo[b] = 0;
    cout << sum << '\\n';
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int size_max = 100001;
int v[size_max];
int main() {
  int n;
  cin >> n;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    for (int i = 0; i < n; i++) {
      if (v[i] == b)
        v[i] = c;
      sum += v[i];
    }
    cout << sum << '\\n';
    sum = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

using std::cin;
using std::cout;
using std::endl;
using std::pow;
using std::string;
using std::vector;

int main() {
  long long int N, Q, i, tmp, out = 0;
  cin >> N;
  vector<int> a(1000000, 0);
  for (i = 0; i < N; i++) {
    cin >> tmp;
    a.at(tmp)++;
    out += tmp;
  }
  cin >> Q;
  vector<int> b(Q), c(Q);
  for (i = 0; i < Q; i++)
    cin >> b.at(i) >> c.at(i);
  for (i = 0; i < Q; i++) {
    tmp = a.at(b.at(i));  // count
    out -= tmp * b.at(i); // count*value
    out += tmp * c.at(i);
    cout << out << endl;
    a.at(b.at(i)) = 0;
    a.at(c.at(i)) += tmp;
  }
}

Error: 0

Fixed submission:

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

using std::cin;
using std::cout;
using std::endl;
using std::pow;
using std::string;
using std::vector;

int main() {
  int N, Q, i, out = 0, tmp;
  cin >> N;
  vector<int> a(100000, 0);
  for (i = 0; i < N; i++) {
    cin >> tmp;
    a.at(tmp)++;
    out += tmp;
  }
  cin >> Q;
  vector<int> b(Q), c(Q);
  for (i = 0; i < Q; i++)
    cin >> b.at(i) >> c.at(i);
  for (i = 0; i < Q; i++) {
    tmp = a.at(b.at(i));  // count
    out -= tmp * b.at(i); // count*value
    out += tmp * c.at(i);
    cout << out << endl;
    a.at(b.at(i)) = 0;
    a.at(c.at(i)) += tmp;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N, Q;
  long long A, B, C, n;
  long long ans_tmp = 0;
  long long gap = 0;
  map<long long, long long> cont;

  std::cin >> N;

  for (int i = 0; i < N; i++) {
    std::cin >> A;
    if (cont.count(A) != 0) {
      cont[A] = cont[A] + 1;
    } else {
      cont[A] = 1;
    }
    ans_tmp = ans_tmp + A;
  }

  std::cin >> Q;
  vector<long long> ans(Q);

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

    if (cont.count(B) != 0) {
      n = cont[B];
      cont[B] = 0;
      if (cont.count(C) != 0) {
        cont[C] = cont[C] + n;
      } else {
        cont[C] = n;
      }
      ans_tmp = ans_tmp + n * (C - B);
      ans[i] = ans_tmp;
    } else {
      ans[i] = ans_tmp;
    }
  }

  for (int i = 0; i < Q; i++) {
    std::cout << ans[i] << "\\n";
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int N, Q, B, C;
  int ans_tmp = 0;

  std::cin >> N;
  vector<int> A(N);

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

  std::cin >> Q;
  vector<int> ans(Q);

  for (int i = 0; i < Q; i++) {
    std::cin >> B >> C;
    for (int j = 0; j < N; j++) {
      if (A[j] == B) {
        A[j] = C;
      }
      ans_tmp = ans_tmp + A[j];
    }
    ans[i] = ans_tmp;
    ans_tmp = 0;
  }

  for (int i = 0; i < Q; i++) {
    std::cout << ans[i] << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n; i > 0; --i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int MAX = 100000;
const int INF = (1 << 12);

int main() {
  int N, Q;
  ll ans = 0;
  cin >> N;
  vector<int> A(MAX + 1);
  rep(i, MAX + 1) A[i] = 0;
  rep(i, N) {
    int a;
    cin >> a;
    A[a] += 1;
    ans += a;
  }
  cin >> Q;
  rep(i, Q) {
    int B, C;
    cin >> B >> C;
    if (A[B]) {
      ans += (C - B) * A[B];
      A[C] += A[B];
      A[B] = 0;
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n; i > 0; --i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int MAX = 100000;
const int INF = (1 << 12);

int main() {
  int N, Q;
  cin >> N;
  vector<int> A(MAX + 1);
  rep(i, MAX + 1) A[i] = 0;
  rep(i, N) {
    int a;
    cin >> a;
    A[a]++;
  }
  cin >> Q;
  rep(i, Q) {
    int B, C;
    cin >> B >> C;
    A[C] += A[B];
    A[B] = 0;

    ll ans = 0;
    rep(j, MAX + 1) ans += (j * A[j]);
    cout << ans << endl;
  }

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

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

int main() {
  int64_t N, Q, a, b, c, sum = 0;
  cin >> N;
  vector<int64_t> V(100010, 0);
  for (int i = 0; i < N; i++) {
    cin >> a;
    sum += a;
    V.at(a)++;
  }

  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> b >> c;
    int64_t n = V[b];
    V[b] = 0;
    V[c] += n;
    sum = sum - (b * n) + (c * n);
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

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

void ans(map<int64_t, int64_t> &mp) {
  int64_t sum = 0;
  for (auto it = mp.begin(); it != mp.end(); it++) {
    sum += (it->first) * (it->second);
  }
  cout << sum << endl;
}

int main() {
  int64_t N, Q, a, b, c;
  cin >> N;
  map<int64_t, int64_t> mp;
  for (int i = 0; i < N; i++) {
    cin >> a;
    mp[a]++;
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> b >> c;
    int64_t n = mp[b];
    mp.erase(b);
    mp[c] += n;
    ans(mp);
  }
}