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

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

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;

#define F first
#define S second
#define pb push_back
#define mp make_pair
#define fr(i, a, b) for (int i = a; i < b; i++)

int isprime(ll n) {
  int flag = 0;
  for (ll i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      flag = 1;
      ;
      break;
    }
  }
  return flag;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int arr[n];
  map<int, int> m;
  ll sum1 = 0;

  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    m[arr[i]]++;
    sum1 += arr[i];
  }

  ll w = m.size();

  int q;
  cin >> q;
  ll sum[q + 1];
  memset(sum, 0, sizeof(sum));
  sum[0] = sum1;

  for (int i = 1; i <= q; i++) {
    int a, b;
    cin >> a >> b;
    if (m[a]) {
      sum[i] += (sum[i - 1] + ((b - a) * m[a]));
      m[b] += m[a];
      m[a] = 0;
    } else {
      sum[i] = sum[i - 1];
    }
  }

  for (int i = 1; i <= q; i++) {
    cout << sum[i] << "\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;

#define F first
#define S second
#define pb push_back
#define mp make_pair
#define fr(i, a, b) for (int i = a; i < b; i++)

int isprime(ll n) {
  int flag = 0;
  for (ll i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      flag = 1;
      ;
      break;
    }
  }
  return flag;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int arr[n];

  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int q;
  cin >> q;
  ll sum[q];
  memset(sum, 0, sizeof(sum));
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    for (int j = 0; j < n; j++) {
      if (arr[j] == a) {
        arr[j] = b;
      }
      sum[i] += arr[j];
    }
  }

  for (int i = 0; i < q; i++) {
    cout << sum[i] << "\\n";
  }

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

#include <bits/stdc++.h>
using namespace std;
#define arep(i, x, n) for (int i = int(x); i < (int)(n); i++)
#define rep(i, n) for (long long i = 0; i < n; ++i)
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9 + 7
using ll = long long;
const string alp = "abcdefghijklmnopqrstuvwxyz";

