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

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

int main() {
  map<long long, long long> a;
  rep(i, 100001) { a[i] = 0; }
  long long n;
  cin >> n;
  rep(i, n) {
    long long temp;
    cin >> temp;
    a[temp]++;
  }
  long long q;
  cin >> q;
  vector<long long> s;
  long long ans = 0;
  rep(i, q) {
    long long b, c;
    cin >> b >> c;
    ans -= b * a[b];
    ans += c * a[b];
    a[c] = a[b] + a[c];
    a[b] = 0;
    if (i == 0) {
      ans = 0;
      rep(j, 100001) {
        long long t;
        t = a[j] * j;
        ans += t;
      }
    }
    s.push_back(ans);
  }
  rep(i, q) { cout << s.at(i) << endl; }
}

Error: TLE

Fixed submission:

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

int main() {
  map<long long, long long> a;
  rep(i, 10001) { a[i] = 0; }
  long long n;
  cin >> n;
  rep(i, n) {
    long long temp;
    cin >> temp;
    a[temp]++;
  }
  long long q;
  cin >> q;
  vector<long long> s;
  long long ans;
  rep(i, q) {
    long long b, c;
    cin >> b >> c;
    a[c] = a[b] + a[c];
    a[b] = 0;
    ans = 0;
    rep(j, 10001) {
      long long t;
      t = a[j] * j;
      ans += t;
    }
    s.push_back(ans);
  }
  rep(i, q) { cout << s.at(i) << endl; }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define pii pair<int, int>
#define vi vector<int>
#define vii vector<std::vector<int>> v;
#define mem(dp, a) memset(dp, a, sizeof dp)
#define sonic                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb(x) push_back(x)
#define mkpr(x, y) make_pair(x, y)
#define itr(it, c) for (auto const &it : c)
#define all(c) c.begin(), c.end()
#define ff first
#define ss second
#define INF INT_MAX
#define REPl(i, a, b) for (ll i = a; i < b; i++)
#define REPi(i, a, b) for (ll i = a; i < b; i++)

ll cnt[100005];

//*******User-Defined Functions*********//

//*******Functions end here************//
//*******Main func starts**************//
int main() {
  sonic;
  ll n, k;
  vector<ll> v;
  cin >> n;
  v.resize(n + 1, 0);
  ll preSum = 0;
  for (ll i = 1, x; i <= n; i++) {
    cin >> x;
    v[i] = x;
    preSum += x;
    cnt[x]++;
  }
  cin >> k;
  for (ll i = 1, x, y; i <= k; i++) {
    cin >> x >> y;
    preSum -= cnt[x] * x;
    preSum += cnt[x] * y;
    cnt[y] += cnt[x];
    cnt[x] = 0;
    cout << preSum << endl;
  }
}
//******Main Ends here****************//

Error: TLE

Fixed submission:

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

#define pii pair<int, int>
#define vi vector<int>
#define vii vector<std::vector<int>> v;
#define mem(dp, a) memset(dp, a, sizeof dp)
#define sonic                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb(x) push_back(x)
#define mkpr(x, y) make_pair(x, y)
#define itr(it, c) for (auto const &it : c)
#define all(c) c.begin(), c.end()
#define ff first
#define ss second
#define INF INT_MAX
#define REPl(i, a, b) for (ll i = a; i < b; i++)
#define REPi(i, a, b) for (ll i = a; i < b; i++)
//*******User-Defined Functions*********//

//*******Functions end here************//
//*******Main func starts**************//
int main() {
  sonic;
  int n, k;
  vi v;
  cin >> n;
  v.resize(n + 1, 0);
  int preSum = 0;
  for (int i = 1, x; i <= n; i++) {
    cin >> x;
    v[i] = x;
    preSum += x;
  }
  cin >> k;
  int cnt;
  for (int i = 1, x, y; i <= k; i++) {
    cnt = 0;
    cin >> x >> y;
    for (int j = 1; j <= n; j++) {
      if (v[j] == x) {
        v[j] = y;
        cnt++;
      }
    }
    if (x > y)
      preSum -= abs(x - y) * cnt;
    else
      preSum += abs(x - y) * cnt;
    cout << preSum << endl;
  }
}
//******Main Ends here****************//
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;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<int> a(n), cnt(100010);
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
    ++cnt[a[i]];
  }
  int q;
  cin >> q;
  vector<ll> ans(q);
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * cnt[b];
    cnt[c] += cnt[b];
    cnt[b] = 0;
    ans[i] = sum;
  }
  for (ll x : ans)
    cout << x << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int N, Q, i, sum = 0;
int A[100010];
pair<int, int> a[100010];

