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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, j, n) for (ll i = j; i < n; i++)
#define all(vec) vec.begin(), vec.end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1LL << 60;

int main() {
  int n;
  cin >> n;
  map<int, int> m;
  rep(i, n) {
    int a;
    cin >> a;
    if (!m.count(a))
      m[a] = 1;
    else
      m[a]++;
  }
  ll sum = 0;
  for (auto x : m)
    sum += x.first * x.second;
  int q;
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    if (m.count(b)) {
      sum -= b * m[b];
      sum += c * m[b];
      if (!m.count(c))
        m[c] = 0;
      m[c] += m[b];
      m.erase(b);
    }
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, j, n) for (ll i = j; i < n; i++)
#define all(vec) vec.begin(), vec.end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1LL << 60;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  ll sum = 0;
  rep(i, n) sum += a[i];
  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    sort(all(a));
    int lb, rb;
    int l = -1, r = n;
    while (r - l > 1) {
      int m = l + (r - l) / 2;
      if (a[m] < b)
        l = m;
      else
        r = m;
    }
    lb = l + 1;
    l = -1, r = n;
    while (r - l > 1) {
      int m = l + (r - l) / 2;
      if (b < a[m])
        r = m;
      else
        l = m;
    }
    rb = r;
    Rep(i, lb, rb) a[i] = c;
    sum += (c - b) * (rb - lb);
    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 int;

const int MOD_NUM = 1e9 + 7;

