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

#include <bits/stdc++.h>
#include <string.h>
using namespace std;
typedef long long int lli;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  lli n, a[100005] = {0}, i, sum = 0;
  cin >> n;
  lli z[n];
  for (i = 0; i < n; i++) {
    cin >> z[i];
    sum = sum + z[i];
    a[z[i]]++;
  }
  lli q;
  cin >> q;
  while (q--) {
    lli b, c;
    cin >> b >> c;
    sum = sum - (a[b] * b);
    sum = sum + (a[b] * c);
    a[c] = a[c] + a[b];
    a[b] = 0;
    cout << sum << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <string.h>
using namespace std;
typedef long long int lli;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  lli n;
  cin >> n;
  lli a[n], i;
  for (i = 0; i < n; i++)
    cin >> a[i];
  lli q;
  cin >> q;
  while (q--) {
    lli b, c, sum = 0;
    cin >> b >> c;
    for (i = 0; i < n; i++) {
      if (a[i] == b) {
        a[i] = c;
        sum = sum + a[i];
      } else {
        sum = sum + a[i];
      }
    }
    cout << sum << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define REP(i, a, n) for (ll i = a; i < (n); i++)
using namespace std;
ll n, q, a[1000010], num = 0, sum[1000010] = {0};
int main() {
  cin >> n;
  REP(i, 0, n) {
    cin >> a[i];
    sum[a[i]]++;
    num += a[i];
  }
  cin >> q;
  REP(z, 0, q) {
    ll x, y, ans = 0;
    cin >> x >> y;
    ans = sum[x] * (y - x);
    cout << num + ans << endl;
    num += ans;
    sum[y] += sum[x];
    sum[x] = 0;
  } /**/
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define REP(i, a, n) for (ll i = a; i < (n); i++)
#define RE return
#define FILL(a, b) memset(a, b, sizeof(a))
#define SO(a) sort(all(a))
#define pb push_back
#define sz(a) a.size()
#define V vector
#define ld long double
#define viit(a) vector<int>::iterator a
#define IT iterater
#define FOR(i, a, n) for (ll i = a; i <= (n); i++)
#define B(s) s.back()
#define all(a) a.begin(), a.end()
#define ER1(a) a.erase(a.begin())
#define ER0(a) a.erase(a.end())
#define pii pair<int, int>
#define pause system("PAUSE")
#define cls system("CLS")
using namespace std;
int main() {
  // ios::sync_with_stdio(0);
  // freopen("C:/Users/LENOVO/Desktop/文件操作/in.txt","r",stdin);
  // freopen("C:/Users/LENOVO/Desktop/文件操作/out.txt","w",stdout);
  ll a[100010], n, q;
  cin >> n;
  REP(i, 0, n)
  cin >> a[i];
  cin >> q;
  REP(z, 0, q) {
    int x, y;
    cin >> x >> y;
    int sum = 0;
    REP(i, 0, n) {
      if (a[i] == x)
        a[i] = y;
      sum += a[i];
    }
    cout << sum << endl;
  }
  // fclose(stdin);
  // fclose(stdout);
  return 0;
}
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;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  map<int, int> mp;
  ll ans = 0;
  rep(i, n) {
    int a;
    cin >> a;
    if (mp.count(a) == 0)
      mp[a] = 1;
    else
      mp[a]++;
    ans += a;
  }
  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    int cnt;
    if (mp.count(b) != 0)
      cnt = mp[b];
    else
      cnt = 0;
    mp[b] = 0;
    if (mp.count(c) == 0)
      mp[c] = cnt;
    else
      mp[c] += cnt;
    ans += cnt * (c - b);
    cout << ans << endl;
  }
}


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;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  multiset<int> ms;
  ll ans = 0;
  rep(i, n) {
    int a;
    cin >> a;
    ms.insert(a);
    ans += a;
  }
  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    int cnt = ms.count(b);
    ms.erase(b);
    rep(i, cnt) ms.insert(c);
    ans += cnt * (c - b);
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  vector<int64_t> A(N);
  vector<int64_t> A_tot(100001, 0);
  int64_t tot = 0;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    tot += A.at(i);
    A_tot.at(A.at(i))++;
  }

  int Q;
  cin >> Q;
  int64_t B, C;
  for (int i = 0; i < Q; i++) {
    cin >> B >> C;
    int64_t cnt = A_tot.at(B);
    tot += cnt * (C - B);
    A_tot.at(C) += cnt;
    A_tot.at(B) = 0;
    cout << tot << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int64_t> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int Q;
  cin >> Q;
  int B, C;
  for (int i = 0; i < Q; i++) {
    cin >> B >> C;
    replace(A.begin(), A.end(), B, C);
    cout << accumulate(A.begin(), A.end(), 0) << 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 < (int)(n); i++)

int main() {
  int n, q, b, c, af;
  long sum = 0;
  cin >> n;
  vector<int> a(n);
  vector<int> num(100001);
  rep(i, n) {
    cin >> a.at(i);
    af = a.at(i);
    sum += a.at(i);
    num.at(af - 1) = num.at(af - 1) + 1;
  }
  cin >> q;
  rep(i, q) {
    cin >> b >> c;
    sum = sum + (c - b) * num.at(b - 1);
    cout << sum << endl;
    num.at(c - 1) += num.at(b - 1);
    num.at(b - 1) = 0;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int n, q, b, c;
  int sum = 0;
  cin >> n;
  vector<int> a(n);
  rep(i, n) {
    cin >> a.at(i);
    sum += a.at(i);
  }
  vector<int> num(100000);
  rep(i, 100000) {
    rep(k, n) {
      if ((a.at(k) - 1) == i) {
        num.at(i) = num.at(i) + 1;
      }
    }
  }
  cin >> q;
  rep(i, q) {
    cin >> b >> c;
    sum = sum + (c - b) * num.at(b - 1);
    cout << sum << endl;
    num.at(c - 1) += num.at(b - 1);
    num.at(b - 1) = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#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);
  for (int i = 0; i < Q; ++i) {
    cin >> B[i] >> C[i];
  }

  long long sum = 0;
  vector<long long> cnt(100005, 0);
  for (int i = 0; i < N; ++i) {
    cnt[A[i]]++;
    sum += A[i];
  }

  for (int i = 0; i < Q; ++i) {
    long long old_num = cnt[B[i]];
    long long minus = (long long)B[i] * (long long)old_num;
    long long plus = (long long)C[i] * (long long)old_num;
    cnt[B[i]] = 0;
    cnt[C[i]] += (long long)old_num;

    sum = sum - minus + plus;
    cout << sum << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#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);
  for (int i = 0; i < Q; ++i) {
    cin >> B[i] >> C[i];
  }

  long long sum = 0;
  vector<long long> cnt(10005, 0);
  for (int i = 0; i < N; ++i) {
    cnt[A[i]]++;
    sum += A[i];
  }

  for (int i = 0; i < Q; ++i) {
    long long old_num = cnt[B[i]];
    long long minus = (long long)B[i] * (long long)old_num;
    long long plus = (long long)C[i] * (long long)old_num;
    cnt[B[i]] = 0;
    cnt[C[i]] += (long long)old_num;

    sum = sum - minus + plus;
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll N, Q, s = 0;
  cin >> N;
  map<ll, ll> m;

  rep(i, N) {
    ll t;
    cin >> t;
    if (m.count(t))
      ++m.at(t);
    else
      m[t] = 1;
    s += t;
  }

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

    if (m.count(B)) {
      s = s - B * m.at(B) + C * m.at(B);
      if (m.count(C))
        m.at(C) += m.at(B);
      else
        m[C] = m.at(B);
      m.erase(B);
    }
    cout << s << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  ll N, Q, s = 0;
  cin >> N;
  map<ll, ll> m;

  rep(i, N) {
    ll t;
    cin >> t;
    if (m.count(t))
      ++m.at(t);
    else
      m[t] = 1;
    s += t;
  }

  cin >> Q;
  rep(i, Q) {
    ll B, C;
    cin >> B >> C;
    s = s - B * m.at(B) + C * m.at(B);
    if (m.count(C))
      m.at(C) += m.at(B);
    else
      m[C] = m.at(B);
    m.erase(B);
    cout << s << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <iostream>  // cout, endl, cin
#include <math.h>    // sqrt
#include <string>    // string, to_string, stoi
#include <vector>    // vector
using namespace std;

int main() {
  long long N, Q;
  cin >> N;
  vector<long long> data(N);
  for (int i = 0; i < N; i++) {
    cin >> data.at(i);
  }
  cin >> Q;
  vector<vector<long long>> act(Q, vector<long long>(2));
  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < 2; j++) {
      cin >> act.at(i).at(j);
    }
  }
  long long ans = 0;
  sort(data.begin(), data.end());
  vector<long long> bit(100001, 0);
  for (long long x : data) {
    ans += x;
    bit.at(x)++;
  }
  long long add;
  int sum;
  for (int i = 0; i < Q; i++) {
    add = act.at(i).at(1) - act.at(i).at(0);
    ans += bit.at(act.at(i).at(0)) * add;
    bit.at(act.at(i).at(1)) += bit.at(act.at(i).at(0));
    bit.at(act.at(i).at(0)) = 0;
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <iostream>  // cout, endl, cin
#include <math.h>    // sqrt
#include <string>    // string, to_string, stoi
#include <vector>    // vector
using namespace std;

int main() {
  long long N, Q;
  cin >> N;
  vector<long long> data(N);
  for (int i = 0; i < N; i++) {
    cin >> data.at(i);
  }
  cin >> Q;
  vector<vector<long long>> act(Q, vector<long long>(2));
  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < 2; j++) {
      cin >> act.at(i).at(j);
    }
  }
  long long ans = 0;
  for (long long x : data) {
    ans += x;
  }
  long long add;
  int sum;
  for (int i = 0; i < Q; i++) {
    add = act.at(i).at(1) - act.at(i).at(0);
    sum = 0;
    for (int j = 0; j < N; j++) {
      if (data.at(j) == act.at(i).at(0)) {
        sum++;
        data.at(j) = act.at(i).at(1);
      }
    }
    ans += add * sum;
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int value[100010];
int main() {
  long long sum = 0;
  int n;
  int v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> v;
    value[v]++;
    sum += v;
  }
  int q;
  cin >> q;
  int b, c, diff, cnt;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    diff = c - b;
    cnt = value[b];
    value[b] = 0;
    value[c] += cnt;
    sum += cnt * diff;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int value[100010];
int main() {
  long long sum = 0;
  int n;
  int v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> v;
    value[i] = v;
    sum += v;
  }
  int q;
  cin >> q;
  int b, c, diff, cnt;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    diff = c - b;
    cnt = 0;
    for (int j = 0; j < n; j++) {
      if (value[j] == b) {
        value[j] = c;
        cnt++;
      }
    }
    sum += cnt * diff;
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define loop(i, a, n) for (ll i = (a); i < (n); i++)
#ifdef _DEBUG
#define dd(x) cout << #x << " : " << x << endl
#else
#define dd(x)
#endif
using namespace std;
using ll = int64_t;
using P = pair<ll, ll>;

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

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define loop(i, a, n) for (ll i = (a); i < (n); i++)
#ifdef _DEBUG
#define dd(x) cout << #x << " : " << x << endl
#else
#define dd(x)
#endif
using namespace std;
using ll = int64_t;
using P = pair<ll, ll>;

int main() {
  ll n;
  cin >> n;
  map<ll, ll> a;
  rep(i, n) {
    ll tmp;
    cin >> tmp;
    a[tmp]++;
  }
  ll q;
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    a[c] += a[b];
    a[b] = 0;
    ll s = 0;
    for (auto p : a) {
      s += p.first * p.second;
    }
    cout << s << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
using namespace std;
const ll M = 1e9 + 7;
ll n, ans;
void solve() {
  ll n;
  cin >> n;
  ll arr[n];
  ll s = 0;
  map<ll, ll> f;
  for (ll i = 0; i < n; i++)
    cin >> arr[i], f[arr[i]]++, s = s + arr[i];
  ll k;
  cin >> k;
  for (ll i = 0; i < k; i++) {
    ll a, b;
    cin >> a >> b;
    s = s - f[a] * a;
    s = s + f[a] * b;
    f[b] = f[b] + f[a];
    f[a] = 0;
    cout << s << endl;
  }
}
int main() {
  int t;
  t = 1;
  while (t--) {
    solve();
  }
}

Error: TLE

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
using namespace std;
const ll M = 1e9 + 7;
ll n, ans;
void solve() {
  int n;
  cin >> n;
  int arr[n];
  int s = 0;
  for (int i = 0; i < n; i++)
    cin >> arr[i], s = s + arr[i];
  int k;
  cin >> k;
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    int s = 0;
    for (int i = 0; i < n; i++) {
      if (arr[i] == a)
        arr[i] = b;
      s = s + arr[i];
    }
    cout << s << endl;
  }
}
int main() {
  int t;
  t = 1;
  while (t--) {
    solve();
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long int64;

int main() {
  int N;
  cin >> N;
  map<int, int> mii;
  int tmp;
  rep(i, N) {
    cin >> tmp;
    ++mii[tmp];
  }

  int64 ans = 0;
  auto end = mii.end();
  for (auto itr = mii.begin(); itr != end; ++itr) {
    int f = itr->first;
    int s = itr->second;
    ans += f * (int64)s;
  }

  int Q;
  cin >> Q;
  int tmp1, tmp2;
  rep(i, Q) {
    cin >> tmp1 >> tmp2;

    ans += mii[tmp1] * tmp2 - mii[tmp1] * tmp1;

    mii[tmp2] += mii[tmp1];
    mii.erase(tmp1);

    cout << ans << "\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long int64;

int main() {
  int N;
  cin >> N;
  map<int, int> mii;
  int tmp;
  rep(i, N) {
    cin >> tmp;
    ++mii[tmp];
  }

  int Q;
  cin >> Q;
  int tmp1, tmp2;
  rep(i, Q) {
    cin >> tmp1 >> tmp2;

    mii[tmp2] += mii[tmp1];
    mii.erase(tmp1);

    int64 ans = 0;

    auto end = mii.end();
    for (auto itr = mii.begin(); itr != end; ++itr) {
      int f = itr->first;
      int s = itr->second;

      ans += f * (int64)s;
    }
    cout << ans << "\\n";
  }

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
// #include<set>
// #include<vector>
// #include<iomanip>
using namespace std;
long long arr[1000000];
int main() {

  long long n, m, sum = 0;
  cin >> n;
  for (int i = 0, x; i < n; i++) {
    cin >> x;
    sum += x;
    arr[x]++;
  }
  cin >> m;
  long long arr2[2];
  for (int i = 0; i < m; i++) {
    cin >> arr2[0] >> arr2[1];
    sum = sum - (arr[arr2[0]] * arr2[0]) + (arr[arr2[0]] * arr2[1]);
    cout << sum;
    if (i != m - 1) {
      cout << endl;
    }
    arr[arr2[1]] += arr[arr2[0]];
    arr[arr2[0]] = 0;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
// #include<set>
// #include<vector>
// #include<iomanip>
using namespace std;
int main() {

  int n, m, sum = 0;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  sort(arr, arr + n);
  cin >> m;
  int arr2[2][m];
  for (int i = 0, r = -1, l = 1; i < m; i++, l = 1, r = -1) {
    cin >> arr2[0][i] >> arr2[1][i];
    for (int j = 0; j < n; j++) {
      if (arr[j] == arr2[0][i]) {
        r = j;
        for (int g = j + 1; g < n; g++) {
          if (arr[g] != arr2[0][i]) {
            break;
          }
          l++;
        }
        break;
      }
    }
    if (r == -1) {
      cout << sum << endl;
      continue;
    }
    sum = sum - (arr[r] * l) + arr2[1][i] * l;
    cout << sum;
    if (i != m - 1) {
      cout << endl;
    }
    if (arr2[0][i] < arr2[1][i]) {
      int j = r + l;
      for (; j < n; j++) {
        if (arr[j] >= arr2[1][i]) {
          break;
        }
        arr[j - l] = arr[j];
      }
      for (r = j - 1; l > 0; r--, l--) {
        arr[r] = arr2[1][i];
      }
    } else {
      r--;
      int j = r;
      for (; j >= 0; j--) {
        if (arr2[1][i] <= arr[j]) {
          break;
        }
        arr[j] = arr[j + l];
      }
      for (r = j + 1; l > 0; l--, r++) {
        arr[r] = arr2[1][i];
      }
    }
  }

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

#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main() {
  int n, q;
  long long sum = 0;
  cin >> n;
  map<int, int> a;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    sum += temp;

    auto lb = a.lower_bound(temp);

    if (lb != a.end() && temp == lb->first)
      lb->second++;
    else
      a.insert(lb, {temp, 1});
  }
  cin >> q;
  vector<int> b(q), c(q);
  for (int i = 0; i < q; i++)
    cin >> b[i] >> c[i];
  for (int i = 0; i < q; i++) {
    auto it = a.find(b[i]);
    if (it != a.end()) {
      int count = it->second;
      sum += ((long long)count) * (c[i] - b[i]);
      a.erase(it);

      auto lb = a.lower_bound(c[i]);

      if (lb != a.end() && c[i] == lb->first)
        lb->second += count;
      else
        a.insert(lb, {c[i], count});
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

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

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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define trav(a, x) for (auto &a : x)

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

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

  int q;
  cin >> q;

  while (q--) {
    ll b, c;
    cin >> b >> c;
    m[c] += m[b];
    curr = curr - b * m[b] + c * m[b];
    cout << curr << '\\n';
    m[b] = 0;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define trav(a, x) for (auto &a : x)

template <class T> void print(vector<T> &v, bool spaced = true) {
  int n = v.size();
  string space = (spaced ? " " : "");
  for (int i = 0; i < n; ++i) {
    cout << v[i] << space;
  }
  cout << '\\n';
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  int n;
  cin >> n;
  vector<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)
      a[i] = (a[i] == b ? c : a[i]);
    int ans = 0;
    for (int i = 0; i < n; ++i)
      ans += a[i];
    cout << ans << '\\n';
  }

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

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

const int width = 100001;
int main() {
  int n;
  cin >> n;
  vector<int> ans(width, 0);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    ans.at(a)++;
  }
  long long answer = 0;
  for (int i = 0; i < width; i++) {
    answer += i * ans.at(i);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    int dif = 0;
    if (ans.at(b) > 0) {
      ans.at(c) += ans.at(b);
      dif = (c - b) * ans.at(b);
      ans.at(b) = 0;
    }
    answer += dif;
    cout << answer << endl;
  }
}


Error: TLE

Fixed submission:

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

const int width = 100001;
int main() {
  int n;
  cin >> n;
  vector<int> ans(width, 0);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    ans.at(a)++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    if (ans.at(b) > 0) {
      ans.at(c) += ans.at(b);
      ans.at(b) = 0;
    }
    long long answer = 0;
    for (int i = 1; i < width; i++) {
      answer += i * ans.at(i);
    }
    cout << answer << endl;
  }
}