int main() {
  ll N;
  cin >> N;
  map<ll, ll> aMap;
  ll sum = 0;
  rep(i, N) {
    ll a;
    cin >> a;
    if (aMap[a]) {
      aMap[a]++;
    } else {
      aMap[a] = 1;
    }

    sum += a;
  }

  map<ll, ll>::iterator it;
  ll Q;
  cin >> Q;
  ll sumAll[Q];

  rep(i, Q) {
    ll B;
    ll C;
    cin >> B >> C;

    if (aMap[B]) {
      ll cnt = aMap[B];
      if (aMap[C]) {
        aMap[C] = aMap[C] + cnt;
      } else {
        aMap[C] = cnt;
      }
      sum = sum + cnt * (C - B);
      aMap.erase(B);
    }

    sumAll[i] = sum;
  }

  rep(i, Q) { cout << sumAll[i] << endl; }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define arep(i, x, n) for (int i = int(x); i < (int)(n); i++)
#define rep(i, n) for (long long i = 0; i < n; ++i)
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9 + 7
using ll = long long;
const string alp = "abcdefghijklmnopqrstuvwxyz";

int main() {
  ll N;
  cin >> N;
  map<ll, ll> aMap;
  rep(i, N) {
    ll a;
    cin >> a;
    if (aMap[a]) {
      aMap[a]++;
    } else {
      aMap[a] = 1;
    }
  }

  map<ll, ll>::iterator it;
  ll Q;
  cin >> Q;
  ll sumAll[Q];
  rep(i, Q) {
    ll B;
    ll C;
    cin >> B >> C;

    if (aMap[B]) {
      ll cnt = aMap[B];
      if (aMap[C]) {
        aMap[C] = aMap[C] + cnt;
      } else {
        aMap[C] = cnt;
      }
      aMap.erase(B);
    }

    ll sum = 0;
    for (it = aMap.begin(); it != aMap.end(); it++) {
      // cout << it->first << ":" << it->second << endl;
      sum += it->first * it->second;
    }

    sumAll[i] = sum;
  }

  rep(i, Q) { cout << sumAll[i] << endl; }

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

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int64_t N;
  cin >> N;
  vector<int64_t> vec(100001, 0);
  for (int64_t i = 0; i < N; i++) {
    int64_t num;
    cin >> num;
    vec[num] += 1;
  }

  int64_t sum = 0;
  for (int64_t i = 0; i < 100001; i++) {
    sum += (vec[i] * i);
  }

  int64_t Q;
  cin >> Q;
  for (int64_t i = 0; i < Q; i++) {
    int64_t B, C;
    cin >> B >> C;
    int64_t count = vec[B];
    vec[B] = 0;
    vec[C] += count;
    sum -= count * B;
    sum += count * C;
    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> vec(100001, 0);
  for (int i = 0; i < N; i++) {
    int num;
    cin >> num;
    vec[num] += 1;
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    int count = vec[B];
    vec[B] = 0;
    vec[C] += count;
    int64_t sum = 0;
    for (int i = 0; i < 100001; i++) {
      sum += (vec[i] * i);
    }
    cout << sum << endl;
  }

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

#include <bits/stdc++.h>
/**CLICK ss---> ctrl+j**/
#define PI 2.0 * acos(0.0)
#define ll long long int
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a) * (b)) / gcd(a, b)
#define MOD 1000000007
#define mx 123456789
using namespace std;
void solve() {
  int n, q, i, j, k, l;
  cin >> n;
  int a[n + 2];
  ll ans = 0;
  map<int, int> mp;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
    ans += a[i];
  }
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    ll tmp = (ll)u * mp[u];
    int tmp2 = mp[u];
    mp[u] = 0;
    mp[v] += tmp2;
    ll tmp1 = (ll)tmp2 * v;
    cout << ans - tmp + tmp1 << endl;
    ans = ans - tmp + tmp1;
  }
}
int main() {
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
/**CLICK ss---> ctrl+j**/
#define PI 2.0 * acos(0.0)
#define ll long long int
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a) * (b)) / gcd(a, b)
#define MOD 1000000007
#define mx 123456789
using namespace std;
void solve() {
  int n, m, q, i, j, k, l;
  cin >> n;
  int a[n + 2];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> q;
  while (q--) {
    int aa, b, sum = 0;
    cin >> aa >> b;
    for (i = 0; i < n; i++) {
      if (a[i] == aa)
        a[i] = b;
      sum += a[i];
    }
    cout << sum << endl;
  }
}
int main() {
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>

typedef long long ll;

constexpr long long INFLL = 1e18;

using namespace std;

int main() {
  map<int, int> m;
  int n;
  cin >> n;
  int temp;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    sum += temp;
    m[temp]++;
  }
  int q;
  cin >> q;
  cout << endl;
  int count;
  int b, c;
  while (q > 0) {
    cin >> b;
    cin >> c;

    count = m[b];
    m[b] = 0;
    m[c] += count;
    sum = sum + (c - b) * count;
    /*m[c] += m[b];
    sum = sum + (c - b) * m[b];
    m.erase(b);*/
    cout << sum << endl;
    q--;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>

typedef long long ll;

constexpr long long INFLL = 1e18;

using namespace std;

int main() {
  map<int, int> m;
  int n;
  cin >> n;
  int temp;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    sum += temp;
    m[temp]++;
  }
  int q;
  cin >> q;
  cout << endl;
  int count;
  int b, c;
  while (q > 0) {
    cin >> b;
    cin >> c;

    // count = m[b];
    // m[b] = 0;
    // m[c] += count;
    // sum = sum + (c - b) * count;
    m[c] += m[b];
    sum = sum + (c - b) * m[b];
    m.erase(b);
    cout << sum << endl;
  }

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

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

int main() {
  int n, q;
  cin >> n;
  map<int, int> A;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    A[a] += 1;
  }
  long sum = 0;
  for (auto x : A) {
    sum += x.first * x.second;
  }

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

    sum += A[b] * (c - b);
    A[c] += A[b];
    A.erase(b);

    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>

typedef long long ll;

constexpr long long INFLL = 1e18;

using namespace std;

int main() {
  map<int, int> m;
  int n;
  cin >> n;
  int temp;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    sum += temp;
    m[temp]++;
  }
  int q;
  cin >> q;
  int count;
  int b, c;
  while (q > 0) {
    cin >> b;
    count = m[b];
    m[b] = 0;
    cin >> c;
    m[c] += count;
    sum = sum + (c - b) * count;
    cout << sum << endl;
  }

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

#include <bits/stdc++.h>

using namespace std;
int main() {
  int n;
  cin >> n;
  //    map<int,int> m;
  long long v[100001] = {0};
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    v[a]++;
    sum += a;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    v[c] += v[b];
    sum += v[b] * (c - b);
    v[b] = 0;
    cout << sum << 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[i];
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    long long sum = 0;
    for (int j = 0; j < n; j++) {
      if (b == a[j]) {
        a[j] = c;
      }
      sum += a[j];
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define Fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define int long long
#define ld long double
#define S second
#define F first
#define mp make_pair
#define ms(x, y) memset(x, y, sizeof(x))
#define pb push_back
#define sl(n) cin >> n
#define pl(n) cout << n
#define sze(v) (int)(v.size())

typedef vector<int> Vl;
typedef pair<int, int> pii;
typedef vector<pii> Vll;
typedef vector<vector<int>> Vv;
typedef vector<pair<int, pii>> Vlll;
typedef priority_queue<int, vector<int>, greater<int>> PQL;
typedef unordered_map<int, int> ump;

const int mod = 1e9 + 7;
const int modx = 998244353;
const int Nx = 1e5 + 2;
const int inf = (int)(1e19 + 10LL);

void solve() {
  int n;
  sl(n);
  Vl arr(n);
  ump counter;
  int sum = 0;
  for (auto &it : arr) {
    sl(it);
    sum = sum + it;
    counter[it]++;
  }
  int q;
  sl(q);
  while (q--) {
    int a, b;
    sl(a);
    sl(b);
    int freq = counter[a];
    sum = sum - freq * a;
    counter[b] = counter[b] + freq;
    sum = sum + freq * b;
    counter[a] = 0;
    cout << sum << endl;
  }
}

signed main() {
  int test = 1;
  // sl(test);
  while (test--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define Fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define int long long
#define ld long double
#define S second
#define F first
#define mp make_pair
#define ms(x, y) memset(x, y, sizeof(x))
#define pb push_back
#define sl(n) cin >> n
#define pl(n) cout << n
#define sze(v) (int)(v.size())

typedef vector<int> Vl;
typedef pair<int, int> pii;
typedef vector<pii> Vll;
typedef vector<vector<int>> Vv;
typedef vector<pair<int, pii>> Vlll;
typedef priority_queue<int, vector<int>, greater<int>> PQL;
typedef unordered_map<int, int> ump;

const int mod = 1e9 + 7;
const int modx = 998244353;
const int Nx = 1e5 + 2;
const int inf = (int)(1e19 + 10LL);

void solve() {
  int n;
  sl(n);
  Vl arr(n);
  ump counter;
  for (auto &it : arr) {
    sl(it);
    counter[it]++;
  }
  int q;
  sl(q);
  while (q--) {
    int a, b;
    sl(a);
    sl(b);
    int freq = counter[a];
    counter[b] = counter[b] + freq;
    counter[a] = 0;
    ump dup;
    int sum = 0;
    for (auto it : counter) {
      if (it.S == 0)
        continue;
      sum = sum + it.F * it.S;
      dup[it.F] = it.S;
    }
    cout << sum << endl;
    counter = dup;
  }
}

signed main() {
  int test = 1;
  // sl(test);
  while (test--) {
    solve();
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int Q, N;
  ll sum = 0;
  cin >> N;

  vector<ll> num(110000, 0);
  for (int i = 0; i < N; i++) {
    ll A;
    cin >> A;
    sum += A;
    num[A]++;
  }

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

Error: TLE

Fixed submission:

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

int main() {
  int Q, N;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++)
    cin >> a[i];
  cin >> Q;
  vector<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])
        a[j] = c[i];
    }
    ll sum = 0;
    for (int j = 0; j < N; j++)
      sum += a[j];
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
using namespace std;
void solve() {
  ll n, q, sum = 0;
  cin >> n;
  ll arr[n + 5];
  map<ll, ll> mp;
  for (int i = 1; i <= n; ++i) {
    cin >> arr[i];
    mp[arr[i]]++;
    sum += arr[i];
  }
  //    cout<<sum<<endl;
  cin >> q;
  while (q--) {
    ll b1, c1;
    cin >> b1 >> c1;
    mp[c1] += mp[b1];
    ll extra = mp[b1] * (c1 - b1);
    mp[b1] = 0;
    sum += extra;
    cout << sum << endl;
  }
}

int main() {
  int x, t;
  solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
void solve() {
  ll n, q, sum = 0;
  cin >> n;
  ll arr[n + 5];
  for (int i = 1; i <= n; ++i) {
    cin >> arr[i];
    //        sum+=arr[i];
  }
  cin >> q;
  while (q--) {
    sum = 0;
    ll b1, c1;
    cin >> b1 >> c1;
    //        int B=arr[b1],C=arr[c1];
    for (int i = 1; i <= n; ++i) {
      if (arr[i] == b1) {
        arr[i] = c1;
      }
      sum += arr[i];
    }
    cout << sum << endl;
  }
}

int main() {
  int x, t;
  solve();
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  cin >> n;
  long sum = 0;
  vector<long> s(1000005);

  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    sum += k;
    s[k]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    sum -= (s[b] * b);
    sum += (s[b] * c);
    s[c] += s[b];
    s[b] = 0;
    cout << sum << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  int arr[n], sum = 0;
  vector<int> s;

  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    for (int j = 0; j < n; j++) {
      if (arr[j] == b) {
        arr[j] = c;
        sum = sum - b + c;
      }
    }
    s.push_back(sum);
  }
  for (int i = 0; i < s.size(); i++) {
    cout << s[i] << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll n, sum = 0;
  cin >> n;
  ll a[n + 2];
  vector<vector<ll>> vp(100005, vector<ll>());
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
    vp[a[i]].push_back(a[i]);
  }
  ll k;
  cin >> k;
  for (int i = 1; i <= k; i++) {
    ll x, y, d;
    cin >> x >> y;
    for (ll j = 0; j < vp[x].size(); j++) {
      // cout<<a[x]<<endl;
      sum -= vp[x][j];
      sum += y;
      // a[x]=y;
      vp[y].push_back(y);
    }
    // for(auto k:vp[2])cout<<k<<" ";
    // for(int i=0;i<5;i++)
    //  for(int j=0;j<4;j++)cout<<vp[i][j]<<" \\n"[j==3];
    vp[x].clear();
    cout << sum << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll n, sum = 0;
  cin >> n;
  ll a[n + 2];
  vector<vector<int>> vp(100005, vector<int>());
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
    vp[a[i]].push_back(a[i]);
  }
  ll k;
  cin >> k;
  for (int i = 1; i <= k; i++) {
    ll x, y, d;
    cin >> x >> y;
    for (ll j = 0; j < vp[x].size(); j++) {
      // cout<<a[x]<<endl;
      sum -= vp[x][j];
      sum += y;
      // a[x]=y;
      vp[y].push_back(y);
    }
    // for(auto k:vp[2])cout<<k<<" ";
    // for(int i=0;i<5;i++)
    //  for(int j=0;j<4;j++)cout<<vp[i][j]<<" \\n"[j==3];
    vp[a[x]].clear();
    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 < (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 <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 1; i <= (n); i++)
#define drep(i, n) for (int i = (n)-1; i >= 0; i--)
#define srep(i, s, t) for (int i = s; i < t; i++)
#define rng(a) a.begin(), a.end()

using namespace std;

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];
    }
    int s = 0;
    rep(j, n) s += a[j];
    cout << s << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  int N = 0;
  int Q = 0;
  cin >> N;
  vector<int> A(N + 1);

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

  vector<int> number(100001);
  for (int i = 1; i <= N; i++) {
    number.at(A.at(i))++;
  }

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

  for (int i = 1; i <= Q; i++) {
    sum += number.at(B.at(i)) * (C.at(i) - B.at(i));
    cout << sum << endl;
    number.at(C.at(i)) += number.at(B.at(i));
    number.at(B.at(i)) = 0;
  }
}


Error: TLE

Fixed submission:

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

int N = 0;
int Q = 0;

int main(void) {
  cin >> N;
  vector<int> A(N + 1);

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

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

  for (int i = 1; i <= Q; i++) {
    int rp = 0;
    for (int j = 1; j <= N; j++) {
      if (A.at(j) == B.at(i)) {
        A.at(j) = C.at(i);
        rp++;
      }
    }
    sum += rp * (C.at(i) - B.at(i));
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(obj) (obj).begin(), (obj).end()
using namespace std;
typedef long long ll;
long long GCD(long long x, long long y) { return y ? GCD(y, x % y) : x; }

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(obj) (obj).begin(), (obj).end()
using namespace std;
typedef long long ll;
long long GCD(long long x, long long y) { return y ? GCD(y, x % y) : x; }

int main() {
  vector<ll> A(100000);
  int N;
  cin >> N;
  int ans = 0;
  rep(i, N) {
    int a;
    cin >> a;
    A[a - 1]++;
  }
  int Q;
  cin >> Q;
  rep(i, Q) {
    int B, C;
    cin >> B >> C;
    A[C - 1] += A[B - 1];
    A[B - 1] = 0;
    rep(i, 100000) ans += (i + 1) * A[i];
    cout << ans << endl;
    ans = 0;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);

  int64_t s = 0;

  vector<vector<int>> v(100001, vector<int>(0));

  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    s += a.at(i);
    v.at(a.at(i)).push_back(i);
  }

  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    if (b != c) {
      int z = v.at(b).size();
      for (int j = 0; j < z; j++) {
        v.at(c).push_back(v.at(b).at(j));
      }
      for (int j = 0; j < z; j++) {
        v.at(b).pop_back();
      }

      s += (c - b) * z;
      cout << s << endl;
    }

    else
      cout << s << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);

  int64_t s = 0;

  vector<vector<int>> v(100000, vector<int>(0));

  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    s += a.at(i);
    v.at(a.at(i)).push_back(i);
  }

  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    if (b != c) {
      int z = v.at(b).size();
      for (int j = 0; j < z; j++) {
        v.at(c).push_back(v.at(b).at(j));
      }
      for (int j = 0; j < z; j++) {
        v.at(b).pop_back();
      }

      s += (c - b) * z;
      cout << s << endl;
    }

    else
      cout << s << endl;
  }
}