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

#include <iostream>
using namespace std;
long long int a[100006];
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    a[x]++;
  }
  long long int q, sum = 0;
  cin >> q;
  for (int i = 1; i <= 100000; i++) {
    if (a[i] > 0) {
      sum += a[i] * i;
    }
  }
  for (int i = 0; i < q; i++) {
    long long int b, c;
    cin >> b >> c;
    sum -= a[b] * b;
    sum += a[b] * c;
    a[c] += a[b];
    a[b] = 0;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int a[100006];
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    a[x]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    a[c] += a[b];
    a[b] = 0;
    int sum = 0;
    for (int i = 1; i <= 100000; i++) {
      if (a[i] > 0) {
        sum += a[i] * i;
      }
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define MAX 1000000000
#define INF 1000000
#define M 1000000007
#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
int main() {
  fast_io ll n;
  cin >> n;
  ll a[n];
  ll sum = 0;
  map<ll, ll> mp;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    mp[a[i]]++;
  }
  ll q;
  cin >> q;
  while (q--) {
    ll x, y;
    cin >> x >> y;
    ll r = mp[x];
    ll o = x * r;
    sum = sum - o;
    sum += y * r;
    mp[y] += mp[x];
    mp[x] = 0;
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define MAX 1000000000
#define INF 1000000
#define M 1000000007
#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
int main() {
  fast_io ll n;
  cin >> n;
  ll a[n];
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll q;
  cin >> q;
  while (q--) {
    ll x, y;
    cin >> x >> y;
    ll sum = 0;
    for (ll i = 0; i < n; i++) {
      if (a[i] == x) {
        a[i] = y;
      }
      sum += a[i];
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long int
#define ul unsigned long long int
#define mp make_pair
#define endl "\\n"
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define lb lower_bound
#define forn(i, n) for (ll i = 0; i < n; i++)
#define arr(a, n)                                                              \\
  for (ll i = 0; i < n; i++) {                                                 \\
    cin >> a[i];                                                               \\
  }
#define ub upper_bound
#define pb push_back
#define pi pair<int, int>
#define mod 1000000007
#define vectormax *max_element
#define vectormin *min_element
#define arraysize 1000000
#define setbit _builtin_popcountll
using namespace std;

ll ipow(ll x, ll n) {
  ll result = 1;
  while (n) {
    if (n & 1)
      result = result * x % mod;
    n = n / 2;
    x = x * x % mod;
  }
  return result;
}

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

  ll t;
  cin >> t;
  while (t--) {
    ll x, y;
    cin >> x >> y;
    s = s - x * m[x] + y * m[x];
    ll t = m[x];
    m[x] -= t;
    m[y] += t;
    cout << s << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define ul unsigned long long int
#define mp make_pair
#define endl "\\n"
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define lb lower_bound
#define forn(i, n) for (ll i = 0; i < n; i++)
#define arr(a, n)                                                              \\
  for (ll i = 0; i < n; i++) {                                                 \\
    cin >> a[i];                                                               \\
  }
#define ub upper_bound
#define pb push_back
#define pi pair<int, int>
#define mod 1000000007
#define vectormax *max_element
#define vectormin *min_element
#define arraysize 1000000
#define setbit _builtin_popcountll
using namespace std;

ll ipow(ll x, ll n) {
  ll result = 1;
  while (n) {
    if (n & 1)
      result = result * x % mod;
    n = n / 2;
    x = x * x % mod;
  }
  return result;
}

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

  ll t;
  cin >> t;
  while (t--) {
    ll x, y;
    cin >> x >> y;
    //      for(auto x:m)
    //        cout<<x.first<<" "<<x.second<<endl;
    s = s - x * m[x] + y * m[x];
    ll t = m[x];
    while (t--) {
      m[x] -= m[x];
      m[y]++;
    }

    cout << s << endl;
  }
  /*
  unordered_map<int,int>m;
  int a[5]={1,1,2,2,3};
  for(int i=0;i<5;i++)
      m[a[i]]++;
      m[5]++;
  for(auto x:m)
  cout<<x.first<<" "<<x.second<<endl;*/

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

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

vector<long long> d(100010);

int main() {
  int n, q, a, b, c;
  cin >> n;
  long long ans = 0;
  rep(i, n) {
    cin >> a;
    d[a]++;
    ans += a;
  }
  cin >> q;
  rep(i, q) {
    cin >> b >> c;
    ans -= d[b] * b;
    ans -= d[c] * c;
    d[c] += d[b];
    d[b] = 0;
    ans += d[c] * c;
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>
using namespace std;
const int Max = 100001;

int main() {

  long long a[Max] = {};
  int A, b, c;
  int n, q;
  long long sum = 0;

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

  cin >> q;

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

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

    cout << sum << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll n;
  cin >> n;
  vector<ll> a;
  a.assign(n, 0);
  ll sum = 0;
  map<ll, ll> m;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
    sum = sum + a[i];
  }
  ll q;
  cin >> q;
  ll x, y;
  while (q--) {
    cin >> x >> y;
    if (m[x]) {
      if (y > x)
        sum += m[x] * (y - x);
      else
        sum = sum - m[x] * (x - y);
      ll temp = m[x];
      m.erase(x);
      m[y] += temp;
    }
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll int
vector<ll> a;
bool binarysearch(ll x, ll y, ll n) {
  ll i = 0;
  // cout<<n<<endl;
  while (i <= n) {
    ll mid = i + (n - i) / 2;
    if (a[mid] == x) {
      a[mid] = y;
      return true;
    } else if (a[mid] > x)
      n = mid - 1;
    else
      i = mid + 1;
  }
  return false;
}
int main() {
  ll n;
  cin >> n;
  a.assign(n, 0);
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    sum = sum + a[i];
  }
  sort(a.begin(), a.end());
  ll q;
  cin >> q;
  ll x, y;
  while (q--) {
    cin >> x >> y;
    while (binarysearch(x, y, n - 1) == true) {
      // for(int i=0;i<n;i++) cout<<a[i]<<" ";
      if (y > x)
        sum += (y - x);
      else
        sum -= (x - y);
      sort(a.begin(), a.end());
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

int main() {
  int N;
  std::cin >> N;
  std::vector<int> A(N);
  std::vector<int> cnt(100001, 0);
  long long total = 0;
  for (int i = 0; i < N; i++) {
    std::cin >> A[i];
    cnt[A[i]]++;
    total += A[i];
  }
  int Q;
  std::cin >> Q;
  std::vector<int> B(Q);
  std::vector<int> C(Q);
  for (int i = 0; i < Q; i++) {
    std::cin >> B[i] >> C[i];
  }

  for (int i = 0; i < Q; i++) {
    int cnt_tmp = cnt[B[i]];
    long long chg = (C[i] - B[i]) * cnt_tmp;
    total += chg;
    cnt[B[i]] = 0;
    cnt[C[i]] += cnt_tmp;
    std::cout << total << std::endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

int main() {
  int N;
  std::cin >> N;
  std::vector<int> A(N);
  std::vector<int> cnt(1000000, 0);
  long long total = 0;
  for (int i = 0; i < N; i++) {
    std::cin >> A[i];
    cnt[A[i]]++;
    total += A[i];
  }
  int Q;
  std::cin >> Q;
  std::vector<int> B(N);
  std::vector<int> C(N);
  for (int i = 0; i < Q; i++) {
    std::cin >> B[i] >> C[i];
  }

  for (int i = 0; i < Q; i++) {
    int cnt_tmp = cnt[B[i]];
    long long chg = (C[i] - B[i]) * cnt_tmp;
    total += chg;
    cnt[B[i]] = 0;
    cnt[C[i]] += cnt_tmp;
    std::cout << total << std::endl;
  }

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

#include <iostream>

using namespace std;

int main() {
  int n, q;
  long long s = 0;
  cin >> n;

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

  cin >> q;

  int p[100005] = {0};

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

    s += (c - b) * p[b];
    p[c] += p[b];
    p[b] = 0;

    cout << s << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  bool flg = false;
  int n, q;
  long long s = 0;
  cin >> n;

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

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

  vector<pair<int, int>> p;
  s += a[0];
  p.push_back(make_pair(a[0], 1));
  for (int i = 1; i < n; i++) {
    s += a[i];
    for (int j = 0; j < p.size(); j++) {

      if (p.at(j).first == a[i]) {
        p.at(j).second += 1;
        flg = true;
        break;
      }
    }
    if (flg) {
      flg = false;
    } else {
      p.push_back(make_pair(a[i], 1));
    }
  }
  for (int i = 0; i < q; i++) {
    for (int j = 0; j < p.size(); j++) {
      if (p.at(j).first == b[i]) {
        s += (c[i] - b[i]) * (p.at(j).second);
        p.at(j).first = c[i];
      }
    }
    cout << s << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
#define ll long long
#define read(n) scanf("%lld", &n)
#define f(i, a, b) for (int i = a; i <= b; ++i)
#define N 100005
ll num[N];
int main() {
  ll n, q, sum = 0;
  read(n);
  f(i, 1, n) {
    ll x;
    read(x);
    ++num[x];
    sum += x;
  }
  read(q);
  f(i, 1, q) {
    ll x, y;
    read(x);
    read(y);
    sum += (y - x) * (num[x]);
    num[y] += num[x];
    num[x] = 0;
    cout << sum << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
#define ll long long
#define read(n) scanf("%d", &n)
#define f(i, a, b) for (int i = a; i <= b; ++i)
#define N 100005
ll num[N];
int main() {
  ll n, q, sum = 0;
  read(n);
  f(i, 1, n) {
    ll x;
    read(x);
    ++num[x];
    sum += x;
  }
  read(q);
  f(i, 1, q) {
    ll x, y;
    read(x);
    read(y);
    sum += (y - x) * (num[x]);
    num[y] += num[x];
    num[x] = 0;
    cout << sum << endl;
  }
  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;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int main() {
  int n;
  cin >> n;
  map<ll, ll> mp;
  ll res = 0;
  rep(i, n) {
    ll a;
    cin >> a;
    mp[a]++;
    res += a;
  }

  ll q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    ll b, c;
    cin >> b >> c;
    ll num = mp[b];
    mp[b] = 0;
    mp[c] += num;

    res += (c - b) * num;
    cout << res << 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;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int main() {
  ll n;
  cin >> n;
  map<ll, ll> mp;
  vector<ll> array(n);
  rep(i, n) {
    ll a;
    cin >> a;
    array[i] = a;
    mp[a]++;
  }

  ll q;
  cin >> q;

  rep(i, q) {
    ll a, b, sum = 0;
    cin >> a >> b;
    mp[b] += mp[a];
    mp[a] = 0;
    rep(j, 100001) {
      if (j == a)
        continue;
      sum += j * mp[j];
    }
    cout << sum << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pf push_front
#define ll long long

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

  ll x, n, q, sum = 0, a, b;
  vector<ll> arr(100005);
  cin >> n;

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

  cin >> q;
  while (q--) {
    cin >> a >> b;
    sum -= (a * arr[a]);
    sum += (b * arr[a]);
    arr[b] += arr[a];
    arr[a] = 0;

    cout << sum << "\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pf push_front
#define ll long long

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

  ll x, n, q, sum = 0, a, b;
  cin >> n;
  map<int, int> mp;
  map<int, int>::iterator it, it2;

  for (int i = 1; i <= n; i++) {
    cin >> x;
    mp[x]++;
  }
  for (it = mp.begin(); it != mp.end(); it++) {
    sum += (it->first * it->second);
  }
  cin >> q;
  while (q--) {
    cin >> a >> b;
    it = mp.find(a);
    sum -= (it->first * it->second);
    int temp = it->second;
    it2 = mp.find(b);
    if (it2 == mp.end()) {
      mp[b] = temp;
      sum += (b * temp);
    } else {
      sum -= (it2->first * it2->second);
      int temp2 = it2->second;
      mp[b] = temp + temp2;
      sum += (b * (temp + temp2));
    }
    mp.erase(it);
    cout << sum << "\\n";
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define PB push_back
#define ALL(v) (v).begin(), (v).end()
#define FOR(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define RFOR(i, n) for (ll(i) = (n)-1; (i) >= 0; (i)--)
#define REP(i, x, n) for (ll(i) = (x); (i) < (n); (i)++)
#define RREP(i, x, n) for (ll(i) = (n)-1; (i) >= x; (i)--)

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

  ll N, Q;
  cin >> N;
  vector<ll> A(N);
  FOR(i, N) cin >> A[i];
  cin >> Q;
  vector<ll> B(Q), C(Q);
  FOR(i, Q) cin >> B[i] >> C[i];
  ll sum = 0;
  vector<ll> num(100000, 0);
  FOR(i, N) {
    sum += A[i];
    num[A[i]]++;
  }
  FOR(i, Q) {
    sum += (C[i] - B[i]) * num[B[i]];
    num[C[i]] += num[B[i]];
    num[B[i]] = 0;
    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define PB push_back
#define ALL(v) (v).begin(), (v).end()
#define FOR(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define RFOR(i, n) for (ll(i) = (n)-1; (i) >= 0; (i)--)
#define REP(i, x, n) for (ll(i) = (x); (i) < (n); (i)++)
#define RREP(i, x, n) for (ll(i) = (n)-1; (i) >= x; (i)--)

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

  ll N, Q;
  cin >> N;
  vector<ll> A(N);
  FOR(i, N) cin >> A[i];
  cin >> Q;
  vector<ll> B(Q), C(Q);
  FOR(i, Q) cin >> B[i] >> C[i];
  ll sum = 0;
  FOR(i, N) sum += A[i];
  FOR(i, Q) {
    FOR(j, N) {
      if (A[j] == B[i]) {
        sum += C[i] - B[i];
        A[j] = C[i];
      }
    }
    cout << sum << endl;
  }

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

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

const int MX = 100005;

int main() {
  int n, q;
  cin >> n;
  vector<int> bunpu(MX);
  ll tot = 0;
  rep(i, n) {
    int a;
    cin >> a;
    bunpu[a]++;
    tot += a;
  }
  cin >> q;
  vector<int> b(q + 5), c(q + 5);
  rep(i, q) cin >> b[i] >> c[i];
  rep(i, q) {
    tot += (c[i] - b[i]) * bunpu[b[i]];
    bunpu[c[i]] += bunpu[b[i]];
    bunpu[b[i]] = 0;
    cout << tot << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n, q;
  cin >> n;
  vector<int> a(n + 5), bunpu(n + 10);
  ll tot = 0;
  rep(i, n) {
    cin >> a[i];
    bunpu[a[i]]++;
    tot += a[i];
  }
  cin >> q;
  vector<int> b(q + 5), c(q + 5);
  rep(i, q) cin >> b[i] >> c[i];
  rep(i, q) {
    tot += (c[i] - b[i]) * bunpu[b[i]];
    bunpu[c[i]] += bunpu[b[i]];
    bunpu[b[i]] = 0;
    cout << tot << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
using ll = long long;

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  int a, b, c, q;
  ll sum;
  map<int, ll> mp;
  for (int i = 0; i < n; ++i) {
    cin >> a;
    mp[a]++;
  }
  // for(auto itr=mp.begin();itr!=mp.end();++itr){
  //     cout<<itr->first<<":"<<itr->second<<endl;
  // }
  cin >> q;
  for (int i = 0; i < q; ++i) {
    sum = 0;
    cin >> b >> c;
    auto itr1 = mp.find(b);
    auto itr2 = mp.find(c);
    if (itr1 != mp.end()) {
      if (itr2 == mp.end())
        mp[c] = itr1->second;
      else
        mp[itr2->first] += itr1->second;
      mp[itr1->first] = 0;
    }
    // for(auto itr=mp.begin();itr!=mp.end();++itr){
    //     cout<<itr->first<<":"<<itr->second<<endl;
    // }
    for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
      sum += (itr->first) * (itr->second);
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N, Q;
  cin >> N;
  long long sum = 0;

  vector<int> A(N);
  vector<int> count(100000, 0);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    count.at(A.at(i) - 1)++;
    sum += A.at(i);
  }
  cin >> Q;

  vector<int> B(Q);
  vector<int> C(Q);
  for (int i = 0; i < Q; i++) {
    cin >> B.at(i) >> C.at(i);
  }

  for (int i = 0; i < Q; i++) {
    count.at(C.at(i) - 1) += count.at(B.at(i) - 1);
    sum += (count.at(B.at(i) - 1)) * (C.at(i) - B.at(i));
    count.at(B.at(i) - 1) = 0;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int N, Q;
  cin >> N;

  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  cin >> Q;

  vector<int> B(Q);
  vector<int> C(Q);
  for (int i = 0; i < Q; i++) {
    cin >> B.at(i) >> C.at(i);
  }

  vector<long long> S(Q, 0);
  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < N; j++) {
      if (A.at(j) == B.at(i)) {
        A.at(j) = C.at(i);
      }
      S.at(i) += A.at(j);
    }
    cout << S.at(i) << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int cnt[100000];
int main() {
  // freopen("input.txt", "r", stdin);

  int n;
  scanf("%d", &n);

  int t;
  long long int sum = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &t);
    cnt[t]++;
    sum += t;
  }

  int q, b, c;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &b, &c);
    cnt[c] += cnt[b];
    sum += (-cnt[b] * b + cnt[b] * c);
    cnt[b] = 0;

    printf("%lld\\n", sum);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  // freopen("input.txt", "r", stdin);

  int n;
  scanf("%d", &n);

  int a[100000];
  long long int sum = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
  }

  int q, b, c;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &b, &c);
    for (int j = 0; j < n; j++) {
      if (a[j] == b) {
        sum += (-a[j] + c);
        a[j] = c;
      }
    }
    printf("%lld\\n", sum);
  }

  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 p = pair<int, int>;
typedef long long ll;
const int MX = 100005;

int main() {
  int n;
  cin >> n;
  vector<ll> cnt(MX);
  rep(i, n) {
    int a;
    cin >> a;
    cnt[a]++;
  }
  ll o = 0;
  rep(i, MX) { o += i * cnt[i]; }
  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    o -= b * cnt[b];
    o -= c * cnt[c];
    cnt[c] += cnt[b];
    cnt[b] = 0;
    o += b * cnt[b];
    o += c * cnt[c];
    cout << o << 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 p = pair<int, int>;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  int q;
  cin >> q;
  ll fin = 0;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    ll ans = 0;
    rep(j, n) {
      if (a[j] == b) {
        a[j] = c;
      }
      ans += a[j];
    }
    cout << ans << endl;
  }
  return 0;
}