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

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  int N, Q, M = 0;
  cin >> N;
  map<int, int> A;
  for (int i = 0; i < N; i++) {
    int j;
    cin >> j;
    A[j]++;
    M += j;
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    M = M - A[B] * (B - C);
    A[C] += A[B];
    A.erase(B);
    cout << M << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  int N, Q;
  cin >> N;
  map<int, int> A;
  for (int i = 0; i < N; i++) {
    int j;
    cin >> j;
    A[j]++;
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    A[C] += A[B];
    A.erase(B);
    int k = 0;
    for (pair<int, int> j : A)
      k += j.first * j.second;
    cout << k << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  cin >> n;
  int keep[100005] = {0};

  long long sum(0);

  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    sum += (long long)x;
    keep[x]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    sum -= (b * keep[b]);
    sum += (c * keep[b]);
    keep[c] += keep[b];
    keep[b] = 0;
    cout << sum << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  int lastindex = 0;
  int keep[100001] = {0};

  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x > lastindex)
      lastindex = x;
    keep[x]++;
  }

  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    if (c > lastindex)
      lastindex = c;
    keep[c] += keep[b];
    keep[b] = 0;
    int sum = 0;
    for (int i = 1; i <= lastindex; i++) {
      sum += (i * keep[i]);
    }
    cout << sum << "\\n";
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
const int INF = 1000000009;
const ll LINF = 1e18;

