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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
using ll = long long;

int main(void) {
  int N;
  cin >> N;
  ll sum = 0;
  vector<int> A;
  int V[1000000] = {};
  for (int i = 0; i < N; i++) {
    int temp;
    cin >> temp;
    sum += temp;
    A.push_back(temp);
    V[temp] += 1;
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    ll diff = C - B;
    sum += V[B] * diff;
    int x = V[B];
    V[B] = 0;
    V[C] += x;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

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

int main(void) {
  int N;
  cin >> N;
  vector<int> A;
  for (int i = 0; i < N; i++) {
    int temp;
    cin >> temp;
    A.push_back(temp);
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    int sum = 0;
    for (int j = 0; j < N; j++) {
      if (A[j] == B)
        A[j] = C;
      sum += A[j];
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main() {
  ios_base::sync_with_stdio(0);
  ll n, q, b, c, sum = 0;
  cin >> n;
  ll arr[n];
  map<ll, ll> m;
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
    m[arr[i]]++;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    sum += (c - b) * m[b];
    m[c] += m[b];
    m[b] = 0;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

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

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL << 60;
const int IINF = 1000000000;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A[i];

  int Q;
  cin >> Q;
  vector<int> B(Q), C(Q);
  rep(i, Q) cin >> B[i] >> C[i];

  map<int, ll> counter;
  ll total = 0;
  rep(i, N) {
    counter[A[i]]++;
    total += A[i];
  }

  rep(i, Q) {
    ll b_count = counter[B[i]];
    counter[B[i]] = 0;
    counter[C[i]] += b_count;
    total -= b_count * B[i];
    total += b_count * C[i];

    cout << total << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL << 60;
const int IINF = 1000000000;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A[i];

  int Q;
  cin >> Q;
  vector<int> B(Q), C(Q);
  rep(i, Q) cin >> B[i] >> C[i];

  map<int, ll> counter;
  rep(i, N) { counter[A[i]]++; }

  rep(i, Q) {
    ll b_count = counter[B[i]];
    counter[B[i]] = 0;
    counter[C[i]] += b_count;

    ll ans = 0;
    for (auto c : counter) {
      ans += (c.first * c.second);
    }
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL << 60;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;

typedef pair<int, int> P;
typedef priority_queue<int, vector<int>, greater<int>> PQ;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  ll Q;
  cin >> Q;
  vector<ll> B(Q), C(Q);
  rep(i, Q) cin >> B[i] >> C[i];

  map<ll, ll> d;
  ll sum = 0;
  rep(i, N) {
    d[A[i]]++;
    sum += A[i];
  }

  rep(i, Q) {

    sum += d[B[i]] * C[i];
    sum -= B[i] * d[B[i]];
    d[C[i]] += d[B[i]];
    d[B[i]] = 0;

    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL << 60;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;

typedef pair<int, int> P;
typedef priority_queue<int, vector<int>, greater<int>> PQ;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  ll Q;
  cin >> Q;
  vector<ll> B(Q), C(Q);
  rep(i, Q) cin >> B[i] >> C[i];

  map<ll, ll> d;
  rep(i, N) { d[A[i]]++; }

  rep(i, Q) {
    d[C[i]] += d[B[i]];
    d[B[i]] = 0;

    ll ans = 0;
    for (auto x : d) {
      ans += x.first * x.second;
    }

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

#include <algorithm>
#include <cstdint>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

ll ans, mx, sum, mn = 1e8, cnt;

ll d[100001];
int main() {
  ll n;
  cin >> n;
  ll a[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    d[a[i]]++;
    sum += a[i];
  }
  ll q;
  cin >> q;
  ll b, c;
  for (ll i = 0; i < q; i++) {
    cin >> b >> c;
    if (d[b] > 0) {
      ll flag = d[b] * b;
      sum -= flag;
      d[c] += d[b];
      sum += c * d[b];
      d[b] = 0;
    }
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdint>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

ll ans, mx, sum, mn = 1e8, cnt;

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

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

#define u unsigned
#define s short
#define ll long long
#define REP(i, a, b) for (auto i = (a); i < (b); i++)
#define REPR(i, a, b) for (auto i = (a - 1); i >= (b); i--)

using namespace std;

int main() {
  vector<int> A(100000);

  u int N;
  cin >> N;

  ll int sum = 0;
  REP(i, 0, N) {
    u int a;
    cin >> a;
    sum += a;
    A[a - 1]++;
  }

  // for (auto it = vect.begin(); it != vect.end(); it++)
  //     cin >> *it;

  u int Q;
  cin >> Q;

  REP(i, 0, Q) {
    int B, C;
    cin >> B >> C;

    sum = sum + (A[B - 1] * (C - B));
    cout << sum << "\\n";

    A[C - 1] += A[B - 1];
    A[B - 1] = 0;
  }
}

Error: TLE

Fixed submission:

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

#define u unsigned
#define s short
#define ll long long
#define REP(i, a, b) for (auto i = (a); i < (b); i++)
#define REPR(i, a, b) for (auto i = (a - 1); i >= (b); i--)

using namespace std;

int main() {
  u int N;
  cin >> N;
  vector<u int> vect(N);

  for (auto it = vect.begin(); it != vect.end(); it++)
    cin >> *it;

  u int Q;
  cin >> Q;

  REP(i, 0, Q) {
    u int B, C;
    cin >> B >> C;
    replace(vect.begin(), vect.end(), B, C);
    cout << accumulate(vect.begin(), vect.end(), 0) << "\\n";
  }
}
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;
const int INF = 1e9;

int main() {
  ll n, ans = 0, a, b, c, q;
  cin >> n;
  unordered_map<ll, ll> mp;
  rep(i, n) cin >> a, mp[a]++;
  cin >> q;

  for (auto m : mp)
    ans += m.second * m.first;

  rep(i, q) {
    cin >> b >> c;
    mp[c] += mp[b];
    ans += (c - b) * mp[b];
    mp.erase(b);
    cout << ans << 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;
const int INF = 1e9;

int main() {
  ll n, ans = 0, a, b, c, q;
  cin >> n;
  unordered_map<ll, ll> mp;
  rep(i, n) cin >> a, mp[a]++;
  cin >> q;

  rep(i, q) {
    cin >> b >> c;
    mp[c] += mp[b];
    mp.erase(b);
    ans = 0;
    for (auto m : mp)
      ans += m.second * m.first;
    cout << ans << endl;
  }

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

#include <iostream>
#include <vector>
using namespace std;
const int MAX = 100005;

int main() {
  int N;
  cin >> N;
  vector<int> A(MAX);
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    A[x]++;
  }

  int Q;
  cin >> Q;
  long long ans = 0;
  for (int i = 0; i < MAX; i++) {
    ans += i * A[i];
  }

  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    ans += (C - B) * A[B];
    A[C] += A[B];
    A[B] = 0;
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
const int MAX = 100005;

int main() {
  int N;
  cin >> N;
  vector<int> A(MAX);
  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;
    long long ans = 0;
    for (int j = 0; j < MAX; j++) {
      ans += (long long)A[j] * j;
    }
    cout << ans << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
// freopen("input.txt" , "r" , stdin);
// freopen("output.txt" , "w" , stdout);
#define ll long long
#define endl '\\n'
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REP1(i1, n) for (ll i = 1; i < n; i++)
#define pb push_back
#define mk make_pair
int main() {
  IOS int n;
  cin >> n;
  int a[n];
  ll sum = 0;
  map<ll, ll> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    m[a[i]]++;
  }
  ll t, b, c;
  cin >> t;
  while (t--) {
    cin >> b >> c;
    ll timesb = m[b];
    ll timesc = m[c];
    sum -= (b * timesb);
    m[c] += timesb;
    sum += (timesb * c);
    m[b] = 0;
    cout << sum << "\\n";
  }
}
// for(ll i=0;i<len;i++)cout<<v[i]<<" ";

/*

Alhamdulilla...

*/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// freopen("input.txt" , "r" , stdin);
// freopen("output.txt" , "w" , stdout);
#define ll long long
#define endl '\\n'
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REP1(i1, n) for (ll i = 1; i < n; i++)
#define pb push_back
#define mk make_pair
int main() {
  IOS int n;
  scanf("%d", &n);
  int a[n];
  int sum = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
  }
  int t;
  scanf("%d", &t);
  while (t--) {
    int c, b;
    scanf("%d%d", &c, &b);
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] == c) {
        cnt++;
        a[i] = b;
      }
    }
    sum -= cnt * c;
    sum += b * cnt;
    printf("%d\\n", sum);
  }
}
// for(ll i=0;i<len;i++)cout<<v[i]<<" ";

/*

Alhamdulilla...

*/
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const llint INF = 1<<21;
// static const llint MOD = 1e9 + 7;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

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

  vector<llint> a(100005);
  llint atmp, ans = 0;
  for (int in = 0; in < n; in++) {
    cin >> atmp;
    atmp--;
    a.at(atmp)++;
    ans += atmp + 1;
  }

  llint b, c, q;
  cin >> q;
  for (int iq = 0; iq < q; iq++) {
    cin >> b >> c;
    b--;
    c--;
    ans += (c - b) * a.at(b);
    a.at(c) += a.at(b);
    a.at(b) = 0;
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const llint INF = 1<<21;
// static const llint MOD = 1e9 + 7;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

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

  vector<int> a(10005);
  int atmp, ans = 0;
  for (int in = 0; in < n; in++) {
    cin >> atmp;
    atmp--;
    a.at(atmp)++;
    ans += atmp + 1;
  }

  int b, c, q;
  cin >> q;
  for (int iq = 0; iq < q; iq++) {
    cin >> b >> c;
    b--;
    c--;
    ans += (c - b) * a.at(b);
    a.at(c) += a.at(b);
    a.at(b) = 0;
    cout << ans << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define mod 1000000007
#define pi 3.141592653589793
typedef vector<ll> vcl;

ll gcd(ll a, ll b) {
  if (b > a)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
ll rep_jijo(ll n, ll x) {
  if (x == 0)
    return 1;
  if (x % 2 == 0) {
    ll t = rep_jijo(n, x / 2);
    return t * t % mod;
  }
  return n * rep_jijo(n, x - 1) % mod;
}
ll c2(ll n) { return n * (n - 1) / 2; }

int main() {
  ll n, q;
  cin >> n;
  ll ans = 0;
  map<ll, ll> m;
  rep(i, n) {
    ll k;
    cin >> k;
    m[k]++;
    ans += k;
  }
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    if (m[b] > 0) {
      ans = ans - b * m[b] + m[b] * c;
      m[c] += m[b];
      m[b] = 0;
    }

    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define mod 1000000007
#define pi 3.141592653589793
typedef vector<ll> vcl;

ll gcd(ll a, ll b) {
  if (b > a)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
ll rep_jijo(ll n, ll x) {
  if (x == 0)
    return 1;
  if (x % 2 == 0) {
    ll t = rep_jijo(n, x / 2);
    return t * t % mod;
  }
  return n * rep_jijo(n, x - 1) % mod;
}
ll c2(ll n) { return n * (n - 1) / 2; }

int main() {
  ll n, q;
  cin >> n;
  map<ll, ll> m;
  rep(i, n) {
    ll k;
    cin >> k;
    m[k]++;
  }
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    if (m[b] > 0) {
      m[c] += m[b];
      m[b] = 0;
    }
    ll ans = 0;
    for (auto p : m) {
      auto k = p.first;
      auto v = p.second;
      ans += k * v;
    }
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N;
  cin >> N;
  map<int, ll> A;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    auto it = A.find(a);
    if (it == A.end()) {
      A.emplace(a, 1);
    } else {
      (it->second)++;
    }
  }
  ll S = 0;
  for (auto p : A) {
    S += p.first * p.second;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    if (A.find(B) != A.end()) {
      S += (C - B) * A.at(B);
      if (A.find(C) == A.end()) {
        A.emplace(C, A.at(B));
      } else {
        A.at(C) += A.at(B);
      }
      A.erase(B);
    }
    cout << S << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  map<int, ll> A;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    auto it = A.find(a);
    if (it == A.end()) {
      A.emplace(a, 1);
    } else {
      (it->second)++;
    }
  }
  ll S = 0;
  for (auto p : A) {
    S += p.first * p.second;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    if (A.find(B) != A.end()) {
      S += (C - B) * A.at(B);
      A.at(C) += A.at(B);
      A.erase(B);
    }
    cout << S << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

// #include<iostream>
// #include<algorithm>
// #include<string>
// #include <cmath>
// #include <vector>
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define REP(x, n) for (int x = 0; x < n; x++)

// vector//
#define vei vector<int>
#define velli vector<long long int>
#define pb(x) push_back(x)            // 末尾にxを加える
#define pb2 pop_back()                // 末尾削除
#define all(x) (x).begin(), (x).end() // イテレーター
#define be(x) (x).begin()
// auto  auto begin = v.begin()とか

typedef pair<int, int> P; // q(p.first,p.second)

// バケット使った問題
const ll int MAX = 100010;

template <class T> T Sum(vector<T> t) {
  T sum = 0;
  ll int t_size = t.size();
  for (ll int i = 0; i < t_size; i++) {
    sum += i * t[i];
  }
  return sum;
}

// 0で初期化 バケット
vector<ll int> bucket(MAX, 0);

int main() {
  ll int n;
  cin >> n;
  vector<ll int> a(n);
  ll int ans = 0;
  for (ll int i = 0; i < n; i++) {
    cin >> a[i];
    bucket[a[i]]++;
    ans += a[i];
  }

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

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

// for(int i=0; i<q; i++){
//     // sort(a.begin(),a.end());
//     // auto pos1 = lower_bound(a.begin(),a.end(),b[i]);
//     // auto pos2 = upper_bound(a.begin(),a.end(),b[i]);
//     // replace(pos1,pos2,b[i],c[i]);
//     replace(a.begin(),b.end(),b[i],c[i]);
//     cout << Sum<int>(a) << endl;
// }

Error: TLE

Fixed submission:

// #include<iostream>
// #include<algorithm>
// #include<string>
// #include <cmath>
// #include <vector>
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define REP(x, n) for (int x = 0; x < n; x++)

// vector//
#define vei vector<int>
#define velli vector<long long int>
#define pb(x) push_back(x)            // 末尾にxを加える
#define pb2 pop_back()                // 末尾削除
#define all(x) (x).begin(), (x).end() // イテレーター
#define be(x) (x).begin()
// auto  auto begin = v.begin()とか

typedef pair<int, int> P; // q(p.first,p.second)

const int MAX = 100010;

template <class T> T Sum(vector<T> t) {
  T sum = 0;
  int t_size = t.size();
  for (int i = 0; i < t_size; i++) {
    sum += i * t[i];
  }
  return sum;
}

// 0で初期化 バケット
vector<int> bucket(MAX, 0);

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

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

  for (int i = 0; i < q; i++) {
    bucket[c[i]] += bucket[b[i]];
    bucket[b[i]] = 0;
    cout << Sum<int>(bucket) << endl;
  }
}

// for(int i=0; i<q; i++){
//     // sort(a.begin(),a.end());
//     // auto pos1 = lower_bound(a.begin(),a.end(),b[i]);
//     // auto pos2 = upper_bound(a.begin(),a.end(),b[i]);
//     // replace(pos1,pos2,b[i],c[i]);
//     replace(a.begin(),b.end(),b[i],c[i]);
//     cout << Sum<int>(a) << endl;
// }
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  ll hist[100001] = {0};
  ll summ = 0;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    summ += t;
    hist[t]++;
  }
  ll q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    ll b, c;
    cin >> b >> c;
    if (b != c) {
      summ -= hist[b] * b;
      summ += hist[b] * c;
      hist[c] += hist[b];
      hist[b] = 0;
    }
    cout << summ << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {

  // ios::sync_with_stdio(0);
  // cin.tie(0);

  int n;
  cin >> n;
  int hist[10001] = {0};
  int summ = 0;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    summ += t;
    hist[t]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    if (b != c) {
      summ -= hist[b] * b;
      summ += hist[b] * c;
      hist[c] += hist[b];
      hist[b] = 0;
    }
    cout << summ << endl;
  }

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

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

int main() {
  int n;
  cin >> n;
  map<int, int> A;
  long long int sum = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    A[x]++;
    sum += x;
  }

  int q;
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    if (A.count(x)) {
      sum -= 1LL * A[x] * x;
      sum += 1LL * A[x] * y;
      A[y] += A[x];
      A.erase(x);
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; i++)
    cin >> A[i];
  int q;
  cin >> q;
  long long int sum = 0;
  for (int i = 0; i < n; i++)
    sum += A[i];

  while (q--) {
    int x, y, c = 0;
    cin >> x >> y;
    for (int i = 0; i < n; i++) {
      if (A[i] == x) {
        A[i] = y;
        c++;
      }
    }
    sum += c * (y - x);
    cout << sum << endl;
  }
  return 0;
}