alexjercan/bugnet huggingface dataset:
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 < (int)(n); i++)
using ll = long long;

const int MX = 100001;

int main() {
  int n;
  cin >> n;
  vector<ll> cnt(MX);
  rep(i, n) {
    int a;
    cin >> a;
    cnt[a]++;
  }
  ll tot = 0;
  rep(i, MX) tot += i * cnt[i];

  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    tot -= b * cnt[b];
    tot -= c * cnt[c];
    cnt[c] += cnt[b];
    cnt[b] = 0;
    tot += b * cnt[b];
    tot += c * cnt[c];
    cout << tot << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  string a = "";
  cin >> n;
  rep(i, n) {
    string tmp;
    cin >> tmp;
    a += tmp;
  }

  int q;
  cin >> q;
  rep(i, q) {
    int place, ans = 0;
    char b, c;
    cin >> b >> c;

    place = a.find(b);
    while (place >= 0) {
      a[place] = c;
      place = a.find(b);
    }

    rep(i, a.length()) ans += a[i] - '0';
    cout << ans << 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)
using namespace std;
using ll = long long;
using vi = vector<int>;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template <typename T, typename... Args>
void ndarray(vector<T> &vec, int len, Args... args) {
  vec.resize(len);
  for (auto &v : vec)
    ndarray(v, args...);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  map<ll, ll> a;
  ll s = 0;
  rep(i, n) {
    int x;
    cin >> x;
    ++a[x];
  }
  for (auto p : a) {
    s += p.first * p.second;
  }
  int q;
  cin >> q;
  vector<ll> b(q);
  vector<ll> c(q);
  rep(i, q) cin >> b[i] >> c[i];
  rep(i, q) {
    s = s - a[b[i]] * (b[i] - c[i]);
    cout << s << "\\n";
    a[c[i]] += a[b[i]];
    a[b[i]] = 0;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using vi = vector<int>;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template <typename T, typename... Args>
void ndarray(vector<T> &vec, int len, Args... args) {
  vec.resize(len);
  for (auto &v : vec)
    ndarray(v, args...);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n);
  ll s = 0;
  rep(i, n) {
    cin >> a[i];
    s += a[i];
  }
  int q;
  cin >> q;
  vector<ll> b(q);
  vector<ll> c(q);
  rep(i, q) cin >> b[i] >> c[i];
  ll dp[10010];
  rep(i, 10005) dp[i] = 0;
  rep(i, n)++ dp[a[i]];
  rep(i, q) {
    s = s - dp[b[i]] * (b[i] - c[i]);
    cout << s << "\\n";
    dp[c[i]] += dp[b[i]];
    dp[b[i]] = 0;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int MAX = 100005;

int main() {
  int n;
  cin >> n;
  vector<ll> cnt(MAX, 0);
  long long sum = 0;
  rep(i, n) {
    int a;
    cin >> a;
    cnt[a]++;
  }
  rep(i, MAX) sum += i * cnt[i];

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


Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  Vi a(n);
  int sum = 0;
  rep(i, n) {
    cin >> a.at(i);
    sum += a.at(i);
  }
  int q;
  cin >> q;
  Vi b(q), c(q), s(q);
  rep(i, q) {
    cin >> b.at(i) >> c.at(i);
    auto it = find(a.begin(), a.end(), b.at(i));
    while (it != a.end()) {
      *it = c.at(i);
      sum -= b.at(i);
      sum += c.at(i);
      it = find(a.begin(), a.end(), b.at(i));
    }
    cout << sum << endl;
    s.push_back(sum);
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  vector<long long int> A(100003);

  long long int sum = 0;

  for (int i = 1; i < N + 1; i++) {
    long int j = 0;
    cin >> j;
    A.at(j)++;
    sum += j;
  }

  long long int Q;
  cin >> Q;

  vector<long long int> B(Q), C(Q);
  for (long long int i = 0; i < Q; i++) {
    cin >> B.at(i) >> C.at(i);
  }

  for (long long int i = 0; i < Q; i++) {
    sum += (C.at(i) - B.at(i)) * A.at(B.at(i));
    A.at(C.at(i)) += A.at(B.at(i));
    A.at(B.at(i)) = 0;

    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> A(100003);
  vector<int> b(100003);
  vector<int> c(100003);
  queue<int> que;

  for (int i = 1; i < N + 1; i++) {
    int j = 0;
    cin >> j;
    A.at(j)++;

    if (b.at(j) == 0) {
      b.at(j) = j;
      // cout << b.at(j)<< " ";
    }
    // cout <<endl;
  }

  int Q;
  cin >> Q;

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

  for (int i = 0; i < Q; i++) {
    /*cout <<"b before ";
    for(int j=0; j<10; j++){
      cout << b.at(j) <<" ";
    }
    cout << endl;*/

    A.at(C.at(i)) += A.at(B.at(i));
    A.at(B.at(i)) = 0;
    if (b.at(C.at(i)) == 0) {
      b.at(C.at(i)) = C.at(i);
    }
    b.at(B.at(i)) = 0;

    /*cout <<"b after ";
    for(int j=0; j<10; j++){
      cout << b.at(j) <<" ";
    }
    cout << endl;*/

    c = b;
    sort(c.begin(), c.end(), greater<int>());

    /*cout <<"c  " ;
    for(int j=0; j<10; j++){
      cout << c.at(j) <<" ";
    }
    cout << endl;*/

    int sum = 0;
    int k = 0;
    while (c.at(k) != 0) {
      sum += c.at(k) * A.at(c.at(k));
      k++;
    }

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

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long int lli;
int main(void) {
  int N;
  cin >> N;
  vector<int> a;
  int tmp;
  for (int i = 0; i < N; i++) {
    cin >> tmp;
    a.push_back(tmp);
  }
  sort(a.begin(), a.end());
  int *x;
  int n = a[N - 1];
  x = new int[100000];
  fill(x, x + (n + 1), 0);
  for (int i = 0; i < N; i++) {
    x[a[i]]++;
  }
  int Q;
  int b, c;
  cin >> Q;
  lli *sum;
  sum = new lli[Q];

  /*cin >> b >> c;
  replace(a.begin(), a.end(), b, c);
  sum.push_back(accumulate(a.begin(), a.end(), 0));
  */
  lli tmp1 = 0;
  for (int i = 0; i < N; i++) {
    tmp1 += a[i];
  }
  cin >> b >> c;
  sum[0] = tmp1 + x[b] * (c - b);
  x[c] += x[b];
  x[b] = 0;
  for (int i = 1; i < Q; i++) {
    cin >> b >> c;
    sum[i] = sum[i - 1] + x[b] * (c - b);
    x[c] += x[b];
    x[b] = 0;
  }
  for (int i = 0; i < Q; i++) {
    cout << sum[i] << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long int lli;
int main(void) {
  int N;
  cin >> N;
  vector<int> a;
  int tmp;
  for (int i = 0; i < N; i++) {
    cin >> tmp;
    a.push_back(tmp);
  }
  int Q;
  int b, c;
  cin >> Q;
  vector<lli> sum;
  lli tmp1;
  for (int i = 0; i < Q; i++) {
    cin >> b >> c;
    replace(a.begin(), a.end(), b, c);
    tmp1 = accumulate(a.begin(), a.end(), 0);
    sum.push_back(tmp1);
  }
  for (int i = 0; i < Q; i++) {
    cout << sum[i] << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  // 標準入力高速化のおまじない
  // scanfとの同期を切る。以降紛らわしいのでscanf使用不可。
  ios::sync_with_stdio(false);
  // // cin前の自動flashをやめる
  cin.tie(nullptr);

  int N;
  cin >> N;
  vector<ll> vec(10e5 + 1, 0);
  ll result = 0;
  for (int i = 0; i < N; i++) {
    int t;
    cin >> t;
    vec[t]++;
    result += t;
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int replace_before, replace_after;
    cin >> replace_before >> replace_after;
    ll replace_count = vec[replace_before];
    result -= (replace_before * replace_count);
    result += (replace_after * replace_count);
    vec[replace_after] += replace_count;
    vec[replace_before] = 0;

    cout << result << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = unsigned long long;
int main() {
  // 標準入力高速化のおまじない
  // scanfとの同期を切る。以降紛らわしいのでscanf使用不可。
  ios::sync_with_stdio(false);
  // // cin前の自動flashをやめる
  cin.tie(nullptr);

  int N;
  cin >> N;
  vector<int> vec(N + 1, 0);
  ll result = 0;
  for (int i = 0; i < N; i++) {
    int t;
    cin >> t;
    vec[t]++;
    result += t;
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int replace_before, replace_after;
    cin >> replace_before >> replace_after;
    int replace_count = vec[replace_before];
    result = result - (replace_before * replace_count) +
             (replace_after * replace_count);
    vec[replace_after] += replace_count;
    vec[replace_before] = 0;

    cout << result << endl;
  }

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

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

int main() {
  // your code goes here
  ll n;
  cin >> n;
  ll a[n], s = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    s = s + a[i];
  }
  map<ll, ll> m;
  for (ll i = 0; i < n; i++)
    m[a[i]]++;
  ll q;
  cin >> q;
  for (ll i = 1; i <= q; i++) {
    ll b, c;
    cin >> b >> c;
    s = s - (b * m[b]);
    s = s + (c * m[b]);
    m[c] = m[c] + m[b];
    m[b] = 0;
    cout << s << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  // your code goes here
  ll n;
  cin >> n;
  ll a[n];
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll q, sum = 0;
  cin >> q;
  for (ll i = 1; i <= q; i++) {
    ll b, c;
    cin >> b >> c;
    for (ll j = 0; j < n; j++) {
      if (a[j] == b)
        a[j] = c;
      sum = sum + a[j];
    }
    cout << sum << endl;
    sum = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <fstream>
#include <iostream>
#include <vector>
#define MAX 1e5 + 1

int main() {
  int n, q, temp;
  long long sum = 0;
  std::cin >> n;
  std::vector<int> a(MAX, 0);

  for (int i = 0; i < n; i++) {
    std::cin >> temp;
    a.at(temp)++;
    sum += temp;
  }
  std::cin >> q;
  std::vector<long long> b(q), c(q);
  int x, y;
  for (int i = 0; i < q; i++) {
    // std::cin >> b[i] >> c[i];
    std::cin >> x >> y;
    b[i] = x;
    c[i] = y;
    if (b[i] <= 0 || c[i] <= 0) {
      std::cout << i << "  x:" << x << ":" << b[i] << "  y" << y << ":" << c[i]
                << std::endl;
    }
  }
  // std::ofstream outputfile("out2.txt");

  for (int j = 0; j < q; j++) {

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

    std::cout << sum << std::endl;
    // outputfile << sum << std::endl;
  }
  // outputfile.close();

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
#define MAX 1e5

int main() {
  long long n, q, temp;
  long long sum = 0;
  std::cin >> n;
  std::vector<long long> a(MAX, 0);
  for (int i = 0; i < n; i++) {
    std::cin >> temp;
    a.at(temp)++;
    sum += temp;
  }
  std::cin >> q;
  long long b[q], c[q];
  for (long long i = 0; i < q; i++) {
    std::cin >> b[i] >> c[i];
  }

  for (long long j = 0; j < q; j++) {
    sum -= b[j] * a.at(b[j]);
    sum += c[j] * a.at(b[j]);
    a.at(c[j]) += a.at(b[j]);
    a.at(b[j]) = 0;
    std::cout << sum << std::endl;
  }

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

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int mod = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  ll x = 0;
  int a, d[100001] = {};
  for (int i = 0; i < n; i++) {
    cin >> a;
    d[a]++;
    x += a;
  }
  int q;
  cin >> q;
  int b, c;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    x += 1ll * (c - b) * d[b];
    cout << x << endl;
    d[c] += d[b];
    d[b] = 0;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int mod = 1000000007;

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

#include <iostream>
#include <vector>

using namespace std;
int main() {
  int N, Q;
  vector<int> A;
  vector<long long int> S;
  int i;
  int count_max;
  long long int Si;

  int count[100000 + 1] = {};

  cin >> N;
  for (i = 0; i < N; i++) {
    int ai;
    cin >> ai;
    A.push_back(ai);
    count[ai] += 1;
    Si += ai;
  }

  cin >> Q;

  int count_bi;
  int bi, ci;
  for (i = 0; i < Q; i++) {
    cin >> bi >> ci;
    count_bi = count[bi];
    Si = Si - (count_bi * bi) + (count_bi * ci);
    count[ci] = count[ci] + count_bi;
    count[bi] = count[bi] - count_bi;
    ;
    S.push_back(Si);
  }

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

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;
int main() {
  int N, Q;
  vector<int> A;
  vector<int> B;
  vector<int> C;

  int i, j;
  long int S;
  vector<long int> S_sum;

  cin >> N;

  S = 0;

  for (i = 0; i < N; i++) {
    int ai;
    cin >> ai;
    A.push_back(ai);
    S += ai;
  }

  cin >> Q;

  for (i = 0; i < Q; i++) {
    long int bi, ci;
    cin >> bi >> ci;
    for (j = 0; j < N; j++) {
      if (A[j] == bi) {
        S = S + ci - bi;
        A[j] = ci;
      }
    }
    S_sum.push_back(S);
  }
  for (i = 0; i < Q; i++) {
    cout << S_sum[i] << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using ll = long long;
int main() {
  ll N;
  cin >> N;
  map<ll, ll> A;
  ll ans = 0;
  for (ll i = 0; i < N; i++) {
    ll sub;
    cin >> sub;
    A[sub] = ++A[sub];
    ans += sub;
  }

  ll Q;
  cin >> Q;
  while (Q) {
    ll B, C;
    cin >> B >> C;

    if (A.count(B)) {
      ll dis = (C - B) * A[B];
      ans += dis;
      A[C] += A.at(B);
      A.erase(B);
    }
    cout << ans << endl;

    Q--;
  }
}

Error: TLE

Fixed submission:

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

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

  ll Q;
  cin >> Q;
  while (Q) {
    ll B, C;
    cin >> B >> C;
    if (A.count(B)) {
      A[C] += A.at(B);
      A.erase(B);
      // cout << "------" << endl;
      /*for(pair<ll, ll> i:A)cout << i.first << " " << i.second << endl;*/
    }
    ll ans = 0;
    for (pair<ll, ll> i : A) {
      ans += i.first * i.second;
    }
    cout << ans << endl;
    Q--;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

// The idea of binary search and achieving O(QlogN) was good
// But reassigning each value defeated the point since the worse case scenario
// would be O(QN), which is TLE Replacing the array with a count array should
// have been the first thing to come to my mind But I was way too occupied with
// the previous problem.

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    sum += A[i];
  }
  int Q;
  cin >> Q;
  int B[Q];
  int C[Q];
  for (int i = 0; i < Q; i++) {
    cin >> B[i] >> C[i];
  }

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

  // O(N)
  unordered_map<int, int> m;
  int i = 0;
  while (i < N) {
    int num = A[i];
    int i_low = i;
    while (i < N && A[i] == num) {
      i++;
    }
    m[num] = i - i_low;
  }

  // O(QlogN)
  for (int i = 0; i < Q; i++) {
    int count = m[B[i]];
    m[B[i]] = 0; // wow this is Godly
    m[C[i]] += count;
    sum += (C[i] - B[i]) * count;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    sum += A[i];
  }
  int Q;
  cin >> Q;
  int B[Q];
  int C[Q];
  for (int i = 0; i < Q; i++) {
    cin >> B[i] >> C[i];
  }

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

  // O(QlogN)
  for (int i = 0; i < Q; i++) {
    int hi = N;
    int lo = -1;
    while (hi - lo > 1) {
      int mid = (hi + lo) / 2;
      if (A[mid] >= B[i])
        hi = mid;
      else
        lo = mid;
    }
    int index = hi;
    while (A[index] == B[i]) {
      A[index] = C[i];
      sum += (C[i] - B[i]);
      index++;
    }
    cout << sum << endl;
    sort(A.begin(), A.end());
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, q;
  cin >> n;
  unordered_map<int, int> a;
  ll current = 0;
  rep(i, n) {
    int temp;
    cin >> temp;
    a[temp]++;
    current += temp;
  }

  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    if (a.count(b)) {
      int kosu = a.at(b);
      int erased = b * kosu;
      // current -= erased;
      int inserted = c * kosu;
      // current += inserted;
      current -= kosu * (b - c);
      a.erase(b);
      a[c] += kosu;

      // cout << "Changed: " << kosu << endl;
    }

    cout << current << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, q;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a.at(i); }

  ll current = accumulate(a.begin(), a.end(), 0);
  cin >> q;
  rep(i, q) {
    int b, c;
    ll ans;
    cin >> b >> c;
    auto temp = a.begin();
    while (true) {
      auto result = std::find(temp, a.end(), b);
      if (result == a.end()) {
        // std::cout << "not found" << std::endl;
        // cout << current << endl;
        break;
      } else {
        // std::cout << "found: " << *result << std::endl;
        current -= *result;
        *result = c;
        current += c;
        temp = result + 1;
      }
    }
    cout << current << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define PI 3.14159265358979323846264338327950L

int main(void) {
  long long N, Q, B, C, ans = 0, tmp_A;
  vector<long long> A(100010, 0);

  cin >> N;

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

  cin >> Q;

  for (long long i = 0; i < Q; i++) {
    cin >> B >> C;
    ans = ans - B * A[B];
    ans = ans + C * A[B];

    A[C] = A[C] + A[B];
    A[B] = 0;

    printf("%lld\\n", ans);
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define PI 3.14159265358979323846264338327950L

int main(void) {
  long long N, Q, B, C, ans = 0, tmp_A;
  map<long long, long long> A;

  cin >> N;

  for (long long i = 0; i < N; i++) {
    cin >> tmp_A;
    A[tmp_A]++;
  }

  cin >> Q;

  for (long long i = 0; i < Q; i++) {
    cin >> B >> C;
    A[C] = A[C] + A[B];
    A[B] = 0;
    ans = 0;
    for (auto j : A) {
      ans = ans + j.first * j.second;
    }

    printf("%lld\\n", ans);
  }

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

#include <iostream>
using namespace std;
int main() {
  long long int n, q, sum = 0, cnt = 0;
  cin >> n;
  long long int arr[n], i, b, c;
  long long int h[100005] = {0};
  for (i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
    h[arr[i]]++;
  }
  cin >> q;
  while (q--) {
    cin >> b >> c;
    cnt = h[b];
    sum -= cnt * b;
    sum += cnt * c;
    cout << sum << endl;
    h[c] += h[b];
    h[b] = 0;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  long long int n, q, sum = 0, a, b;
  cin >> n;
  int arr[n], i;
  for (i = 0; i < n; i++)
    cin >> arr[i];
  cin >> q;
  while (q--) {
    cin >> a >> b;
    for (i = 0; i < n; i++)
      if (arr[i] == a)
        arr[i] = b;
    for (i = 0; i < n; i++)
      sum += arr[i];
    cout << sum << endl;
    sum = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long N;
  cin >> N;
  vector<long> A(N);
  vector<long> R(100010, 0);
  long count = 0;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    count += A.at(i);
    R.at(A.at(i))++;
  }
  long Q;
  cin >> Q;
  vector<long> B(Q), C(Q);
  for (int i = 0; i < Q; i++)
    cin >> B.at(i) >> C.at(i);

  for (int i = 0; i < Q; i++) {
    if (R.at(B.at(i)) > 0) {
      long Bi = R.at(B.at(i));
      R.at(B.at(i)) = 0;
      R.at(C.at(i)) += Bi;
      count += Bi * (C.at(i) - B.at(i));
    }
    cout << count << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  int Q;
  cin >> Q;
  vector<int> B(Q);
  vector<int> C(Q);
  for (int i = 0; i < Q; i++) {
    cin >> B.at(i);
    cin >> C.at(i);
  }

  vector<int64_t> S(Q);
  vector<int64_t> T(Q + 1);
  for (int j = 0; j < N; j++)
    T.at(0) += A.at(j);
  for (int i = 0; i < Q; i++) {
    int count = 0;
    int dis = C.at(i) - B.at(i);
    for (int k = 0; k < N; k++) {
      if (A.at(k) == B.at(i)) {
        count++;
        A.at(k) = C.at(i);
      }
    }
    T.at(i + 1) = T.at(i) + dis * count;
    S.at(i) = T.at(i + 1);
  }
  for (int i = 0; i < Q; i++)
    cout << S.at(i) << endl;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>

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

  std::map<long long, long long> m;
  long long a;
  for (int i = 0; i < N; i++) {
    std::cin >> a;
    m[a]++;
  }

  std::cin >> Q;

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

  long long b, c, tmp;
  for (int i = 0; i < Q; i++) {
    std::cin >> b >> c;
    tmp = m[b];
    m[b] = 0;
    ans -= b * tmp;
    m[c] += tmp;
    ans += c * tmp;

    std::cout << ans << std::endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>

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

  std::map<long long, long long> m;
  long long a;
  for (int i = 0; i < N; i++) {
    std::cin >> a;
    m[a]++;
  }

  std::cin >> Q;

  long long b, c, tmp;
  for (int i = 0; i < Q; i++) {
    std::cin >> b >> c;
    tmp = m[b];
    m[b] = 0;
    m[c] += tmp;

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

    std::cout << ans << std::endl;
  }

  return 0;
}