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 pair<ll, ll> pll;
typedef vector<ll> vll;

#define INX(x) (cin >> x)
#define OUTX(x) (cout << x << endl)

#define ADD emplace_back
#define MP make_pair
#define VVEC(type) vector<vector<type>>

#define maxlen 100001

int main() {
  ll N;
  cin >> N;

  ll arr[maxlen];
  for (ll i = 0; i < maxlen; i++) {
    arr[i] = 0;
  }

  ll S = 0;
  for (ll i = 0; i < N; i++) {
    ll tmp;
    cin >> tmp;
    arr[tmp]++;
    S += tmp;
  }

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

    S += arr[B] * (C - B);
    arr[C] += arr[B];
    arr[B] = 0;

    cout << S << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;

#define INX(x) (cin >> x)
#define OUTX(x) (cout << x << endl)

#define ADD emplace_back
#define MP make_pair
#define VVEC(type) vector<vector<type>>

#define maxlen 100001

int main() {
  ll N;
  cin >> N;

  ll arr[maxlen];
  for (ll i = 0; i < maxlen; i++) {
    arr[i] = 0;
  }

  set<ll> Adash;
  for (ll i = 0; i < N; i++) {
    ll tmp;
    cin >> tmp;
    arr[tmp]++;
    Adash.emplace(tmp);
  }

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

    arr[C] += arr[B];
    arr[B] = 0;

    Adash.erase(B);
    Adash.emplace(C);

    ll S = 0;
    for (ll i : Adash) {
      S += i * arr[i];
    }

    cout << S << endl;
  }

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

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

void solve() {
  ll n;
  cin >> n;
  unordered_map<ll, ll> umap;
  ll sum = 0;
  for (ll i = 0; i < n; ++i) {
    ll in;
    cin >> in;
    umap[in]++;
    sum += in;
  }
  // cout<<sum<<endl;

  // cout<<sum<<endl;
  ll q;
  cin >> q;
  while (q--) {
    ll x, y;
    cin >> x >> y;
    sum += y * umap[x] - umap[x] * x;
    umap[y] += umap[x];
    umap[x] = 0;
    cout << sum << endl;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int q;
  q = 1;
  // cin>>q;
  while (q--)
    solve();

  return 0;
}

Error: TLE

Fixed submission:

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

void solve() {
  ll n;
  cin >> n;
  vector<ll> vec(n);
  for (ll i = 0; i < n; ++i) {
    cin >> vec[i];
  }

  ll sum = accumulate(vec.begin(), vec.end(), 0);
  // cout<<sum<<endl;
  ll q;
  cin >> q;
  while (q--) {
    ll x, y;
    cin >> x >> y;
    for (ll i = 0; i < n; ++i) {
      if (vec[i] == x) {
        vec[i] = y;
        sum += y - x;
      }
    }
    cout << sum << endl;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int q;
  q = 1;
  // cin>>q;
  while (q--)
    solve();

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, q;
  cin >> n;
  long long arr[1000001];
  for (int i = 0; i < 1000001; i++) {
    arr[i] = 0;
  }

  long long num = 0;
  for (int i = 0; i < n; i++) {
    long long x;
    cin >> x;
    arr[x]++;
    num += x;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    long long b, c;
    cin >> b >> c;
    arr[c] = arr[b] + arr[c];
    num += (c - b) * arr[b];
    arr[b] = 0;
    cout << num << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int q;
  cin >> q;
  int b, c;
  int ans;
  for (int i = 0; i < q; i++) {

    ans = 0;
    cin >> b >> c;
    for (int j = 0; j < n; j++) {
      if (arr[j] == b) {
        arr[j] = c;
      }
      ans += arr[j];
    }
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef pair<string, string> Pstring;
typedef pair<double, double> Pdouble;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REP1(i, n) for (ll i = 1; i <= n; i++)
#define Precision13 cout << fixed << setprecision(13)
const double PI = 3.14159265358979323846;
const int MAX = 510000;
const int MOD = 1000000007;
const int INF = 1 << 29;
using Graph = vector<vector<int>>;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  map<ll, ll> MAP;

  ll sum = 0;
  REP(i, n) {
    cin >> a[i];
    MAP[a[i]]++;
    sum += a[i];
  }

  ll q;
  cin >> q;

  REP(i, q) {
    ll b, c;
    cin >> b >> c;
    sum += (c - b) * MAP[b];
    cout << sum << endl;

    MAP[c] += MAP[b];
    MAP[b] = 0;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef pair<string, string> Pstring;
typedef pair<double, double> Pdouble;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REP1(i, n) for (ll i = 1; i <= n; i++)
#define Precision13 cout << fixed << setprecision(13)
const double PI = 3.14159265358979323846;
const int MAX = 510000;
const int MOD = 1000000007;
const int INF = 1 << 29;
using Graph = vector<vector<int>>;

int main() {

  ll n;
  cin >> n;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  ll q;
  cin >> q;
  vector<ll> b(q);
  vector<ll> c(q);
  REP(i, q) cin >> b[i] >> c[i];

  ll sum = 0;
  REP(i, n) { sum += a[i]; }

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

// By Zank100
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mod 1000000007
#define din(t)                                                                 \\
  long long t;                                                                 \\
  cin >> t;
#define in(t) cin >> t;
ll mul(ll a, ll b) { return (((a % mod) * (b % mod)) % mod); }
ll modexp(ll b, ll e) {
  ll res = 1;
  for (; e; b = b * b % mod, e >>= 1)
    if (e & 1)
      res = res * b % mod;
  return res;
}
ll modinv(ll a) {
  ll x = mod - 2;
  return modexp(a, x);
}
ll power(ll x, ll y) {
  ll temp;
  if (y == 0)
    return 1;
  temp = power(x, y / 2);
  if (y % 2 == 0)
    return temp * temp;
  else {
    if (y > 0)
      return x * temp * temp;
    else
      return (temp * temp) / x;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  din(n) ll a[n];
  ll sum = 0, max = 0;
  for (ll i = 0; i < n; i++) {
    in(a[i]);
    sum += a[i];
  }
  ll hash[100001] = {0};
  for (ll i = 0; i < n; i++)
    hash[a[i]]++;
  din(q);
  vector<ll> ans;
  for (ll i = 0; i < q; i++) {
    din(b);
    din(c);
    sum -= hash[b] * b;
    sum += hash[b] * c;
    hash[c] += hash[b];
    hash[b] = 0;
    ans.push_back(sum);
  }
  for (ll i = 0; i < q; i++)
    cout << ans[i] << endl;
  return 0;
}


Error: 0

Fixed submission:

// By Zank100
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mod 1000000007
#define din(t)                                                                 \\
  long long t;                                                                 \\
  cin >> t;
#define in(t) cin >> t;
ll mul(ll a, ll b) { return (((a % mod) * (b % mod)) % mod); }
ll modexp(ll b, ll e) {
  ll res = 1;
  for (; e; b = b * b % mod, e >>= 1)
    if (e & 1)
      res = res * b % mod;
  return res;
}
ll modinv(ll a) {
  ll x = mod - 2;
  return modexp(a, x);
}
ll power(ll x, ll y) {
  ll temp;
  if (y == 0)
    return 1;
  temp = power(x, y / 2);
  if (y % 2 == 0)
    return temp * temp;
  else {
    if (y > 0)
      return x * temp * temp;
    else
      return (temp * temp) / x;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  din(n) ll a[n];
  ll sum = 0, max = 0;
  for (ll i = 0; i < n; i++) {
    in(a[i]);
    sum += a[i];
  }
  ll hash[10001] = {0};
  for (ll i = 0; i < n; i++)
    hash[a[i]]++;
  din(q);
  vector<ll> ans;
  for (ll i = 0; i < q; i++) {
    din(b);
    din(c);
    sum -= hash[b] * b;
    sum += hash[b] * c;
    hash[c] += hash[b];
    hash[b] = 0;
    ans.push_back(sum);
  }
  for (ll i = 0; i < q; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <bits/stdc++.h>

#define PI 3.14159265359
#define rep(i, a, n) for (int i = a; i < (int)n; ++i)
#define SZ(x) ((int)(x).size()) // size() unsigned -> int
#define descSort(a) sort(a.begin(), a.end(), std::greater<int>())
using namespace std;
typedef long long ll;
const ll INF = 1e9 + 7;

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


Error: TLE

Fixed submission:


#include <bits/stdc++.h>

#define PI 3.14159265359
#define rep(i, a, n) for (int i = a; i < (int)n; ++i)
#define SZ(x) ((int)(x).size()) // size() unsigned -> int
#define descSort(a) sort(a.begin(), a.end(), std::greater<int>())
using namespace std;
typedef long long ll;
const ll INF = 1e9 + 7;

int main(void) {
  int n, q;
  cin >> n;
  map<ll, ll> mp;
  rep(i, 0, n) {
    int a;
    cin >> a;
    mp[a]++;
  }
  cin >> q;
  rep(i, 0, q) {
    ll sum = 0;
    int b, c;
    cin >> b >> c;
    mp[c] += mp[b];
    mp[b] = 0;
    for (auto i : mp)
      sum += (i.first * i.second);
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
typedef long long LL;

int c[100001] = {0};

int read() {
  char c;
  while ((c = getchar()) < 48 || c > 57)
    ;
  int x = c - 48;
  while ((c = getchar()) > 47 && c < 58)
    x = x * 10 + c - 48;
  return x;
}

int main() {
  int n, T, x, y, t;
  LL s = 0;

  n = read();
  while (n--) {
    x = read();
    c[x]++, s += x;
  }

  T = read();
  while (T--) {
    x = read(), y = read();
    t = c[x], c[y] += t, c[x] = 0;
    s += (LL)(y - x) * (LL)t;
    printf("%lld\\n", s);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int a, b, c, d, e, f, h, i;
  cin >> a;
  int s[a];
  for (i = 0; i < a; i++) {
    cin >> s[i];
  }
  cin >> d;
  while (d--) {
    cin >> e >> f;
    for (i = 0; i < a; i++) {
      if (e == s[i])
        s[i] = f;
    }
    int sum = 0;
    for (i = 0; i < a; i++) {
      sum = sum + s[i];
    }

    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

ll ar[100005];
ll a, b, q, c, d;

int main() {
  scanf("%lld", &a);
  ll jum = 0;
  for (int k = 1; k <= a; k++) {
    scanf("%lld", &b);
    ar[b]++;
    jum += b;
  }
  scanf("%lld", &q);
  for (int k = 1; k <= q; k++) {
    scanf("%lld %lld", &c, &d);
    if (ar[c] != 0) {
      jum += ar[c] * (d - c);
      ar[d] += ar[c];
      ar[c] = 0;
    }
    printf("%lld\\n", jum);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main() {
  ios_base::sync_with_stdio();
  cin.tie(0);
  cout.tie(0);
  ll n, m, a, b;
  cin >> n;
  ll x[n];
  for (ll i = 0; i < n; i++) {
    cin >> x[i];
  }
  cin >> m;
  for (ll i = 0; i < m; i++) {
    cin >> a >> b;
    ll sum = 0;
    for (ll j = 0; j < n; j++) {
      if (x[j] == a) {
        x[j] = b;
      }
      sum += x[j];
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
int num[10000000];

int main() {
  int n;
  scanf("%d", &n);
  int max = INT_MIN;
  for (int i = 1; i <= n; i++) {
    int temp;
    scanf("%d", &temp);
    num[temp]++;
    max = std::max(max, temp);
  }
  int t;
  scanf("%d", &t);

  unsigned long long count = 0;
  for (int i = 1; i <= max; i++) {
    count += i * num[i];
  }
  for (int i = 1; i <= t; i++) {
    int b, c;
    scanf("%d %d", &b, &c);
    if (num[b]) {
      count += (c - b) * num[b];
      num[c] += num[b];
      num[b] = 0;
    }
    printf("%llu\\n", count);
  }
}


Error: TLE

Fixed submission:

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

#define LL long long
LL n, a[100100], b[100100], c[100100], tot = 0, q, x;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> b[i] >> c[i];
  }
  for (int i = 1; i <= q; i++) {
    for (int j = 1; j <= n; j++) {
      if (a[j] == b[i]) {
        a[j] = c[i];
      }
      tot += a[j];
    }
    cout << tot << endl;
    tot = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <memory.h>
using namespace std;
int a[1000001];
int main() {
  memset(a, 0, sizeof(a));
  int n;
  cin >> n;
  long long int count = 0;
  for (int i = 0; i < n; i++) {
    int b;
    cin >> b;
    count += b;
    a[b]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int m, n;
    cin >> m >> n;
    a[n] += a[m];
    int c = a[m];
    a[m] = 0;
    count = count + c * (n - m);
    cout << count << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <memory.h>
using namespace std;
int a[10001];
int main() {
  memset(a, 0, sizeof(a));
  int n;
  cin >> n;
  long long int count = 0;
  for (int i = 0; i < n; i++) {
    int b;
    cin >> b;
    count += b;
    a[b]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int m, n;
    cin >> m >> n;
    a[n] += a[m];
    int c = a[m];
    a[m] = 0;
    count = count + c * (n - m);
    cout << count << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
typedef long long ll;
using namespace std;
const int N = 100010;
int a[N];
int main() {
  int n;
  cin >> n;
  int x;
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    sum += x;
    a[x]++;
  }

  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * 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;
int a[100001];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  cin >> k;
  while (k--) {
    int p, q;
    cin >> p >> q;
    int sum = 0;
    for (int i = 0; i < n; ++i) {
      if (a[i] == p)
        a[i] = q;
      sum += a[i];
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>

using namespace std;
const int MAXN = 2e5 + 5;

int n, q, a[MAXN], cnt[MAXN];
long long sum;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i), cnt[a[i]]++, sum += a[i];
  }
  scanf("%d", &q);
  for (int b, c, i = 1; i <= q; i++) {
    scanf("%d%d", &b, &c);
    sum = sum + (long long)cnt[b] * (c - b);
    cnt[c] += cnt[b];
    cnt[b] = 0;
    printf("%lld\\n", sum);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;

  vector<int> cnt(10010);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    cnt[a]++;
    sum += a;
  }

  int q, b, c;

  cin >> q;
  while (q--) {
    cin >> b >> c;

    sum += (c - b) * cnt[b];
    cnt[c] += cnt[b];
    cnt[b] = 0;

    cout << sum << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

using ll = long long;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;

void setIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

// ctrl q is comment
// ctrl b is run
// ctrl s is compile
// shift r is search & replace
// shift d is delete a line
int main() {
  setIO();
  int n;
  cin >> n;
  int a[n];
  ll sum = 0;
  int count[100001];
  fill(count, count + 100001, 0);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    sum += a[i];
    ++count[a[i]];
  }
  int q;
  cin >> q;
  while (q--) {
    int ai, bi;
    cin >> ai >> bi;
    sum += ((ll)count[ai]) * (bi - ai);
    count[bi] += count[ai];
    count[ai] = 0;
    cout << sum << "\\n";
  }
}


Error: 0

Fixed submission:

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

using ll = long long;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;

void setIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

// ctrl q is comment
// ctrl b is run
// ctrl s is compile
// shift r is search & replace
// shift d is delete a line

bool comp(pii a, pii b) { return a.x == b.x ? a.y < b.y : a.x < b.x; }
int main() {
  setIO();
  int n;
  cin >> n;
  int a[n];
  ll sum = 0;
  int count[n];
  fill(count, count + n, 0);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    sum += a[i];
    ++count[a[i]];
  }
  int q;
  cin >> q;
  while (q--) {
    int ai, bi;
    cin >> ai >> bi;
    sum += count[ai] * (bi - ai);
    count[bi] += count[ai];
    count[ai] = 0;
    cout << sum << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> item(10e5 + 1);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    item[a[i]]++;
  }

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

  long long sum = 0;
  for (int i = 0; i < n; i++)
    sum += a[i];

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

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

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;
  vector<int> b(q), c(q), s(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];
      s[i] += a[j];
    }
  }

  for (int i = 0; i < q; i++)
    cout << s[i] << 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;
  map<int, int> mp;
  ll suma = 0;
  for (int i = 0; i < n; i++) {
    int cur;
    cin >> cur;
    mp[cur]++;
    suma += cur;
  }
  int q;
  cin >> q;
  while (q--) {
    ll b, c;
    cin >> b >> c;
    ll antes_b = mp[b];
    mp[b] = 0;
    mp[c] += antes_b;
    suma -= antes_b * b;
    suma += antes_b * c;
    cout << suma << '\\n';
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
  int n;
  cin >> n;
  unordered_multiset<int> mst;
  ll suma = 0;
  for (int i = 0; i < n; i++) {
    int cur;
    cin >> cur;
    mst.insert(cur);
    suma += cur;
  }
  int q;
  cin >> q;
  while (q--) {
    ll b, c;
    cin >> b >> c;
    ll antes_b = mst.count(b);
    mst.erase(b);
    for (int i = 0; i < antes_b; i++)
      mst.insert(c);
    suma -= antes_b * b;
    suma += antes_b * c;
    cout << suma << '\\n';
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int N, Q;
  long sum = 0;
  vector<long> num(110000, 0);
  long a, b, c;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a;
    sum += a;
    num[a]++;
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> b >> c;
    sum += (c - b) * num[b];
    num[c] += num[b];
    num[b] = 0;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int N, Q, B, C;
  vector<int> A;
  vector<pair<int, int>> BC;
  vector<int> res;
  cin >> N;
  A.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> B >> C;
    pair<int, int> bc = make_pair(B, C);
    BC.emplace_back(bc);
  }
  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < N; j++) {
      if (A[j] == BC[i].first)
        A[j] = BC[i].second;
    }
    res.emplace_back(accumulate(A.begin(), A.end(), 0));
  }
  for (int i = 0; i < Q; i++) {
    cout << res[i] << endl;
    ;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  long int N, Q;
  int *A;
  int A_count[100001] = {0};
  int B, C;
  long int s = 0;
  int diff;

  cin >> N;
  A = new int[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    s += A[i];
  }
  for (int i = 0; i < N; i++)
    A_count[A[i]] += 1;

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

    cin >> B >> C;
    diff = C - B;

    s += diff * A_count[B];
    A_count[C] += A_count[B];
    A_count[B] = 0;
    cout << s << '\\n';
  }

  delete[] A;
}

Error: TLE

Fixed submission:

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

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

  long int N, Q;
  int *A;
  int B, C, s = 0;
  int diff;
  int left, right, mid;
  int j;

  cin >> N;
  A = new int[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    s += A[i];
  }

  cin >> Q;
  for (int i = 0; i < Q; i++) {
    sort(A, A + N);
    cin >> B >> C;
    diff = C - B;

    left = -1;
    right = N;
    while (abs(right - left) > 1) {
      mid = (right + left) / 2;
      if (A[mid] == B) {
        j = 1;
        while (mid + j < N) {
          if (A[mid + j] != B)
            break;
          A[mid + j] = C;
          s += diff;
          j++;
        }
        j = 1;
        while (mid - j >= 0) {
          if (A[mid - j] != B)
            break;
          A[mid - j] = C;
          s += diff;
          j++;
        }
        A[mid] = C;
        s += diff;
      } else if (A[mid] < B)
        left = mid;
      else if (A[mid] > B)
        right = mid;
    }
    cout << s << '\\n';
  }

  delete[] A;
}