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

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

// forループ関係
// 引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
// Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
// xにはvectorなどのコンテナ
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size())      // sizeをsize_tからllに直しておく
// 定数
#define INF 1000000000000 // 10^12:極めて大きい値,∞
#define MOD 1000000007    // 10^9+7:合同式の法
#define MAXR 100000 // 10^5:配列の最大のrange(素数列挙などで使用)
// 略記
#define PB push_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first      // pairの一つ目の要素
#define S second     // pairの二つ目の要素

int main() {

  ll n, q;
  ll sum = 0;
  cin >> n;
  int a[n];
  REP(i, n) {
    cin >> a[i];
    sum += a[i];
  }
  sort(a, a + n);
  ll count = 1;
  ll cons = a[0];
  map<int, int> mp;
  for (ll i = 1; i < (ll)(n); i++) {
    if (a[i] == a[i - 1]) {
      count++;
    } else {
      mp[cons] = count;
      count = 1;
      cons = a[i];
    }
  }
  mp[cons] = count;
  cin >> q;
  ll b, c;
  // 記憶用
  REP(i, q) {
    cin >> b >> c;
    auto itr = mp.find(b);
    if (itr != mp.end()) { // 検索あるなら
      count = mp[b];
      mp[b] = 0;
      auto itr = mp.find(c);
      if (itr != mp.end()) { // 検索あるなら
        mp[c] += count;
      } else {
        mp[c] = count;
      }
      sum += (c - b) * count;
    }
    // 和を出す
    cout << (ll)sum << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

// forループ関係
// 引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
// Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
// xにはvectorなどのコンテナ
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size())      // sizeをsize_tからllに直しておく
// 定数
#define INF 1000000000000 // 10^12:極めて大きい値,∞
#define MOD 1000000007    // 10^9+7:合同式の法
#define MAXR 100000 // 10^5:配列の最大のrange(素数列挙などで使用)
// 略記
#define PB push_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first      // pairの一つ目の要素
#define S second     // pairの二つ目の要素

int main(int argc, char *argv[]) {

  ll n, q;
  ll sum = 0;
  cin >> n;
  int a[n];
  REP(i, n) {
    cin >> a[i];
    sum += a[i];
  }
  cin >> q;
  int b, c;
  REP(i, q) {
    cin >> b >> c;
    // bをcに置き換え
    ll count = 0;
    REP(i, n) {
      if (a[i] == b) {
        a[i] = c;
        count++;
      }
    }
    sum += (c - b) * count;
    // 和を出す
    cout << sum << endl;
  }

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

#include <cstdio>
#include <iostream>
#define maxn 100005
#define ll long long
using namespace std;
ll n, q, a[maxn], sum;
int tm[maxn];
int main(void) {
  while (~scanf("%lld", &n)) {
    sum = 0;
    for (int i = 0; i < n; ++i) {
      scanf("%lld", &a[i]);
      sum += a[i];
      tm[a[i]]++;
    }
    scanf("%lld", &q);
    ll b, c;
    for (int i = 0; i < q; ++i) {
      scanf("%lld %lld", &b, &c);
      sum += (c - b) * tm[b];
      tm[c] += tm[b];
      tm[b] = 0;
      printf("%lld\\n", sum);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#define maxn 100005
#define ll long long
using namespace std;
ll n, q, a[maxn], sum;
int main(void) {
  while (~scanf("%lld", &n)) {
    sum = 0;
    for (int i = 0; i < n; ++i) {
      scanf("%lld", &a[i]);
      sum += a[i];
    }
    scanf("%lld", &q);
    ll b, c;
    for (int i = 0; i < q; ++i) {
      scanf("%lld %lld", &b, &c);
      for (int i = 0; i < n; ++i)
        if (a[i] == b)
          a[i] = c, sum += (c - b);
      printf("%lld\\n", sum);
    }
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

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

  int a[100001];
  int num;
  int i, j;

  for (i = 0; i < 100001; i++) {
    a[i] = 0;
  }

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

  long long sum;
  for (i = 0, sum = 0; i < 100001; i++) {
    sum += a[i] * i;
    ;
  }

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

  int b[q], c[q];

  for (i = 0; i < q; i++) {
    scanf("%d%d", &b[i], &c[i]);
  }

  for (i = 0; i < q; i++) {
    sum = sum + (c[i] - b[i]) * a[b[i]];
    a[c[i]] += a[b[i]];
    a[b[i]] = 0;
    printf("%lld\\n", sum);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>

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

  int aval[n];
  int anum[n];
  int i, j;
  int num;
  int tail = 0;

  for (i = 0; i < n; i++) {
    scanf("%d", &num);
    for (j = 0;; j++) {
      if (j == tail) {
        aval[tail] = num;
        anum[tail] = 1;
        tail++;
        break;
      } else if (aval[j] == num) {
        anum[j]++;
        break;
      }
    }
  }

  int sum;
  for (i = 0, sum = 0; i < tail; i++) {
    sum += aval[i] * anum[i];
  }

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

  int b[n], c[n];
  for (i = 0; i < q; i++) {
    scanf("%d%d", &b[i], &c[i]);
  }

  for (j = 0; j < q; j++) {
    // replace
    for (i = 0; i < tail; i++) {
      if (aval[i] == b[j]) {
        sum = sum + (c[j] - b[j]) * anum[i];
        aval[i] = c[j];
      }
    }

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

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

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

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define int long long
#define sc(n) scanf("%lld", &n)
#define scc(n1, n2) scanf("%lld %lld", &n1, &n2)
#define sccc(n1, n2, n3) scanf("%lld %lld %lld", &n1, &n2, &n3)
#define debug(x) cerr << #x << ": " << x << endl;

signed main() {
  int n, sum = 0;
  sc(n);
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    int x;
    sc(x);
    sum += x;
    m[x]++;
  }
  int q;
  sc(q);
  while (q--) {
    int b, c;
    scc(b, c);
    sum -= m[b] * b;
    sum += m[b] * c;
    m[c] += m[b];
    m[b] = 0;
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

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

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define int long long

#define sc(n) scanf("%lld", &n)
#define scc(n1, n2) scanf("%lld %lld", &n1, &n2)
#define sccc(n1, n2, n3) scanf("%lld %lld %lld", &n1, &n2, &n3)
#define debug(x) cerr << #x << ": " << x << endl;
const int N = 1000000000000002;
int sum(int a[], int sz) {
  int s = 0;
  for (int i = 0; i < sz; i++)
    s += a[i];
  return s;
}

signed main() {
  int n;

  sc(n);
  int a[n];
  for (int i = 0; i < n; i++) {
    sc(a[i]);
  }
  int q;
  sc(q);
  while (q--) {
    int b, c;
    scc(b, c);
    for (int i = 0; i < n; i++) {
      if (a[i] == b)
        a[i] = c;
    }
    printf("%d\\n", sum(a, n));
    // cout<<sum(a, n)<<endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  cin >> n;
  vector<vector<int>> a(100001, vector<int>(1));
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a.at(x).at(0)++;
    ans += x;
  }
  int q;
  cin >> q;
  while (q > 0) {
    int b, c;
    cin >> b >> c;
    long long d = a.at(b).at(0);
    a.at(c).at(0) += a.at(b).at(0);
    a.at(b).at(0) = 0;
    ans -= b * d;
    ans += c * d;

    cout << ans << endl;
    q--;
  }
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)


Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<vector<int>> a(100001, vector<int>(0));
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a.at(x).at(0)++;
    ans += x;
  }
  int q;
  cin >> q;
  while (q > 0) {
    int b, c;
    cin >> b >> c;
    int d = a.at(b).at(0);
    a.at(c).at(0) += a.at(b).at(0);
    a.at(b).at(0) = 0;
    ans -= b * d;
    ans += c * d;

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

#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <stack>
using namespace std;
#define ll long long
int main() {
  // int t;
  // cin>>t;
  // for(int i=0;i<t;i++)
  //{
  ll n, l = 1000010, sum = 0;
  cin >> n;
  vector<ll> a(n);
  vector<ll> cnt(l);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    sum = sum + a[i];
    cnt[a[i]]++;
  }
  ll q;
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll b, c;
    cin >> b >> c;
    sum = sum + (c - b) * cnt[b];
    cout << sum << endl;
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <stack>
using namespace std;
#define ll long long
int main() {
  // int t;
  // cin>>t;
  // for(int i=0;i<t;i++)
  //{
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll t;
  cin >> t;
  for (ll i = 0; i < t; i++) {
    ll b, c, sum = 0;
    cin >> b >> c;
    for (ll i = 0; i < n; i++) {
      if (a[i] == b) {
        a[i] = c;
        // cout<<"a[i]="<<a[i];
      }
      sum += a[i];
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void Main() {
  int n, q;
  cin >> n;
  unordered_map<int, long> am;
  for (int i = 0; i < n; i++) {
    long a;
    cin >> a;
    am[a]++;
  }
  cin >> q;
  long sum = -1;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    am[c] = am[b] + am[c];
    long diff = am[b] * (c - b);
    am.erase(b);

    if (sum < 0) {
      sum = 0;
      for (auto j : am)
        sum += j.first * j.second;
    } else {
      sum = sum + diff;
    }
    cout << sum << endl;
  }
}

int main(int argc, char **argv) {
  Main();
  return 0;
}


Error: TLE

Fixed submission:

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

void Main() {
  int n, q;
  cin >> n;
  unordered_map<int, long> am;
  for (int i = 0; i < n; i++) {
    long a;
    cin >> a;
    am[a]++;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    am[c] = am[b] + am[c];
    am.erase(b);

    long sum = 0;
    for (auto j : am)
      sum += j.first * j.second;
    cout << sum << endl;
  }
}

int main(int argc, char **argv) {
  Main();
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1.0)
#define MOD 1000000007
#define pb push_back
#define cy cout << "Yes\\n"
#define cn cout << "No\\n"
typedef unsigned long long int ll;
int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  ll sum = 0;
  ll cnt[100005] = {};
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    cnt[a[i]]++;
  }
  ll q;
  cin >> q;
  ll sum1 = sum;
  while (q--) {
    ll b, c;
    cin >> b >> c;
    ll va = cnt[b];
    // cout<<va<<endl;
    if (va == 0) {
      cout << sum1 << endl;
    } else {
      sum1 -= (va * b);
      sum1 += (va * c);
      cnt[c] += va;
      cnt[b] = 0;
      cout << sum1 << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1.0)
#define MOD 1000000007
#define pb push_back
#define cy cout << "Yes\\n"
#define cn cout << "No\\n"
typedef long long int ll;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  ll sum = 0;
  int cnt[100005] = {};
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    // sum+=a[i];
    cnt[a[i]]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    for (int i = 0; i < n; i++) {
      if (b == a[i]) {
        a[i] = c;
      }
      sum += a[i];
    }
    cout << sum << endl;
    sum = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;

  int n;
  cin >> n;
  // vector<int> a(n);
  long long currsum = 0;

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

  int q;
  cin >> q;

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

    // int cc = m[c];
    if (m.count(c) == 0) {
      m[c] = 0;
    }
    if (m.count(b) == 0) {
      m[b] = 0;
    }
    int cb = m[b];
    m[c] += cb;
    m[b] -= cb;
    if (m[b] == 0) {
      m.erase(b);
    }
    /*
    long long sum = 0;
    for (auto it = m.begin(); it != m.end(); ++it) {
        sum += (long long)it->first * (long long)it->second;
    }
    */
    currsum += (long long)((c - b) * cb);
    // currsum -= (long long)cb * (long long)b;
    // currsum += (long long)cb * (long long)c;
    cout << currsum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

#define ll long long

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;

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

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

    // int cc = m[c];
    if (m.count(c) == 0) {
      m[c] = 0;
    }
    if (m.count(b) == 0) {
      m[b] = 0;
    }
    int cb = m[b];
    m[c] += cb;
    m[b] -= cb;
    long long sum = 0;
    for (auto it = m.begin(); it != m.end(); ++it) {
      sum += (long long)it->first * (long long)it->second;
    }
    cout << sum << endl;
  }

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

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

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

Error: 0

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N), memo(100000);
  int64_t ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    memo.at(A.at(i))++;
    ans += A.at(i);
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int64_t B, C;
    cin >> B >> C;
    ans += memo.at(B) * (C - B);
    memo.at(C) += memo.at(B);
    memo.at(B) = 0;
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define vint vector<int>

int main() {
  int n, q;
  cin >> n;
  vint a(n);
  REP(i, n) cin >> a[i];
  cin >> q;
  vector<vector<int>> b(q, vector<int>(2));
  REP(i, q) { cin >> b[i][0] >> b[i][1]; }

  sort(a.begin(), a.end());
  vector<long long> h((int)(1e5), 0);

  long long s = 0;
  REP(i, n) {
    h[a[i] - 1]++;
    s += a[i];
  }

  REP(i, q) {
    s += (b[i][1] - b[i][0]) * h[b[i][0] - 1];
    h[b[i][1] - 1] += h[b[i][0] - 1];
    h[b[i][0] - 1] = 0;
    cout << s << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define vint vector<int>

int main() {
  int n, q;
  cin >> n;
  vint a(n);
  REP(i, n) cin >> a[i];
  cin >> q;
  vector<vector<int>> b(q, vector<int>(2));
  REP(i, q) { cin >> b[i][0] >> b[i][1]; }

  sort(a.begin(), a.end());
  vector<long long> h(a[a.size() - 1], 0);

  long long s = 0;
  REP(i, n) {
    h[a[i] - 1]++;
    s += a[i];
  }

  REP(i, q) {
    s += (b[i][1] - b[i][0]) * h[b[i][0] - 1];
    h[b[i][1] - 1] += h[b[i][0] - 1];
    h[b[i][0] - 1] = 0;
    cout << s << endl;
  }

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

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#define int long long
  int N, B, C, Q;
  long long s = 0;
  cin >> N;
  vector<int> v(N), count_vec(1e6);
  for (int i = 0; i < 1e6; ++i)
    count_vec[i] = 0;

  for (int i = 0; i < N; ++i) {
    cin >> v[i];
    s += v[i];
    count_vec[v[i] - 1]++;
  }

  cin >> Q;
  while (Q--) {
    cin >> B >> C;
    if (count_vec[B - 1] != 0) {
      s = s - (count_vec[B - 1] * B) + (count_vec[B - 1] * C);
      count_vec[C - 1] += count_vec[B - 1];
      count_vec[B - 1] = 0;
    }
    cout << s << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

  int N, B, C, Q;
  long long s = 0;
  cin >> N;
  vector<int> v(N), count_vec(N);
  for (int i = 0; i < N; ++i)
    count_vec[i] = 0;

  for (int i = 0; i < N; ++i) {
    cin >> v[i];
    s += v[i];
    count_vec[v[i] - 1]++;
  }

  /*cout << "count_vec is: " << endl;
  for (int i = 0; i < N; ++i)
      cout << count_vec[i] << " ";
  cout << endl;*/
  cin >> Q;
  while (Q--) {
    cin >> B >> C;
    if (count_vec[B - 1] != 0) {
      s = s - (count_vec[B - 1] * B) + (count_vec[B - 1] * C);
      count_vec[C - 1] += count_vec[B - 1];
      count_vec[B - 1] = 0;
    }
    cout << s << endl;
  }

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

#include <bits/stdc++.h>

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define ll long long
#define pii pair<int, int>
#define all(x) begin(x), end(x)
#define loop(i, n) for (int i = 0; i < n; i++)
#define rep(i, a, b, c) for (int i = a; i < b; i += c)
#define brep(i, a, b, c) for (int i = a; i > b; i -= c)
#define tc(t)                                                                  \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define sz(v) int((v).size())
#define pb push_back

const int N = 2e5 + 10;

using namespace std;

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  IOS;

  ll n;
  cin >> n;

  ll a[n];

  ll cnt[100010] = {};

  ll sum = 0;

  loop(i, n) {
    cin >> a[i];

    cnt[a[i]]++;

    sum += a[i];
  }

  ll q;
  cin >> q;

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

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

    cnt[c] += cnt[b];

    cnt[b] = 0;

    cout << sum << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define ll long long
#define pii pair<int, int>
#define all(x) begin(x), end(x)
#define loop(i, n) for (int i = 0; i < n; i++)
#define rep(i, a, b, c) for (int i = a; i < b; i += c)
#define brep(i, a, b, c) for (int i = a; i > b; i -= c)
#define tc(t)                                                                  \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define sz(v) int((v).size())
#define pb push_back

const int N = 2e5 + 10;

using namespace std;

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  IOS;

  ll n;
  cin >> n;

  ll a[n];

  loop(i, n) { cin >> a[i]; }

  ll q;
  cin >> q;

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

    ll sum = 0;

    loop(i, n) {

      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;

#define froop1(a, n) for (int i = a; i < n; i++)
#define froop2(a, n) for (int i = a; i > n; i--)
int main() {
  long long N;
  cin >> N;
  map<long long, long long> a;
  froop1(0, N) {
    int c;
    cin >> c;
    if (a.count(c)) {
      a[c]++;
    } else {
      a[c] = 1;
    }
  }
  long long M;
  cin >> M;
  long long sum = 0;
  for (pair<long long, long long> p : a) {
    long long p1 = p.first;
    long long p2 = p.second;
    sum += p1 * p2;
  }
  froop1(0, M) {
    long long d, e;
    cin >> d >> e;
    if (a.count(d)) {
      if (a.count(e)) {
        sum += (e - d) * a[d];
        a[e] += a[d];
        a.erase(d);
      } else {
        sum += (e - d) * a[d];
        a[e] = a[d];
        a.erase(d);
      }
    }
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

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

#define froop1(a, n) for (int i = a; i < n; i++)
#define froop2(a, n) for (int i = a; i > n; i--)
int main() {
  long long N;
  cin >> N;
  map<long long, long long> a;
  froop1(0, N) {
    int c;
    cin >> c;
    if (a.count(c)) {
      a[c]++;
    } else {
      a[c] = 1;
    }
  }
  long long M;
  cin >> M;
  froop1(0, M) {
    long long sum = 0, d, e;
    cin >> d >> e;
    if (a.count(d)) {
      if (a.count(e)) {
        a[e] += a[d];
        a.erase(d);
      } else {
        a[e] = a[d];
        a.erase(d);
      }
    }
    for (pair<long long, long long> p : a) {
      long long p1 = p.first;
      long long p2 = p.second;
      sum += p1 * p2;
    }
    cout << sum << endl;
  }
}