alexjercan/bugnet huggingface dataset:
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
const ll mod = 1000000007;
const ll INF = 1e+14;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define _GLIBCXX_DEBUG

int main() {
  ll n;
  cin >> n;
  vector<ll> num(100010, 0);
  ll ans = 0;

  rep(i, n) {
    ll a;
    cin >> a;
    num[a]++;
    ans += a;
  }

  ll q;
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    ll v = num[b];
    num[b] = 0;
    num[c] += v;
    ans -= v * b;
    ans += v * c;
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
const ll mod = 1000000007;
const ll INF = 1e+14;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define _GLIBCXX_DEBUG

int main() {
  ll n;
  cin >> n;
  vector<ll> num(10010, 0);
  ll ans = 0;

  rep(i, n) {
    ll a;
    cin >> a;
    num[a]++;
    ans += a;
  }

  ll q;
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    ll v = num[b];
    num[b] = 0;
    num[c] += v;
    ans -= v * b;
    ans += v * c;
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  int t = 1;
  // cin>>t;
  while (t--) {
    ll n, k;
    cin >> n;
    ll a[n];
    ll fre[100005];
    ll sum = 0;
    memset(fre, 0, sizeof(fre));
    for (ll i = 0; i < n; ++i) {
      cin >> a[i];
      fre[a[i]]++;
      sum += a[i];
    }
    ll q;
    cin >> q;
    ll x, y;

    for (ll i = 1; i <= q; ++i) {
      cin >> x >> y;

      sum -= x * fre[x];
      sum += fre[x] * y;
      fre[y] += fre[x];
      fre[x] = 0;

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


Error: TLE

Fixed submission:

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

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

  int t = 1;
  // cin>>t;
  while (t--) {
    int n, k;
    cin >> n;
    int a[n];
    int fre[100001];
    memset(fre, 0, sizeof(fre));
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
      fre[a[i]]++;
    }
    int q;
    cin >> q;
    int x, y;
    ll sum = 0;
    for (int i = 1; i <= q; ++i) {
      cin >> x >> y;
      sum = 0;
      fre[y] += fre[x];
      fre[x] = 0;
      for (int i = 1; i <= 1e5; ++i)
        sum += i * fre[i];
      cout << sum << "\\n";
    }
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  vector<int> a(n);
  map<int, int> mp;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
    sum += a[i];
  }
  ll q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    ll x, y;
    cin >> x >> y;
    mp[y] += mp[x];
    sum += mp[x] * (y - x);
    mp[x] = 0;
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  // your code goes here
  int n, q;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int b, c;
  cin >> q;
  int sum;
  while (q--) {
    cin >> b >> c;
    sum = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] == b)
        a[i] = c;
      sum += a[i];
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> idx[100001];
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    idx[a[i]].push_back(i);
  }
  int q, b, c;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    for (auto &p : idx[b]) {
      idx[c].push_back(p);
      sum -= b;
      sum += c;
    }
    idx[b].clear();
    cout << sum << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sort(a.begin(), a.end());
    auto lower = lower_bound(a.begin(), a.end(), b);
    auto upper = upper_bound(a.begin(), a.end(), b);
    for (; lower < upper; lower++) {
      *lower = c;
      sum -= b;
      sum += c;
    }
    cout << sum << '\\n';
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)

#define ALL(x) (x).begin(), (x).end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

using namespace std;
using ll = unsigned long long;
using vel = vector<ll>;
using ves = vector<string>;
using veb = vector<bool>;

using qul = queue<ll>;