int main() {
  int n;
  cin >> n;
  vector<int> a_kind(100001, 0);
  ll sum = 0;
  for (int i = 0; i < n; ++i) {
    int inp;
    cin >> inp;
    ++a_kind[inp];
    sum += inp;
  }
  int q;
  cin >> q;
  vector<ll> ans(q);
  for (int i = 0; i < q; ++i) {
    int b, c;
    cin >> b >> c;
    sum += (ll)(-1 * (ll)a_kind[b] * (ll)b) + (ll)((ll)a_kind[b] * (ll)c);
    a_kind[c] += a_kind[b];
    a_kind[b] = 0;
    ans[i] = sum;
  }
  for (int i = 0; i < q; ++i)
    cout << ans[i] << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
const int INF = 1000000009;
const ll LINF = 1e18;

int main() {
  int n;
  cin >> n;
  vector<int> a_kind(n + 1, 0);
  ll sum = 0;
  for (int i = 0; i < n; ++i) {
    int inp;
    cin >> inp;
    ++a_kind[inp];
    sum += inp;
  }
  int q;
  cin >> q;
  vector<ll> ans(q);
  for (int i = 0; i < q; ++i) {
    int b, c;
    cin >> b >> c;
    sum += (ll)(-1 * (ll)a_kind[b] * (ll)b) + (ll)((ll)a_kind[b] * (ll)c);
    a_kind[c] += a_kind[b];
    a_kind[b] = 0;
    ans[i] = sum;
  }
  for (int i = 0; i < q; ++i)
    cout << ans[i] << 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);
  vector<int> anum(100000);
  unsigned long long int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ans += a[i];
    anum[a[i]]++;
  }
  int q;
  cin >> q;
  vector<int> b(q);
  vector<int> c(q);

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

  for (int i = 0; i < q; i++) {
    ans += anum[b[i]] * (c[i] - b[i]);

    anum[c[i]] += anum[b[i]];
    anum[b[i]] = 0;
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

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

#include <iostream>
#define int long long

using namespace std;

signed main(void) {
  // Your code here!
  int n;
  int a[100001];
  int q;
  int b[100001];
  int c[100001];
  int num[100001];

  int sum[100001];

  for (int i = 0; i < 100001; i++) {
    sum[i] = 0;
    num[i] = 0;
  }
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum[0] += a[i];
  }

  for (int i = 0; i < n; i++) {
    num[a[i]]++;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> b[i] >> c[i];
  }

  // cout << b[0] << " " << c[0];
  for (int j = 0; j < q; j++) {
    int tmp = 0;
    tmp = c[j] - b[j];
    sum[j + 1] = sum[j] + tmp * num[b[j]];
    num[c[j]] += num[b[j]];
    num[b[j]] = 0;
  }

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


Error: TLE

Fixed submission:

#include <iostream>
#define int long long

using namespace std;

signed main(void) {
  // Your code here!
  int n;
  int a[100001];
  int q;
  int b[100001];
  int c[100001];

  int sum[100001];

  for (int i = 0; i < 100001; i++) {
    sum[i] = 0;
  }
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> b[i] >> c[i];
  }

  // cout << b[0] << " " << c[0];
  for (int j = 0; j < q; j++) {
    for (int i = 0; i < n; i++) {
      if (a[i] == b[j]) {
        a[i] = c[j];
      }
      sum[j] += a[i];
    }
  }

  for (int i = 0; i < q; i++) {
    cout << sum[i] << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n;
  cin >> n;
  ll a[n];
  ll sum = 0;
  map<ll, ll> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
    sum += a[i];
  }
  ll q;
  cin >> q;
  while (q--) {
    ll b, c;
    cin >> b >> c;
    ll count = m[b];
    sum = sum - b * count + c * count;
    m[b] = 0;
    m[c] += count;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    for (int i = 0; i < n; i++) {
      if (a[i] == b) {
        a[i] = c;
      }
    }
    cout << accumulate(a, a + n, 0) << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long int
#define N 100000
#define M 1000000007
#define f(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define rf(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define po pop_back
#define pb push_back
#define lb lower_bound
#define fi first
#define se second
#define mp make_pair
#define bs binary_search
#define debug(x) cout << #x << " = " << x << endl
#define ub upper_bound
#define ibs ios_base::sync_with_stdio(false)
#define cti cin.tie(0)
#define all(x) x.begin(), x.end()
#define PI 3.14159265
#define cot cout.tie(0)
using namespace std;
/**
 * @author :: Rishabh
 *
 */
int main() {
  ibs;
  cti;
  ll n;
  cin >> n;
  ll a[n];
  ll c[N + 1] = {0};
  ll m = 0;
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    ans = ans + a[i];
    c[a[i]] = c[a[i]] + 1;
    if (a[i] > m)
      m = a[i];
  }
  sort(a, a + n);
  ll q;
  cin >> q;
  while (q--) {
    ll p, b;
    cin >> p >> b;
    c[b] = c[b] + c[p];
    ans = ans + ((b - p) * c[p]);
    c[p] = 0;
    // for(ll i=1;i<=N;i++)
    // {
    // 	//cout<<c[i]<<" ";
    // 	ans=ans+(c[i]*i);
    // }
    cout << ans << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define N 100000
#define M 1000000007
#define f(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define rf(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define po pop_back
#define pb push_back
#define lb lower_bound
#define fi first
#define se second
#define mp make_pair
#define bs binary_search
#define debug(x) cout << #x << " = " << x << endl
#define ub upper_bound
#define ibs ios_base::sync_with_stdio(false)
#define cti cin.tie(0)
#define all(x) x.begin(), x.end()
#define PI 3.14159265
#define cot cout.tie(0)
using namespace std;
/**
 * @author :: Rishabh
 *
 */
int main() {
  ibs;
  cti;
  ll n;
  cin >> n;
  ll a[n];
  ll c[N + 1] = {0};
  ll m = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    c[a[i]] = c[a[i]] + 1;
    if (a[i] > m)
      m = a[i];
  }
  sort(a, a + n);
  ll q;
  cin >> q;
  while (q--) {
    ll p, b;
    cin >> p >> b;
    c[b] = c[b] + c[p];
    c[p] = 0;
    ll ans = 0;
    for (ll i = 1; i <= N; i++) {
      // cout<<c[i]<<" ";
      ans = ans + (c[i] * i);
    }
    cout << ans << "\\n";
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

using ll = long long;
const int INF = 1001001001;
const ll LLINF = 1001001001001001001;
const int MOD = 1000000007;

#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  ll n, q;
  cin >> n;
  vector<ll> man(100005, 0);
  set<ll> s;
  rep(i, n) {
    ll a;
    cin >> a;
    man[a]++;
  }

  ll result = 0;
  rep(i, 100001) { result += i * man[i]; }

  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    ll tmp = man[b];
    man[b] = 0;
    man[c] += tmp;

    result -= b * tmp;
    result += c * tmp;

    cout << result << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

using ll = long long;
const int INF = 1001001001;
const ll LLINF = 1001001001001001001;
const int MOD = 1000000007;

#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  ll n, q;
  cin >> n;
  vector<ll> man(100005, 0);
  set<ll> s;
  rep(i, n) {
    ll a;
    cin >> a;
    man[a]++;
    s.insert(a);
  }
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    ll tmp = man[b];
    man[b] = 0;
    man[c] += tmp;
    s.erase(b);
    s.insert(c);

    ll result = 0;
    for (auto x : s) {
      result += x * man[x];
    }
    cout << result << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <istream>
#include <map>
#include <math.h>
#include <set>
#include <sstream>
#include <string>
#include <vector>
// cout << fixed << setprecision(20) << *** << endl;
#define rep(i, N) for (int i = 0; i < (int)N; ++i)
typedef long long ll;
using namespace std;
int main() {
  ll N, Q, t, u, ANS = 0;
  map<ll, ll> mp;
  cin >> N;
  rep(i, N) {
    cin >> t;
    ANS = ANS + t;
    mp[t]++;
  }
  cin >> Q;
  rep(i, Q) {
    cin >> t >> u;
    ANS = ANS + mp[t] * (u - t);
    mp[u] = mp[u] + mp[t];
    mp[t] = 0;
    cout << ANS << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <istream>
#include <map>
#include <math.h>
#include <set>
#include <sstream>
#include <string>
#include <vector>
// cout << fixed << setprecision(20) << *** << endl;
#define rep(i, N) for (int i = 0; i < (int)N; ++i)
typedef long long ll;
using namespace std;
int main() {
  ll N, Q, t, u, ANS = 0;
  vector<pair<ll, ll>> A;
  cin >> N;
  rep(i, N) {
    cin >> t;
    ANS = ANS + t;
    A.push_back(make_pair(t, 1));
  }
  cin >> Q;
  rep(i, Q) {
    cin >> t >> u;
    rep(j, A.size()) {
      if (A[j].first == t) {
        ANS = ANS + A[j].second * (u - t);
        A[j].first = u;
      }
    }
    cout << ANS << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
using namespace std;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ull unsigned long long
#define int long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define endl "\\n"
#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))
#define MAX 1e18
#define PI 3.14159265
const int mod = 1e9 + 7;

int power(int x, int y) {
  int res = 1;

  while (y > 0) {

    if (y & 1)
      res = (long long)(res * x);

    y = y >> 1;

    if (x <= 100000000)
      x = (long long)(x * x);

    // cout<<x<<'\\n';
  }
  return res;
}

void solve() {
  int n;
  cin >> n;

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

  unordered_map<int, int> m;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    m[a[i]]++;
    sum += a[i];
  }

  int q;
  cin >> q;

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

int32_t main() {
  IOS;
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
using namespace std;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ull unsigned long long
#define int long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define endl "\\n"
#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))
#define MAX 1e18
#define PI 3.14159265
const int mod = 1e9 + 7;

int power(int x, int y) {
  int res = 1;

  while (y > 0) {

    if (y & 1)
      res = (long long)(res * x);

    y = y >> 1;

    if (x <= 100000000)
      x = (long long)(x * x);

    // cout<<x<<'\\n';
  }
  return res;
}

void solve() {
  int n;
  cin >> n;

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

  map<int, int> m;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    m[a[i]]++;
    sum += a[i];
  }

  int b[n];
  int c[n];
  int q;
  cin >> q;

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

  for (int i = 0; i < q; i++) {
    sum += (-b[i] * m[b[i]] + c[i] * m[b[i]]);
    cout << sum << endl;
    m[c[i]] += m[b[i]];
    m[b[i]] = 0;
  }
  return;
}

int32_t main() {
  IOS;
  solve();
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int main(void) {
  int N;
  multiset<int> A;
  cin >> N;
  long long sum = 0;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    A.insert(a);
    sum += a;
  }

  vector<int> L(100000, -1);

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

    if (L[B - 1] == -1) {
      L[B - 1] = A.erase(B);
    }
    int tmp = L[B - 1];
    if (L[C - 1] == -1) {
      L[C - 1] = A.count(C);
    }
    L[C - 1] += L[B - 1];
    L[B - 1] = 0;

    sum = sum + tmp * (C - B);
    cout << sum << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

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

int main(void) {
  int N;
  multiset<int> A;
  cin >> N;
  long long sum = 0;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    A.insert(a);
    sum += a;
  }

  vector<int> L(10000, -1);

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

    if (L[B - 1] == -1) {
      L[B - 1] = A.erase(B);
    }
    L[C - 1] = L[B - 1];
    L[B - 1] = 0;

    sum = sum + L[C - 1] * (C - B);
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> p(100001);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    p[a[i] - 1]++;
    sum += a[i];
  }
  long long q;
  cin >> q;
  vector<pair<long long, long long>> b(q);
  for (int i = 0; i < q; i++)
    cin >> b[i].first >> b[i].second;
  for (int i = 0; i < q; i++) {
    sum -= p[b[i].first - 1] * b[i].first;
    long long q = p[b[i].first - 1];
    p[b[i].first - 1] = 0;
    p[b[i].second - 1] += q;
    sum += b[i].second * q;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  ll n, q;
  cin >> n;
  ll a[100002];
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    sum += a[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    for (int i = 0; i < n; i++) {
      if (a[i] == b) {
        if (b > c)
          sum -= a[i] - c;
        else
          sum += c - a[i];
        a[i] = c;
      }
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

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

  vector<ll> D(100005);
  for (ll i = 0; i <= 100004; i++)
    D[i] = 0;
  for (ll i = 0; i < N; i++)
    D[A[i]] += 1;
  ll sum = 0;
  for (ll i = 0; i < N; i++)
    sum += A[i];
  for (ll q = 0; q < Q; q++) {
    sum -= B[q] * D[B[q]];
    sum += C[q] * D[B[q]];
    D[C[q]] += D[B[q]];
    D[B[q]] = 0;
    cout << sum << endl;
  }
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

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

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

  vector<int> D(100005);
  for (int i = 1; i <= 100000; i++)
    D[i] = 0;
  for (int i = 0; i < N; i++)
    D[A[i]] += 1;
  ll sum = 0;
  for (ll i = 0; i < N; i++)
    sum += A[i];
  for (int q = 0; q < Q; q++) {
    sum -= B[q] * D[B[q]];
    sum += C[q] * D[B[q]];
    D[C[q]] += D[B[q]];
    D[B[q]] = 0;
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

typedef long long ll;
typedef long double ld;
#define mod 1000000007
#define INF 1000000000
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  vector<ll> a(100005);
  int n;
  cin >> n;
  ll sum = 0;
  for (int i = 0; i < n; ++i) {
    int k;
    cin >> k;
    sum += (ll)k;
    ++a[k];
  }
  int q;
  cin >> q;
  while (q--) {
    ll b, c;
    cin >> b >> c;
    sum -= (a[b] * b);
    sum += (a[b] * c);
    a[c] += a[b];
    a[b] = 0;
    cout << sum << '\\n';
  }

  return 0;
}


Error: TLE

Fixed submission:

#define mod 1000000007
#define INF 1000000000
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  long long n, q;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  cin >> q;
  vector<long long> b(q);
  vector<long long> c(q);
  for (int i = 0; i < q; ++i) {
    cin >> b[i] >> c[i];
  }
  long long sum = 0;
  for (int i = 0; i < q; ++i) {
    for (int j = 0; j < n; ++j) {
      if (b[i] == a[j]) {
        a[j] = c[i];
      }
      sum += a[j];
    }
    cout << sum << '\\n';
    sum = 0;
  }

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

#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e5 + 7;

#include <math.h>
#define PI 3.14159265358979323846264338327950L

int main() {
  int n;
  cin >> n;
  vector<int> a(MOD, 0);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    int n1;
    cin >> n1;
    a[n1]++;
    sum += n1;
  }

  int Q;
  cin >> Q;
  while (Q--) {
    int x, y;
    cin >> x >> y;
    long long total = (long long)a[x] * x;
    sum -= total;
    sum += (long long)a[x] * y;
    a[y] += a[x];
    a[x] = 0;
    cout << sum << "\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e5 + 7;

#include <math.h>
#define PI 3.14159265358979323846264338327950L

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;
  while (Q--) {
    int x, y;
    cin >> x >> y;
    sort(a.begin(), a.end());
    int l = 0, h = n - 1;
    int ans = 0;
    while (l <= h) {
      int m = l + (h - l) / 2;
      if (a[m] == x) {
        ans = m;
        h = m - 1;
      } else if (a[m] > x) {
        h = m - 1;
      } else {
        l = m + 1;
      }
    }
    while (ans < n && a[ans] == x) {
      sum -= x;
      sum += y;
      a[ans] = y;
      ans++;
    }
    cout << sum << "\\n";
  }

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

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

int main() {
  ll N, Q, sum = 0, cnt[100001] = {};
  cin >> N;
  rep(i, N) {
    int a;
    cin >> a;
    cnt[a]++;
    sum += a;
  }
  cin >> Q;
  rep(i, Q) {
    ll b, c;
    cin >> b >> c;
    sum += cnt[b] * (c - b);
    cnt[c] += cnt[b];
    cnt[b] = 0;
    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  ll N, Q, count = 0;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A[i];
  cin >> Q;
  vector<int> B(Q), C(Q), S(Q + 1);
  rep(i, Q) cin >> B[i] >> C[i];
  S[0] = accumulate(A.begin(), A.end(), 0);
  rep(i, Q) {
    count = 0;
    rep(j, N) {
      if (A[j] == B[i]) {
        A[j] = C[i];
        count++;
      }
    }
    S[i + 1] = S[i] + (C[i] - B[i]) * count;
    cout << S[i + 1] << endl;
  }
  // for(int i=1;i<Q+1;i++) cout << S[i] << endl;

  return 0;
}