int main() {
  // Input
  int N, Q;
  vector<int> A, B, C;

  cin >> N;
  A = vector<int>(N);
  rep(i, N) cin >> A[i];
  cin >> Q;
  B = vector<int>(Q);
  C = vector<int>(Q);
  rep(i, Q) cin >> B[i] >> C[i];

  // Process
  vector<ll> S(Q);

  ll s_0 = accumulate(A.begin(), A.end(), 0LL);

  vector<int> a_count(1e5 + 1);
  rep(i, N) a_count[A[i]]++;

  rep(i, Q) {
    int b_count = a_count[B[i]];
    a_count[B[i]] = 0;
    a_count[C[i]] += b_count;

    S[i] = (i == 0) ? s_0 : S[i - 1];
    S[i] += (C[i] - B[i]) * b_count;
  }

  // Output
  rep(i, Q) cout << S[i] << 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 int;

const int MOD_NUM = 1e9 + 7;

ll sum_by_a_count(const vector<int> &a_count) {
  ll ans = 0;
  rep(i, 1e5 + 1) ans += a_count[i] * i;

  return ans;
}

int main() {
  // Input
  int N, Q;
  vector<int> A, B, C;

  cin >> N;
  A = vector<int>(N);
  rep(i, N) cin >> A[i];
  cin >> Q;
  B = vector<int>(Q);
  C = vector<int>(Q);
  rep(i, Q) cin >> B[i] >> C[i];

  // Process
  vector<ll> S(Q);

  vector<int> a_count(1e5 + 1);
  rep(i, N) a_count[A[i]]++;

  rep(i, Q) {
    int b = a_count[B[i]];
    a_count[B[i]] = 0;
    a_count[C[i]] += b;

    S[i] = sum_by_a_count(a_count);
  }

  // Output
  rep(i, Q) cout << S[i] << endl;

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

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

#define FOR(i, N) for (int i = 0; i < N; i++)

int main() {
  long long H, W, Q, K, N, x, max = 0, sum = 0;
  cin >> N;
  vector<long> A(N);
  vector<long> cnt(100000, 0);
  FOR(i, N) {
    cin >> A[i];
    cnt[A[i] - 1]++;
    sum += A[i];
  }
  cin >> Q;
  vector<long> B(Q), C(Q);
  FOR(i, Q) cin >> B[i] >> C[i];

  FOR(i, Q) {
    x = cnt[B[i] - 1];
    if (x != 0) {
      cnt[B[i] - 1] = 0;
      cnt[C[i] - 1] += x;
      sum += x * (C[i] - B[i]);
    }
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

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

#define FOR(N) for (int i = 0; i < N; i++)
#define FORj(N) for (int j = 0; j < N; j++)

int main() {
  long long H, W, Q, K, N, x, max = 0, sum = 0;
  cin >> N;
  vector<long> A(N);
  FOR(N) cin >> A[i];
  cin >> Q;
  vector<long> B(Q), C(Q);
  FOR(Q) cin >> B[i] >> C[i];

  FOR(Q) {
    FORj(N) {
      if (A[j] == B[i]) {
        A[j] = C[i];
      }
    }
    FORj(N) { sum += A[j]; }
    cout << sum << endl;
    sum = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> cnt(100001, 0);
  long long ans = 0;
  rep(i, n) {
    cin >> a[i];
    ans += a[i];
    cnt[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) {
    int d = c[i] - b[i];
    ans += d * cnt[b[i]];
    cout << ans << endl;
    cnt[c[i]] += cnt[b[i]];
    cnt[b[i]] = 0;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> cnt(n + 1, 0);
  long long ans = 0;
  rep(i, n) {
    cin >> a[i];
    ans += a[i];
    cnt[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) {
    int d = c[i] - b[i];
    ans += d * cnt[b[i]];
    cout << ans << endl;
    cnt[c[i]] += cnt[b[i]];
    cnt[b[i]] = 0;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  map<long long, long long> maps;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (maps.count(a[i]))
      maps[a[i]] += 1;
    else
      maps[a[i]] = 1;
  }

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

  int q;
  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 j = 0; j < n; j++) {
    sum += a[j];
  }

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


Error: TLE

Fixed submission:

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

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

  int q;
  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 j = 0; j < n; j++)
    sum += a[j];

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef set<int> si;
typedef set<ld> sd;
typedef set<ll> sl;
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define Plabs                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
int main() {
  Plabs;
  int n, q;
  cin >> n;
  map<int, int> m;
  int a;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a;
    sum = sum + a;
    m[a]++;
  }

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef set<int> si;
typedef set<ld> sd;
typedef set<ll> sl;
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define Plabs                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
int main() {
  Plabs;
  ll n;
  cin >> n;
  ll sum = 0;
  ll ar[n];
  for (ll i = 0; i < n; i++) {
    cin >> ar[i];
    sum = sum + ar[i];
  }
  ll q;
  cin >> q;
  ll a, b;
  while (q--) {
    cin >> a >> b;
    ll c = count(ar, ar + n, a);
    replace(ar, ar + n, a, b);
    sum = sum + (c * (b - a));
    cout << sum << '\\n';
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  int N, Q;
  scanf("%d\\n", &N);
  vector<int> count(1000010, 0);
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    int x;
    scanf("%d", &x);
    ans += x;
    count[x]++;
  }

  scanf("\\n%d\\n", &Q);
  for (int i = 0; i < Q; i++) {

    int b, c;
    scanf("%d%d\\n", &b, &c);
    int y = count[b];
    count[c] += y;
    count[b] = 0;

    ans += (c - b) * y;
    printf("%lld\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {

  int N, Q;
  scanf("%d\\n", &N);
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    scanf("%d", &A[i]);

  scanf("\\n%d\\n", &Q);
  for (int i = 0; i < Q; i++) {
    long long ans = 0;

    int b, c;
    scanf("%d%d\\n", &b, &c);
    for (int j = 0; j < N; j++) {
      if (A[j] == b)
        A[j] = c;
      ans += A[j];
    }
    printf("%lld\\n", ans);
  }

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

/* D - Replacing */

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

int main() {
  int N, A, Q, B, C;
  long long S = 0;
  vector<int> num(100001);
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A;
    num.at(A)++;
    S += A;
  }
  cin >> Q;
  while (Q-- > 0) {
    cin >> B >> C;
    S += (C - B) * num.at(B);
    num.at(C) += num.at(B);
    num.at(B) = 0;
    cout << S << endl;
  }
}


Error: TLE

Fixed submission:

/* D - Replacing */

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

int main() {
  int N, Q, B, C;
  long long S = 0;
  vector<int> A(100001);
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    S += A.at(i);
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int cnt = 0;
    cin >> B >> C;
    for (int j = 0; j < N; j++) {
      if (A.at(j) == B) {
        cnt++;
        A.at(j) = C;
      }
    }
    // cout << cnt << endl;
    S += (C - B) * cnt;
    cout << S << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define for_int(i, a, b) for (int i = (a); i <= (b); ++i)
#define rep_int(i, a, b) for (int i = (a); i >= (b); --i)
#define for_ll(i, a, b) for (ll i = (a); i <= (b); ++i)
#define rep_ll(i, a, b) for (ll i = (a); i >= (b); --i)
#define ull unsigned long long
#define ll long long
#define db double
#define HPMAX 1005
#define KMP_MAX 1005
#define prime_MAX 1e6 + 5
#define quick_pow_mod 1e9 + 7
#define PII pair<int, int>
using namespace std;
// 快读
int read() {
  char c = getchar();
  int x = 0, f = 1;
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - 48;
  return x * f;
}
const int N = 1e5 + 5;
int an[N];
ll ans = 0;
int n, q, a, b, c;
int main() {
  n = read();
  for_int(i, 1, n) {
    a = read();
    an[a]++;
    ans += a;
  }
  q = read();
  while (q--) {
    b = read();
    c = read();
    ans -= (ll)b * an[b];
    ans += (ll)c * an[b];
    cout << ans << endl;
    an[c] += an[b];
    an[b] = 0;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define for_int(i, a, b) for (int i = (a); i <= (b); ++i)
#define rep_int(i, a, b) for (int i = (a); i >= (b); --i)
#define for_ll(i, a, b) for (ll i = (a); i <= (b); ++i)
#define rep_ll(i, a, b) for (ll i = (a); i >= (b); --i)
#define ull unsigned long long
#define ll long long
#define db double
#define HPMAX 1005
#define KMP_MAX 1005
#define prime_MAX 1e6 + 5
#define quick_pow_mod 1e9 + 7
#define PII pair<int, int>
using namespace std;
// 快读
int read() {
  char c = getchar();
  int x = 0, f = 1;
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - 48;
  return x * f;
}
const int N = 1e5 + 5;
vector<vector<int>> an(N);
ll ans = 0;
int n, q, a, b, c;
int main() {
  n = read();
  for_int(i, 1, n) {
    a = read();
    if (an[a].size() == 0)
      an[a].push_back(1);
    else
      an[a][0] += 1;
    ans += a;
  }
  q = read();
  while (q--) {
    b = read();
    c = read();
    ans -= an[b][0] * b;
    ans += an[b][0] * c;
    cout << ans << endl;
    if (an[c].size() == 0)
      an[c].push_back(an[b][0]);
    else
      an[c][0] += an[b][0];
    an[b].clear();
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

using namespace std;
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <cmath>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <vector>
#define rip(i, n) for (ll i = 0; i < n; i++)
#define Rip(i, n) for (int i = 1; i < n; i++)
#define all(V) V.begin(), V.end()
#define ll long long
#define ld long double
#define MOD 1000000007
#define mod 998244353
#define sec setprecision
#define _CRT_SECURE_NO_WARNINGS
#pragma target("avx")
#pragma optimize("O3")
#pragma optimize("unroll-loops")
constexpr double eps = 1e-9;
constexpr double pi = 3.141592653589793238462643383279;
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll pow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return pow(x * x, n / 2);
  else
    return x * pow(x, n - 1);
}
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rip(i, n) cin >> a[i];
  int q;
  cin >> q;
  vector<int> b(q);
  vector<int> c(q);
  rip(i, q) cin >> b[i] >> c[i];
  sort(all(a));
  ll sum = 0;
  rip(i, n) sum += a[i];
  vector<int> kosuu(100001, 0);
  rip(i, n) kosuu[a[i]]++;
  rip(i, q) {
    int sa = c[i] - b[i];
    sum += kosuu[b[i]] * sa;
    kosuu[c[i]] += kosuu[b[i]];
    kosuu[b[i]] = 0;
    cout << sum << endl;
  }
}

Error: 0

Fixed submission:

using namespace std;
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <cmath>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <vector>
#define rip(i, n) for (ll i = 0; i < n; i++)
#define Rip(i, n) for (int i = 1; i < n; i++)
#define all(V) V.begin(), V.end()
#define ll long long
#define ld long double
#define MOD 1000000007
#define mod 998244353
#define sec setprecision
#define _CRT_SECURE_NO_WARNINGS
#pragma target("avx")
#pragma optimize("O3")
#pragma optimize("unroll-loops")
constexpr double eps = 1e-9;
constexpr double pi = 3.141592653589793238462643383279;
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll pow(ll x, ll n, ll m) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return pow(x * x % m, n / 2, m);
  else
    return x * pow(x, n - 1, m) % m;
}
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rip(i, n) cin >> a[i];
  int q;
  cin >> q;
  vector<int> b(n);
  vector<int> c(n);
  rip(i, q) cin >> b[i] >> c[i];
  sort(all(a));
  ll sum = 0;
  rip(i, n) sum += a[i];
  vector<int> kosuu(100001, 0);
  rip(i, n) kosuu[a[i]]++;
  rip(i, q) {
    int sa = c[i] - b[i];
    sum += kosuu[b[i]] * sa;
    kosuu[c[i]] += kosuu[b[i]];
    kosuu[b[i]] = 0;
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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


Error: TLE

Fixed submission:

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

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

int main() {
  int a, b, c, n, q;
  long long sum = 0;
  cin >> n;
  vector<int> cnt(100001);
  for (int i = 0; i < n; i++) {
    cin >> a;
    cnt[a]++;
    sum += a;
  }
  cin >> q;
  long long *adSum;
  adSum = new long long[q];
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    adSum[i] = ((long long)c - b) * cnt[b];
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }

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

Error: TLE

Fixed submission:

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

int replace(vector<int> &a, int b, int c) {
  int sum = 0;
  for (int i = 0; i < (int)a.size(); i++) {
    if (a[i] == b)
      a[i] = c;
    sum += a[i];
  }
  return sum;
}

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

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

#include <bits/stdc++.h>
#define LL long long
#define MOD 1000000007
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
using namespace std;

int main() {
  fastio;
  int n;
  cin >> n;
  LL a[n], sum = 0;
  int count[100000 + 7];

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

  int q;
  cin >> q;
  while (q--) {

    LL b, c;
    cin >> b >> c;
    if (count[b] > 0) {
      sum = sum - (b * count[b]) + (c * count[b]);
      count[c] += count[b];
      count[b] = 0;
    }
    cout << sum << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
#define MOD 1000000007
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
using namespace std;

int main() {
  fastio;
  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, sum = 0;
    cin >> b >> c;
    for (int i = 0; i < n; i++) {
      if (a[i] == b)
        a[i] = c;
      sum += a[i];
    }
    cout << sum << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
typedef long long int lli;
#define pb push_back

int main() {
  int n;
  cin >> n;
  std::vector<long long int> v(n + 1);

  for (long long int i = 1; i <= n; ++i) {
    /* code */
    cin >> v[i];
  }

  long long int initSum = 0;

  for (long long int i = 1; i <= n; ++i) {
    /* code */
    initSum = initSum + v[i];
  }
  unordered_map<long long int, long long int> mp;

  for (long long int i = 1; i <= n; ++i) {
    /* code */
    mp[v[i]]++;
  }

  long long int q;

  cin >> q;

  for (long long int k = 1; k <= q; ++k) {
    long long int b, c;
    cin >> b >> c;
    if (mp[b] != 0) {
      long long int sum = initSum - (mp[b] * b) + (mp[b] * c);

      mp[c] = mp[c] + mp[b];
      mp[b] = 0;
      initSum = sum;
    }

    cout << initSum << endl;
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long int lli;
#define pb push_back

int main() {
  int n;
  cin >> n;
  std::vector<int> v(n + 1);

  for (int i = 1; i <= n; ++i) {
    /* code */
    cin >> v[i];
  }

  int q;

  cin >> q;

  for (int k = 1; k <= q; ++k) {
    int b, c;
    cin >> b >> c;
    int sum = 0;
    for (int i = 1; i <= n; ++i) {
      /* code */
      if (v[i] == b) {
        v[i] = c;
      }
    }

    for (int i = 1; i <= n; ++i) {
      /* code */
      sum = sum + v[i];
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  // ios_base::sync_with_stdio(0), cin.tie(nullptr);
  long long n;
  cin >> n;
  long long a[n];
  long long sum = 0;
  map<long long, long long> cnt;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    cnt[a[i]]++;
  }
  long long q;
  cin >> q;
  while (q--) {
    long long b, c;
    cin >> b >> c;
    long long x = cnt[b];
    cnt[b] = 0;
    cnt[c] += x;
    sum += (x * c) - (x * b);
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a, b, c;
int x[100100], y[100100], z[100100];
int main() {
  cin >> a;
  for (int i = 0; i < a; i++)
    cin >> x[i];
  cin >> b;
  for (int i = 0; i < b; i++)
    cin >> y[i] >> z[i];
  for (int i = 0; i < b; i++) {
    int n = 0;
    for (int j = 0; j < a; j++) {
      if (x[j] == y[i])
        x[j] = z[i];
      n += x[j];
    }
    cout << n << endl;
  }
  return 0;
}