int main() {
  ll N, Q, sum = 0;
  cin >> N;
  vel A(N), count(1000000);
  REP(i, 1000000) count[i] = 0;
  REP(i, N) {
    cin >> A[i];
    count[A[i] - 1] = count[A[i] - 1] + 1;
  }
  cin >> Q;
  vel B(Q), C(Q);
  REP(i, Q) { cin >> B[i] >> C[i]; }
  REP(i, N) sum = sum + A[i];
  REP(i, Q) {
    sum = sum + count[B[i] - 1] * (C[i] - B[i]);
    count[C[i] - 1] = count[C[i] - 1] + count[B[i] - 1];
    count[B[i] - 1] = 0;
    cout << sum << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)

#define ALL(x) (x).begin(), (x).end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

using namespace std;
using ll = unsigned long long;
using vel = vector<ll>;
using ves = vector<string>;
using veb = vector<bool>;

using qul = queue<ll>;

int main() {
  ll N, Q, sum = 0;
  cin >> N;
  vel A(2 * N), count(100000);
  REP(i, 2 * N) count[i] = 0;
  REP(i, N) {
    cin >> A[i];
    count[A[i] - 1] = count[A[i] - 1] + 1;
  }
  cin >> Q;
  vel B(2 * Q), C(2 * Q);
  REP(i, Q) { cin >> B[i] >> C[i]; }
  REP(i, N) sum = sum + A[i];
  REP(i, Q) {
    sum = sum + count[B[i] - 1] * (C[i] - B[i]);
    count[C[i] - 1] = count[C[i] - 1] + count[B[i] - 1];
    count[B[i] - 1] = 0;
    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 < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
typedef long long ll;
#define _GLIBCXX_DEBUG
typedef vector<int> vec;
typedef vector<ll> lvec;
typedef vector<char> cvec;
typedef vector<double> dvec;
typedef pair<ll, ll> LP;
typedef tuple<ll, ll, ll> LT;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
#define fs first
#define sc second
const int mx = 100001;
int main() {
  int n;
  ll sum = 0;
  cin >> n;
  vector<ll> d(mx);
  rep(i, n) {
    int a;
    cin >> a;
    d[a]++;
  }
  rep(i, mx) sum += i * d[i];
  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * d[b];
    d[c] += d[b];
    d[b] = 0;
    cout << sum << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
typedef long long ll;
#define _GLIBCXX_DEBUG
typedef vector<int> vec;
typedef vector<ll> lvec;
typedef vector<char> cvec;
typedef vector<double> dvec;
typedef pair<ll, ll> LP;
typedef tuple<ll, ll, ll> LT;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
#define fs first
#define sc second
const int mx = 10001;
int main() {
  int n;
  ll sum = 0;
  cin >> n;
  vector<ll> d(mx);
  rep(i, n) {
    int a;
    cin >> a;
    d[a]++;
  }
  rep(i, mx) sum += i * d[i];
  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * d[b];
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_set>

using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, n, m) for (int i = n; i < m; ++i)

using Graph = vector<vector<int>>;

// 深さ優先探索
vector<bool> seen;
void dfs(const Graph &G, int v) {
  seen[v] = true; // v を訪問済にする

  // v から行ける各頂点 next_v について
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue;     // next_v が探索済だったらスルー
    dfs(G, next_v); // 再帰的に探索
  }
}

bool IsPrime(int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false; // 偶数はあらかじめ除く

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      // 素数ではない
      return false;
    }
  }

  // 素数である
  return true;
}

int main() {
  ll N, Q, tmp;
  cin >> N;
  ll naka[100010] = {};
  set<ll> A;
  rep(i, N) {
    cin >> tmp;
    if (naka[tmp] == 0)
      A.insert(tmp);
    naka[tmp]++;
  }

  cin >> Q;
  ll B[100010];
  ll C[100010];
  rep(i, Q) { cin >> B[i] >> C[i]; }

  ll t1, t2;
  ll sum = 0;
  for (auto x : A) {
    sum += x * naka[x];
  }
  rep(i, Q) {
    // auto itr = A.find(B[i]);
    // if(itr != A.end()){
    t1 = naka[B[i]];
    naka[C[i]] += naka[B[i]];
    naka[B[i]] = 0;
    A.erase(B[i]);
    A.insert(C[i]);
    sum = sum + t1 * (C[i] - B[i]);
    //}
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_set>

using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, n, m) for (int i = n; i < m; ++i)

using Graph = vector<vector<int>>;

// 深さ優先探索
vector<bool> seen;
void dfs(const Graph &G, int v) {
  seen[v] = true; // v を訪問済にする

  // v から行ける各頂点 next_v について
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue;     // next_v が探索済だったらスルー
    dfs(G, next_v); // 再帰的に探索
  }
}

bool IsPrime(int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false; // 偶数はあらかじめ除く

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      // 素数ではない
      return false;
    }
  }

  // 素数である
  return true;
}