int main() {
  cin >> N;
  for (i = 1; i <= N; i++)
    scanf("%d", &A[i]);
  cin >> Q;
  sort(A + 1, A + N + 1);
  for (i = 1; i <= Q; i++) {
    cin >> a[i].first;
    cin >> a[i].second;
  }
  for (int j = 1; j <= Q; j++) {
    for (i = 1; i <= N; i++) {
      if (A[i] == a[j].first) {
        A[i] = a[j].second;
      }
      sum += A[i];
    }
    cout << sum << endl;
    sum = 0;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

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

Error: 0

Fixed submission:

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

int main() {
  int n;
  long long s = 0;
  vector<long long> a(10005);
  cin >> n;
  for (int i = 0; i < 10005; i++)
    a[i] = 0;
  for (int i = 0; i < n; i++) {
    long long t;
    cin >> t;
    a[t]++;
    s += t;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    long long b, c;
    cin >> b >> c;
    a[c] += a[b];
    s -= (a[b] * b);
    s += (c * a[b]);
    a[b] = 0;
    cout << s << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

const ll INF = 1e18;
const ll MOD = 1000000007;
const ll MX = 0; // 最大値

ll dp[MX][MX];
inline void chmax(ll &a, ll b) { a = max(a, b); }

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((ll)(x).size()) // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))
#define PB push_back

int main() {
  ll n, q;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);
  cin >> q;
  vector<ll> b(q), c(q);
  rep(i, q) cin >> b.at(i) >> c.at(i);
  ll cnt = 0;
  rep(i, n) cnt += a.at(i);

  vector<ll> set(100005, 0);
  rep(i, n) { set.at(a.at(i))++; }

  rep(i, q) {
    ll tmp = set.at(b.at(i));
    set.at(c.at(i)) += tmp;
    cnt += (c.at(i) - b.at(i)) * tmp;
    set.at(b.at(i)) = 0;
    cout << cnt << endl;
  }
}

Error: 0

Fixed submission:

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

const ll INF = 1e18;
const ll MOD = 1000000007;
const ll MX = 0; // 最大値

ll dp[MX][MX];
inline void chmax(ll &a, ll b) { a = max(a, b); }

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((ll)(x).size()) // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))
#define PB push_back

int main() {
  ll n, q;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);
  cin >> q;
  vector<ll> b(q), c(q);
  rep(i, q) cin >> b.at(i) >> c.at(i);
  ll cnt = 0;
  rep(i, n) cnt += a.at(i);

  vector<ll> set(10005, 0);
  rep(i, n) { set.at(a.at(i))++; }

  rep(i, q) {
    ll tmp = set.at(b.at(i));
    set.at(c.at(i)) += tmp;
    cnt += (c.at(i) - b.at(i)) * tmp;
    cout << cnt << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

int main() {

  long long N = 1, Q;
  cin >> N;

  vector<long long> v(100001);
  v.resize(100001);

  for (long long i = 0; i < N; i++) {
    int a;
    cin >> a;
    v[a]++;
  }
  cin >> Q;

  long long sum = 0;
  for (int i = 1; i < 100001; i++) {
    sum += i * v[i];
  }

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

    sum -= v[b] * b;
    sum += v[b] * c;
    v[c] += v[b];
    v[b] = 0;
    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {

  long long N = 1, Q;
  cin >> N;

  vector<long long> v(100001);
  v.resize(100001);

  for (long long i = 0; i < N; i++) {
    int a;
    cin >> a;
    v[a]++;
  }
  cin >> Q;

  auto Output = [&]() {
    long long sum = 0;
    for (int i = 1; i < 100001; i++) {
      sum += i * v[i];
    }

    cout << sum << endl;
  };

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

    v[c] += v[b];
    v[b] = 0;
    Output();
  }

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

#include <bits/stdc++.h>

#define F first
#define S second
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define in(x) insert(x)
#define forr(i, a, b) for (int i = a; i <= b; i++)
#define sz(s) (int)(s.size())
#define all(x) x.begin(), x.end()
#define did_you_calis_enough                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define file(s)                                                                \\
  if (fopen(s ".in", "r"))                                                     \\
  freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)
#define int long long

using namespace std;

const int INF = 4294967295;
const int N = 2e6 + 667;
const int mod = 1e9 + 7;
const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
const int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
const double PI = acos(-1.0);

map<int, int> m;

void input(int &n, int &sum) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    m[t]++;
    sum += t;
  }
}

main() {
  did_you_calis_enough int n, sum = 0, q;
  input(n, sum);
  cin >> q;
  while (q--) {
    int p, x;
    cin >> p >> x;
    sum -= m[p] * p;
    sum += m[p] * x;
    m[x] += m[p];
    m[p] = 0;
    cout << sum << '\\n';
  }
  return 0;
}

// Coded by LoliThief

Error: TLE

Fixed submission:

#include <stdio.h>
int main() {
  long long int N, i;
  scanf("%lld", &N);
  int a[N];
  for (i = 0; i < N; i++) {
    scanf("%lld", &a[i]);
  }

  long long int K, m, n, sum = 0;
  scanf("%lld", &K);
  while (K--) {
    sum = 0;
    scanf("%lld%lld", &m, &n);
    for (i = 0; i < N; i++) {
      if (m == a[i]) {
        a[i] = n;
      }
    }
    for (i = 0; i < N; i++) {
      sum = sum + a[i];
    }
    printf("%lld\\n", sum);
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define speed                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define int long long

using namespace std;

const int N = 1e3 + 7;
const int mod = 1e9 + 7;

int32_t main() {
  speed;
  int n;
  cin >> n;
  int sum = 0;
  map<int, int> mp;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    sum += x;
    mp[x]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    int x = mp[b];
    sum -= x * b;
    sum += x * c;
    mp[b] = 0;
    mp[c] += x;
    cout << sum << '\\n';
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, q, i, b, c, j, sum = 0;
  scanf("%lld", &n);
  long long int a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  cin >> q;
  long long int res[q];
  for (i = 0; i < q; i++) {
    cin >> b >> c;
    for (j = 0; j < n; j++) {
      if (a[j] == b) {
        sum = ((sum - b) + c);
        a[j] = c;
      }
    }
    res[i] = sum;
  }
  for (i = 0; i < q; i++)
    cout << res[i] << endl;
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long int sum, a, n, q, b, c;
map<long long int, int> mmp;
int main() {
  scanf("%lld", &n);
  while (n--) {
    scanf("%lld", &a);
    sum += a;
    if (!mmp.count(a))
      mmp[a] = 1;
    else
      mmp[a]++;
  }
  scanf("%lld", &q);
  while (q--) {
    scanf("%lld%lld", &b, &c);
    if (mmp.count(b)) {
      int tmp = mmp[b];
      sum -= b * tmp;
      sum += c * tmp;
      if (!mmp.count(c))
        mmp[c] = 0;
      mmp[c] += tmp;
      mmp[b] = 0;
    }
    printf("%lld\\n", sum);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int res[10005] = {0};
int N, d = 0;
long long sum = 0;
int main() {

  while (scanf("%d", &N)) {
    sum = 0;
    memset(res, 0, sizeof(res));
    for (int i = 0; i < N; i++) {
      scanf("%d", &d);
      res[d]++;
      sum += d;
    }
    int Q;
    scanf("%d", &Q);
    int y = 0, x = 0;
    for (int i = 0; i < Q; i++) {
      scanf("%d %d", &y, &x);
      sum += (x - y) * res[y];
      res[x] += res[y];
      res[y] = 0;
      cout << sum << endl;
    }
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
const int SIZE = 1000005;

using namespace std;
int n, q, b, c;
int a[1000005];
long long int ans, ji;

int main() {
  cin >> n;
  ans = 0;
  memset(a, 0, sizeof(a));
  for (int i = 0; i < n; i++) {
    cin >> ji;
    ans += ji;
    a[ji]++;
  }

  cin >> q;
  while (q--) {
    cin >> b >> c;
    int j = c - b;
    ans += (j * a[b]);
    a[c] += a[b];
    a[b] = 0;

    cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int a[10010];
int cont[10010] = {};
int main() {
  int n, q, x, y;
  scanf("%d", &n);
  int total, temp = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cont[a[i]]++;
    temp += a[i];
  }
  scanf("%d", &q);
  while (q--) {
    total = temp;
    scanf("%d %d", &x, &y);
    total += (cont[x]) * (y - x);
    cont[y] += cont[x];
    cont[x] = 0;
    cout << total << endl;
    temp = total;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

/*
 * a.cpp
 *
 *  Created on: 2019/06/08
 *      Author: MEIP-users
 */

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define MOD (long long int)(1e9 + 7)
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPS(i, n) for (int i = n; i > 0; i--)
#define INF (int)(1e9 + 10)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))

int main() {
  ll n;
  cin >> n;
  vector<ll> as(n);
  vector<ll> p(100000, 0);
  ll ans = 0;
  rep(i, n) {
    cin >> as[i];
    p[as[i] - 1]++;
    ans += as[i];
  }
  ll q;
  cin >> q;
  vector<ll> anss;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    ans -= p[b - 1] * b;
    ans += p[b - 1] * c;
    p[c - 1] += p[b - 1];
    p[b - 1] = 0;
    anss.push_back(ans);
  }
  rep(i, q) { cout << anss[i] << endl; }
  return 0;
}


Error: 0

Fixed submission:

/*
 * a.cpp
 *
 *  Created on: 2019/06/08
 *      Author: MEIP-users
 */

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define MOD (long long int)(1e9 + 7)
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPS(i, n) for (int i = n; i > 0; i--)
#define INF (int)(1e9 + 10)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))

int main() {
  int n;
  cin >> n;
  vector<int> as(n);
  vector<int> p(n, 0);
  ll ans = 0;
  rep(i, n) {
    cin >> as[i];
    p[as[i] - 1]++;
    ans += as[i];
  }
  int q;
  cin >> q;
  vector<ll> anss;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    ans -= p[b - 1] * b;
    ans += p[b - 1] * c;
    p[c - 1] += p[b - 1];
    p[b - 1] = 0;
    anss.push_back(ans);
  }
  rep(i, q) { cout << anss[i] << 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;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }
  int Q;
  cin >> Q;
  vector<int> b(Q), c(Q);
  for (int i = 0; i < Q; i++) {
    cin >> b.at(i) >> c.at(i);
  }

  map<int, long long> ansA;
  for (int i = 0; i < N; i++) {
    if (ansA.count(a.at(i))) {
      ansA[a.at(i)]++;
    } else {
      ansA[a.at(i)] = 1;
    }
  }

  long long sum = 0;
  for (int i = 1; i < 100001; i++) {
    if (ansA.count(i)) {
      sum += i * ansA.at(i);
    }
  }

  for (int i = 0; i < Q; i++) {
    if (ansA.count(b.at(i))) {
      if (ansA.count(c.at(i))) {
        sum = sum - b.at(i) * ansA[b.at(i)] + c.at(i) * ansA[b.at(i)];

        ansA[c.at(i)] += ansA.at(b.at(i));
        ansA.erase(b.at(i));
      } else {
        sum = sum - b.at(i) * ansA[b.at(i)] + c.at(i) * ansA[b.at(i)];

        ansA[c.at(i)] = ansA.at(b.at(i));
        ansA.erase(b.at(i));
      }
    }

    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.at(i);
  }
  int Q;
  cin >> Q;
  vector<int> b(Q), c(Q);
  for (int i = 0; i < Q; i++) {
    cin >> b.at(i) >> c.at(i);
  }

  map<int, int> ansA;
  for (int i = 0; i < N; i++) {
    if (ansA.count(a.at(i))) {
      ansA[a.at(i)]++;
    } else {
      ansA[a.at(i)] = 1;
    }
  }

  for (int i = 0; i < Q; i++) {
    if (ansA.count(b.at(i))) {
      if (ansA.count(c.at(i))) {
        ansA[c.at(i)] += ansA.at(b.at(i));
        ansA.erase(b.at(i));
      } else {
        ansA[c.at(i)] = ansA.at(b.at(i));
        ansA.erase(b.at(i));
      }
    }

    long long sum = 0;
    long long tmp;
    for (int i = 1; i < 100001; i++) {
      if (ansA.count(i)) {
        sum += i * ansA.at(i);
      }
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using P = pair<int, int>;
int main() {
  int n;
  const ll t = 200000;
  cin >> n;
  ll sum = 0;
  vector<ll> a(t);
  rep(i, n) {
    ll l;
    cin >> l;
    a[l]++;
    sum += l;
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
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;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    rep(j, n) {
      if (a[j] == b)
        a[j] = c;
    }
    int s = 0;
    rep(j, n) s += a[j];
    cout << s << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  cin >> q;
  while (q--) {
    // s=0;
    int h{0};
    cin >> b >> c;

    h = d[b];
    d[b] = 0;
    d[c] += h;
    s += h * (c - b);
    // cout<<h<<" "<<c<<" "<<b<<endl;

    cout << s << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

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

  int n;
  cin >> n;
  int a[n];
  f(i, 0, n) { cin >> a[i]; }
  int q, b, c;

  cin >> q;
  while (q--) {
    ll s{0};
    cin >> b >> c;
    f(i, 0, n) {
      if (a[i] == b) {
        a[i] = c;
      }
      s += a[i];
    }
    cout << s << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi = 3.141592653589793;
typedef unsigned long long ull;
typedef long double ldouble;
const ll INF = 1e18;

int main() {
  ull n, sum = 0, q;
  cin >> n;
  vector<ull> a(100001);
  fill(a.begin(), a.end(), 0);
  for (int i = 0; i < n; i++) {
    int y;
    cin >> y;
    sum += y;
    a.at(y)++;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    ull count = a.at(b);
    a.at(b) = 0;
    sum += (c - b) * count;
    a.at(c) += count;
    cout << sum << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi = 3.141592653589793;
typedef unsigned long long ull;
typedef long double ldouble;
const ll INF = 1e18;

int main() {
  ull n, sum = 0, q;
  cin >> n;
  vector<ull> a(100000);
  fill(a.begin(), a.end(), 0);
  for (int i = 0; i < n; i++) {
    int y;
    cin >> y;
    sum += y;
    a.at(y)++;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    ull count = a.at(b);
    a.at(b) = 0;
    sum += (c - b) * count;
    a.at(c) += count;
    cout << sum << endl;
  }
}