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

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
ll a[222222], n, q, from, to, cnt[111111], ans;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
    ans += a[i];
  }
  cin >> q;
  while (q--) {
    cin >> from >> to;
    if (cnt[from]) {
      ans -= cnt[from] * from;
      ans += to * cnt[from];
      cnt[to] += cnt[from];
      cnt[from] = 0;
    }
    cout << ans << '\\n';
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
int a[222222], n, q, from, to;
ll tree[888888];
ll init(int node, int start, int end) {
  if (start == end)
    return tree[node] = a[start];
  return tree[node] = init(node * 2, start, start + end >> 1) +
                      init(node * 2 + 1, (start + end >> 1) + 1, end);
}
ll query(int node, int start, int end, int diff, int change) {
  if (!tree[node * 2] && !tree[node * 2 + 1]) {
    if (tree[node] == diff)
      return tree[node] = change;
    return tree[node];
  }
  return query(node * 2, start, start + end >> 1, diff, change) +
         query(node * 2 + 1, (start + end >> 1) + 1, end, diff, change);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  init(1, 1, n);
  cin >> q;
  while (q--) {
    cin >> from >> to;
    cout << query(1, 1, n, from, to) << '\\n';
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded


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

using namespace std;

#define REP(i, a, b) for (int i = a; i <= b; i++)
#define ALL(a) a.begin(), a.end()
#define ALLN(a, n) (a, a + n)
#define MAX 10000007
#define MIN -10000007

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
// freopen("error.txt", "w", stderr);
#endif

  int t;
  t = 1;
  //  cin>>t;
  while (t--) {
    ll n, a[100002], b[100003] = {0}, q, sum = 0, l, r;
    cin >> n;
    REP(i, 0, n - 1) {
      cin >> a[i];
      b[a[i]]++;
      sum += a[i];
    }
    cin >> q;
    REP(i, 0, q - 1) {
      cin >> l >> r;
      sum += (r - l) * b[l];
      b[r] += b[l];
      b[l] = 0;
      cout << sum << "\\n";
    }
  }
  cerr << "Time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
  return 0;
}


Error: TLE

Fixed submission:


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

using namespace std;

#define REP(i, a, b) for (int i = a; i <= b; i++)
#define ALL(a) a.begin(), a.end()
#define ALLN(a, n) (a, a + n)
#define MAX 10000007
#define MIN -10000007

ll S(ll x) {
  ll ans = sqrt(x);
  if (ans * ans == x)
    return true;
  return false;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
// freopen("error.txt", "w", stderr);
#endif

  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    /* ll n,k,c=0,d=0;
     cin>>n;
     if(n==1){cout<<"FastestFinger"<<"\\n";}
     else if(n%2!=0){cout<<"Ashishgup"<<"\\n";}
     else if(S(n)==true &&n%2!=0){cout<<"FastestFinger"<<"\\n";}
     else{
       for(int i=2;i*i<=n;i++){
         if(n%i==0){
           if(i%2==0) c++;
           else d++;
         }
       }
       if(n%2==0){k=abs(c+1-(d+1));}
       else{k=abs(d+2-c);}
       k!=1? cout<<"Ashishgup"<<"\\n" : cout<<"FastestFinger"<<"\\n";
     }
     */
    int n, q, sum = 0, l, r, input;
    std::vector<int> v;
    cin >> n;
    REP(i, 0, n - 1) {
      cin >> input;
      v.push_back(input);
    }
    cin >> q;
    REP(i, 0, q - 1) {
      cin >> l >> r;
      REP(i, 0, n - 1) {
        if (v[i] == l) {
          sum += r;
          v[i] = r;
        } else
          sum += v[i];
      }
      printf("%d\\n", sum);
      sum = 0;
    }
  }
  cerr << "Time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
int arr[22222222];
int a[22222222];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    sum += arr[i];
    a[arr[i]]++;
  }
  int x;
  cin >> x;
  while (x--) {
    int b, c;
    cin >> b >> c;
    ll cur = a[b];
    sum -= cur * b;
    a[b] = 0;
    sum += cur * c;
    a[c] += cur;
    cout << sum << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
int main() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int x;
  cin >> x;
  while (x--) {
    int a, b;
    cin >> a >> b;
    int c = b;
    ll sum = 0;
    for (int i = 0; i < n; i++) {
      if (arr[i] == a) {
        swap(arr[i], b);
        b = c;
      }
      sum += arr[i];
    }
    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 < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using Graph = vector<vector<int>>;
using ll = long long;

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

Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>
#define endl "\\n"
#define ll long long
#define eb emplace_back
#define sz(x) ((int)(x).size())
#define mo 1000000007
using namespace std;
ll getmul(ll n) {
  ll int k{0};
  while (n > 0) {
    k += n % 10;
    n = n / 10;
  }
  return k;
} // Sum OF n DIGITS
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll int n;
  cin >> n;
  ll int arr[n], sum{0};
  map<ll int, ll int> mp;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
    mp[arr[i]]++;
  }
  ll int q;
  cin >> q;
  ll int a[q][2];
  for (int i = 0; i < q; i++) {
    for (int j = 0; j < 2; j++)
      cin >> a[i][j];
  }
  for (int i = 0; i < q; i++) {
    ll int x = mp[a[i][0]];
    sum -= x * a[i][0];
    sum += x * a[i][1];
    if (mp.find(a[i][1]) != mp.end())
      mp[a[i][1]] += x;
    else
      mp[a[i][1]] = x;
    mp[a[i][0]] = 0;
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl "\\n"
#define ll long long
#define eb emplace_back
#define sz(x) ((int)(x).size())
#define mo 1000000007
using namespace std;
ll getmul(ll n) {
  ll int k{0};
  while (n > 0) {
    k += n % 10;
    n = n / 10;
  }
  return k;
} // Sum OF n DIGITS
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll int n;
  cin >> n;
  ll int arr[n], sum{0};
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  ll int q;
  cin >> q;
  ll int a[q][2];
  for (int i = 0; i < q; i++) {
    for (int j = 0; j < 2; j++)
      cin >> a[i][j];
  }
  for (int i = 0; i < q; i++) {
    sum = 0;
    for (int j = 0; j < n; j++) {
      if (arr[j] == a[i][0]) {
        sum += a[i][1];
        arr[j] = a[i][1];
      } else
        sum += arr[j];
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define FAST_INP                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
int main() {
  FAST_INP;
  long long n, a, b, c, q;
  cin >> n;
  set<long long> num;
  map<long long, long long> m;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a;
    num.insert(a);
    m[a]++;
    sum += a;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    sum -= (m[b] * b);
    m[c] += m[b];
    sum += (m[b] * c);
    m[b] = 0;
    cout << sum << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define FAST_INP                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
int main() {
  FAST_INP;
  long long n, q, sum = 0;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    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++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int MX = 100005;

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

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 P = pair<int, int>;
const int MX = 10005;

int main() {
  int n, q, b, c;
  ll ans;
  cin >> n;
  vector<ll> k(MX);
  rep(i, n) {
    int a;
    cin >> a;
    k[a]++;
  }
  ans = 0;
  rep(i, MX) ans += i * k[i];
  cin >> q;
  rep(i, q) {
    cin >> b >> c;
    ans -= b * k[b];
    ans -= c * k[c];
    k[c] += k[b];
    k[b] = 0;
    ans += c * k[c];
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#include <cmath>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vs = vector<string>;

int main() {
  ll N;
  cin >> N;
  vl A(N);
  map<ll, ll> a;
  ll sum = 0;
  for (ll i = 0; i < N; i++) {
    cin >> A.at(i);
    sum += A.at(i);
    a[A.at(i)] += 1;
  }

  ll Q;
  cin >> Q;
  ll b, c;
  ll tmp;
  for (ll i = 0; i < Q; i++) {
    cin >> b >> c;
    if (a.find(b) != a.end()) {
      tmp = a.at(b);
      sum -= b * a.at(b);
      a.at(b) = 0;
      a[c] += tmp;
      sum += c * tmp;
    }
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#include <cmath>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vs = vector<string>;

int main() {
  ll N;
  cin >> N;
  vl A(N);
  map<ll, ll> a;
  for (ll i = 0; i < N; i++) {
    cin >> A.at(i);
    a[A.at(i)] += 1;
  }

  ll Q;
  cin >> Q;
  ll b, c;
  ll tmp;
  for (ll i = 0; i < Q; i++) {
    ll sum = 0;
    cin >> b >> c;
    if (a.find(b) != a.end()) {
      tmp = a.at(b);
      a.at(b) = 0;
      a[c] += tmp;
    }
    for (auto c : a) {
      sum += c.first * c.second;
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N, Q, x;
  cin >> N;
  map<int, int> A;

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

  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int a, b;
    cin >> a >> b;
    sum = sum + (b - a) * A[a];
    A[b] += A[a];
    A[a] = 0;
    cout << sum << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

//
//  main.cpp
//  tpcoder
//
//  Created by Sankalp Bhamare on 29/07/20.
//  Copyright © 2020 sankalp. All rights reserved.
//

#include <iostream>
#include <map>
#include <math.h>
#include <vector>
using namespace std;

int main(int argc, const char *argv[]) {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    long long sum = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] == b) {
        a[i] = c;
      }
      sum += a[i];
    }
    cout << sum << endl;
  }
}
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++)
using namespace std;
typedef long long ll;

int main() {
  ll n, sum = 0;
  map<ll, ll> mp;
  cin >> n;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    sum += x;
    mp[x]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    ll b, c;
    cin >> b >> c;
    sum -= mp[b] * b;
    sum += mp[b] * c;
    mp[c] += mp[b];
    mp.erase(b);

    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

ll sum = 0;
int _count;

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

    _count = count(a.begin(), a.end(), b.at(i));
    a.erase(remove(a.begin(), a.end(), b.at(i)), a.end());
    rep(j, _count) a.push_back(c.at(i));

    sum = 0;
    rep(i, n) sum += a.at(i);
    cout << sum << endl;
  }

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

#include <algorithm>
#include <boost/foreach.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

std::vector<long long int> ans;
long long int N, Q, sum;
long long int A[100000 + 1], B[100000 + 1], C[100000 + 1];
long long int count[100000 + 1];

void func() {
  for (int i = 1; i <= N; i++) {
    sum += A[i];
    count[A[i]]++;
  }
}

int main() {
  std::cin >> N;
  for (int i = 1; i <= N; i++) {
    std::cin >> A[i];
  }
  std::cin >> Q;
  for (int i = 1; i <= Q; i++) {
    std::cin >> B[i] >> C[i];
  }

  func();

  for (int i = 1; i <= Q; i++) {
    long long int tmp = count[B[i]];
    count[B[i]] = 0;
    count[C[i]] += tmp;
    sum = sum + tmp * (C[i] - B[i]);
    ans.push_back(sum);
  }

  for (std::size_t i = 0; i < ans.size(); i++) {
    std::cout << ans[i] << std::endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <boost/foreach.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

std::vector<long long int> ans;
long long int N, Q, sum;
long long int A[10000 + 1], B[10000 + 1], C[10000 + 1];
long long int count[100000 + 1];

void func() {
  for (int i = 1; i <= N; i++) {
    sum += A[i];
    count[A[i]]++;
  }
}

int main() {
  std::cin >> N;
  for (int i = 1; i <= N; i++) {
    std::cin >> A[i];
  }
  std::cin >> Q;
  for (int i = 1; i <= Q; i++) {
    std::cin >> B[i] >> C[i];
  }

  func();

  for (int i = 1; i <= Q; i++) {
    long long int tmp = count[B[i]];
    count[B[i]] = 0;
    count[C[i]] += tmp;
    sum = sum + tmp * (C[i] - B[i]);
    ans.push_back(sum);
  }

  for (std::size_t i = 0; i < ans.size(); i++) {
    std::cout << ans[i] << std::endl;
  }

  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)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int MX = 100005;

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>

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

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

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

#define rep(inc, bgn, end) for (int inc = bgn; inc < end; ++inc)
#define repe(inc, bgn, end) for (int inc = bgn; inc <= end; ++inc)

using ll = long long;
using namespace std;
int main() {
  int n = -1; // < 10^5
  cin >> n;

  ll sum = 0;
  map<int, ll> valcnt;
  rep(i, 0, n) {
    int inp = -1;
    cin >> inp;
    valcnt[inp]++;
    sum += inp;
  }

  int q = -1;
  cin >> q;
  // n
  rep(i, 0, q) {
    int from_val, to_val;
    cin >> from_val >> to_val;

    sum += to_val * valcnt[from_val] - from_val * valcnt[from_val];

    // log n
    valcnt[to_val] += valcnt[from_val];
    valcnt[from_val] = 0;

    cout << sum << endl;

    //     // n
    //     ll ans = 0;
    //     for (auto x : valcnt) {
    //         // cout << x.first << " : " << x.second << endl;
    //         ans += x.first * x.second;
    //     }
    //     cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

#define rep(inc, bgn, end) for (int inc = bgn; inc < end; ++inc)
#define repe(inc, bgn, end) for (int inc = bgn; inc <= end; ++inc)

using ll = long long;
using namespace std;
int main() {
  int n = -1; // < 10^5
  cin >> n;

  map<int, ll> valcnt;
  rep(i, 0, n) {
    int inp = -1;
    cin >> inp;
    valcnt[inp]++;
  }

  int q = -1;
  cin >> q;
  // n
  rep(i, 0, q) {
    int from_val, to_val;
    cin >> from_val >> to_val;

    // log n
    valcnt[to_val] += valcnt[from_val];
    valcnt[from_val] = 0;

    // n
    ll ans = 0;
    for (auto x : valcnt) {
      // cout << x.first << " : " << x.second << endl;
      ans += x.first * x.second;
    }
    cout << ans << endl;
  }

  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)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, q;
  cin >> n;
  int tmp;
  int cnt[100000 + 7] = {};

  ll sum = 0;
  rep(i, n) {
    cin >> tmp;
    sum += tmp;
    cnt[tmp]++;
  }

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

Error: TLE

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 P = pair<int, int>;

int main() {
  int n, q;
  cin >> n;
  int tmp, foo;
  vector<int> a(n);

  int sum = 0;
  rep(i, n) {
    cin >> tmp;
    sum += tmp;
    a.at(i) = tmp;
  }

  cin >> q;
  int dif = 0;
  rep(i, q) {
    cin >> tmp >> foo;
    dif = foo - tmp;
    rep(j, n) {
      if (a.at(j) == tmp) {
        a.at(j) = foo;
        sum += dif;
      }
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int &Ai : A) {
    cin >> Ai;
  }
  int Q;
  cin >> Q;
  vector<int> B(Q);
  vector<int> C(Q);
  for (int i = 0; i < Q; ++i) {
    int bi, ci;
    cin >> bi >> ci;
    B[i] = bi;
    C[i] = ci;
  }

  // Value Ai --> the i where Ai is located
  unordered_map<int, vector<int>> locations;
  for (int i = 0; i < N; ++i) {
    locations[A[i]].push_back(i);
  }

  long long sum = 0;
  for (int Ai : A)
    sum += Ai;

  for (int i = 0; i < Q; ++i) {
    int Bi = B[i];
    int Ci = C[i];

    vector<int> locs = locations[Bi];
    locations[Ci].insert(locations[Ci].end(), locs.begin(), locs.end());

    int nl = locs.size();
    sum += nl * (Ci - Bi);
    locations[Bi].clear();

    for (int ii : locs) {
      A[ii] = Ci;
    }
    cout << sum << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int &Ai : A) {
    cin >> Ai;
  }
  int Q;
  cin >> Q;
  vector<int> B(Q);
  vector<int> C(Q);
  for (int i = 0; i < Q; ++i) {
    int bi, ci;
    cin >> bi >> ci;
    B[i] = bi;
    C[i] = ci;
  }

  // Value Ai --> the i where Ai is located
  unordered_map<int, vector<int>> locations;
  for (int i = 0; i < N; ++i) {
    locations[A[i]].push_back(i);
  }
  for (int i = 0; i < Q; ++i) {
    // replace elements whose value is Bi into Ci
    int Bi = B[i];
    int Ci = C[i];
    vector<int> locs = locations[Bi];
    for (int ii : locs) {
      A[ii] = Ci;
      locations[Ci].push_back(ii);
    }
    locations[Bi].clear();
    int sum = 0;
    for (int Ai : A)
      sum += Ai;
    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 < (int)(n); i++)
const int INF = 1 << 29;
const int MOD = 1000000007;

int main() {
  int n;
  cin >> n;
  // 値：個数のmap
  map<long long, long long> mp;
  long long a;
  rep(i, n) {
    cin >> a;
    mp[a] += 1;
  }

  int q;
  cin >> q;

  // 最初に全ての和を求めておく
  long long sum = 0;
  for (auto itr : mp) {
    sum += itr.first * itr.second;
  }

  // Q回操作
  int b, c;
  rep(i, q) {
    cin >> b >> c;

    //  値Cの個数に値Bの個数を足して値Bの個数を0にする
    long long num_b;
    num_b = mp[b];
    mp[c] += num_b;
    mp[b] = 0;

    // sumの更新をする
    sum += (c - b) * num_b;
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  // 値：個数のmap
  map<long long, long long> mp;
  long long a;
  rep(i, n) {
    cin >> a;
    mp[a] += 1;
  }

  int q;
  cin >> q;

  // Q回操作
  int b, c;
  rep(i, q) {
    cin >> b >> c;

    //  値Cの個数に値Bの個数を足して値Bの個数を0にする
    mp[c] += mp[b];
    mp[b] = 0;

    // 値*個数の総和をとる
    long long ans = 0;
    for (auto itr : mp) {
      ans += itr.first * itr.second;
    }

    cout << ans << endl;
  }
}