int main() {
  ll N, Q, tmp;
  cin >> N;
  ll naka[100010] = {};
  set<ll> A;
  rep(i, N) {
    cin >> tmp;
    if (naka[tmp] == 0)
      A.insert(tmp);
    naka[tmp]++;
  }

  cin >> Q;
  ll B[100010];
  ll C[100010];
  rep(i, Q) { cin >> B[i] >> C[i]; }

  ll sum;
  rep(i, Q) {
    sum = 0;
    // auto itr = A.find(B[i]);
    // if(itr != A.end()){
    naka[C[i]] += naka[B[i]];
    naka[B[i]] = 0;
    A.erase(B[i]);
    A.insert(C[i]);
    //}
    for (auto x : A) {
      sum += x * naka[x];
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N;
  cin >> N;
  long long Q;
  vector<long long> A(N);
  vector<long long> hantei(100000);
  long long answer = 0;
  for (long long i = 0; i < N; i++) {
    cin >> A.at(i);
    hantei.at(A.at(i) - 1)++;
    answer += A.at(i);
  }
  cin >> Q;
  vector<long long> B(Q);
  vector<long long> C(Q);
  for (long long i = 0; i < Q; i++) {
    cin >> B.at(i);
    cin >> C.at(i);
    answer += hantei.at(B.at(i) - 1) * (C.at(i) - B.at(i));
    cout << answer << endl;
    if (hantei.at(B.at(i) - 1) != 0) {
      hantei.at(C.at(i) - 1) += hantei.at(B.at(i) - 1);
      hantei.at(B.at(i) - 1) = 0;
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int Q;
  vector<int> A(N);
  vector<int> hantei(10000);
  long long answer = 0;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    hantei.at(A.at(i) - 1)++;
    answer += A.at(i);
  }
  cin >> Q;
  vector<int> B(Q);
  vector<int> C(Q);
  for (int i = 0; i < Q; i++) {
    cin >> B.at(i);
    cin >> C.at(i);
    answer += hantei.at(B.at(i) - 1) * (C.at(i) - B.at(i));
    cout << answer << endl;
    if (hantei.at(B.at(i) - 1) != 0) {
      hantei.at(C.at(i) - 1) += hantei.at(B.at(i) - 1);
      hantei.at(B.at(i) - 1) = 0;
    }
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

// BISMILLAH
#include <bits/stdc++.h>
#define ll long long
#define max 100005
using namespace std;
vector<ll> freq(max);
// int DFS(int);

int main() {
  ll n, flag = 0, x, value = 0, sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    sum += x;
    freq[x]++;
  }

  int t;
  cin >> t;
  while (t--) {
    ll a, b;
    cin >> a >> b;
    sum -= freq[a] * a;
    sum += freq[a] * b;
    freq[b] += freq[a];
    freq[a] = 0;

    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  int t, n, flag = 0, ans = 0;
  cin >> n;
  vector<int> v(n);

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

  cin >> t;
  while (t--) {
    int sum = 0;
    sort(v.begin(), v.end());
    vector<int>::iterator upper, lower;
    int x, y;
    cin >> x >> y;
    upper = upper_bound(v.begin(), v.end(), x);
    lower = lower_bound(v.begin(), v.end(), x);
    int f = upper - v.begin();
    int g = lower - v.begin();
    // int minuss=upper-lower;
    for (int i = g; i < f; i++)
      v[i] = y;
    for (int i = 0; i < n; i++)
      sum += v[i];
    cout << sum << endl;
  }

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

/**
 *   Bismillahir Rahmanir Rahim.
 *   Imtiaz_rafi
 *   PCIU, CHITTAGONG
 **/
#include <bits/stdc++.h>
using namespace std;
#define speed                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);
#define filein freopen("input.txt", "r", stdin)
#define fileout freopen("output.txt", "w", stdout)
#define rep(i, a) for (ll i = 0; i < a; i++)
#define rep1(i, begin, end) for (ll i = begin; i <= end; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define pf push_front
#define mp make_pair
#define pi acos(-1)
// bitshift (1LL<<(3)) *2^3
typedef long long int ll;
typedef vector<ll> vi;
typedef map<ll, ll> m;
typedef map<char, ll> mc;
typedef set<ll> st;
typedef set<char> sc;
const ll mx = 1e5 + 123;
ll binarySearch(ll arr[], ll p, ll r, ll num) {
  if (p <= r) {
    ll mid = (p + r) / 2;
    if (arr[mid] == num)
      return mid;
    if (arr[mid] > num)
      return binarySearch(arr, p, mid - 1, num);
    if (arr[mid] < num)
      return binarySearch(arr, mid + 1, r, num);
  }
  return -1;
}
int main() {
  speed;
  ll t, i, j, a, b, x, y, e = 0, ans = 0, sum = 0;
  cin >> a;
  vi c(mx, 0);
  vi d(a);
  rep(i, a) {
    cin >> d[i];
    sum += d[i];
    c[d[i]]++;
  }
  cin >> b;
  while (b--) {
    cin >> x >> y;
    sum += (y - x) * c[x];
    c[y] += c[x];
    c[x] = 0;
    cout << sum << endl;
  }
}


Error: 0

Fixed submission:

/**
 *   Bismillahir Rahmanir Rahim.
 *   Imtiaz_rafi
 *   PCIU, CHITTAGONG
 **/
#include <bits/stdc++.h>
using namespace std;
#define speed                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);
#define filein freopen("input.txt", "r", stdin)
#define fileout freopen("output.txt", "w", stdout)
#define rep(i, a) for (ll i = 0; i < a; i++)
#define rep1(i, begin, end) for (ll i = begin; i <= end; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define pf push_front
#define mp make_pair
#define pi acos(-1)
// bitshift (1LL<<(3)) *2^3
typedef long long int ll;
typedef vector<ll> vi;
typedef map<ll, ll> m;
typedef map<char, ll> mc;
typedef set<ll> st;
typedef set<char> sc;
const ll mx = 1e5 + 123;
ll binarySearch(ll arr[], ll p, ll r, ll num) {
  if (p <= r) {
    ll mid = (p + r) / 2;
    if (arr[mid] == num)
      return mid;
    if (arr[mid] > num)
      return binarySearch(arr, p, mid - 1, num);
    if (arr[mid] < num)
      return binarySearch(arr, mid + 1, r, num);
  }
  return -1;
}
int main() {
  speed;
  ll t, i, j, a, b, x, y, e = 0, ans = 0, sum = 0;
  cin >> a;
  vi c(a + 5, 0);
  vi d(a);
  rep(i, a) {
    cin >> d[i];
    sum += d[i];
    c[d[i]]++;
  }
  cin >> b;
  while (b--) {
    cin >> x >> y;
    sum += (y - x) * c[x];
    c[y] += c[x];
    c[x] = 0;
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

lli t, n, tmp_a, tmp_b, sum = 0;
vector<lli> v;

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

  cin >> n;
  v.resize(n);
  vector<lli> cnts(100001, 0);

  for (int i = 0; i < v.size(); i++) {
    cin >> v[i];
    sum += v[i];
    ++cnts[v[i]];
  }

  cin >> t;
  while (t--) {
    cin >> tmp_a >> tmp_b;
    sum -= cnts[tmp_a] * tmp_a;
    sum += cnts[tmp_a] * tmp_b;
    cnts[tmp_b] += cnts[tmp_a];
    cnts[tmp_a] = 0;
    cout << sum << "\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

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

lli t, n, tmp_a, tmp_b, sum = 0;
vector<lli> v;

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

  cin >> n;
  v.resize(n);

  for (int i = 0; i < v.size(); i++) {
    cin >> v[i];
    sum += v[i];
  }

  cin >> t;
  while (t--) {
    cin >> tmp_a >> tmp_b;
    for (int i = 0; i < v.size(); i++) {
      if (v[i] == tmp_a) {
        sum -= tmp_a;
        v[i] = tmp_b;
        sum += tmp_b;
      }
    }
    cout << sum << "\\n";
  }

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

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

signed main() {
  int N;
  cin >> N;
  unsigned long long int count = 0;
  vector<unsigned long long int> A(N + 1);
  vector<int> D(100001);
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    count += A[i];
    D[A[i]]++;
  }
  int Q;
  cin >> Q;
  vector<int> B(Q + 1);
  vector<int> C(Q + 1);
  for (int i = 1; i <= Q; i++) {
    cin >> B[i] >> C[i];
    count += D[B[i]] * (C[i] - B[i]);
    cout << count << endl;
    D[C[i]] += D[B[i]];
    D[B[i]] = 0;
  }
}

Error: TLE

Fixed submission:

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

signed main() {
  int N;
  cin >> N;
  vector<int> A(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }
  unsigned long long int count = 0;
  int Q;
  cin >> Q;
  vector<int> B(Q + 1);
  vector<int> C(Q + 1);
  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];
      }
      count += A[j];
    }
    cout << count << endl;
    count = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
int A[100001];
int main() {
  int n, q, tmp;
  cin >> n;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    ++A[tmp];
    sum += tmp;
  }
  cin >> q;
  int b, c;
  for (int ii = 0; ii < q; ii++) {
    cin >> b >> c;
    sum -= 1LL * A[b] * b;
    sum += 1LL * A[b] * c;
    A[c] += A[b];
    A[b] = 0;
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

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

#include <iostream>
#include <vector>
typedef long long ll;
using namespace std;

int main(void) {
  ll N;
  cin >> N;
  vector<ll> vc(100001, 0);
  ll total = 0;
  for (ll i = 0; i < N; i++) {
    ll A;
    cin >> A;
    vc.at(A) += 1;
    total += A;
  }
  int Q;
  cin >> Q;
  for (int i = 1; i <= Q; i++) {
    ll B, C;
    cin >> B >> C;
    ll tmp_b = vc.at(B);
    total -= tmp_b * B;
    vc.at(B) = 0;
    total += C * tmp_b;
    vc.at(C) += tmp_b;
    cout << total << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
typedef long long ll;
using namespace std;

int main(void) {
  ll N;
  cin >> N;
  vector<ll> vc(100000, 0);
  ll total = 0;
  for (ll i = 0; i < N; i++) {
    ll A;
    cin >> A;
    vc.at(A) += 1;
    total += A;
  }
  int Q;
  cin >> Q;
  for (int i = 1; i <= Q; i++) {
    ll B, C;
    cin >> B >> C;
    ll tmp_b = vc.at(B);
    total -= tmp_b * B;
    vc.at(B) = 0;
    total += C * tmp_b;
    vc.at(C) += tmp_b;
    cout << total << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#include <string>
const int mxN = 1e5;
const int maxN = 5e3;
#define ld long double
#define pb push_back
#define mp make_pair
#define ins insert
#define vi vector<int>

int main() {
  int n;
  cin >> n;
  int cnt[100000] = {};
  ll s = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    cnt[x - 1]++;
    s += x;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    s += cnt[b - 1] * (c - b);
    cnt[c - 1] += cnt[b - 1];
    cnt[b - 1] = 0;
    cout << s << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#include <string>
const int mxN = 2e5;
const int maxN = 5e3;
#define ld long double
#define pb push_back
#define mp make_pair
#define ins insert
#define vi vector<int>

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  int q;
  cin >> q;
  while (q--) {
    ll b, c;
    cin >> b >> c;
    for (int i = 0; i < n; i++) {
      if (a[i] == b) {
        sum += c - b;
        a[i] = c;
      }
    }
    cout << sum << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n, q, sum = 0;
  cin >> n;
  vector<int> vec(100000, 0);
  rep(i, n) {
    int temp;
    cin >> temp;
    vec[temp] += 1;
    sum += temp;
  }
  cin >> q;
  rep(i, q) {
    int a, b;
    cin >> a >> b;
    sum += (b - a) * vec[a];
    cout << sum << endl;
    vec[b] += vec[a];
    vec[a] = 0;
  }
}


Error: TLE

Fixed submission:

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

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