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

#include <iostream>
#define rep(i, n) for (long i = 0; i < (n); i++)
using namespace std;

int main() {
  long n, q, num[100010], sum = 0;
  rep(i, 100010) { num[i] = 0; }
  cin >> n;
  long long a[n];
  rep(i, n) {
    cin >> a[i];
    num[a[i]]++;
    sum += a[i];
  }
  cin >> q;
  long long b[q], c[q];
  rep(i, q) { cin >> b[i] >> c[i]; }

  rep(i, q) {
    num[c[i]] += num[b[i]];
    sum = sum + ((c[i] - b[i]) * num[b[i]]);
    num[b[i]] = 0;
    cout << sum << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#define rep(i, n) for (long i = 0; i < (n); i++)
using namespace std;

int main() {
  long n, q;
  cin >> n;
  long long a[n];
  rep(i, n) { cin >> a[i]; }
  cin >> q;
  long long b[q], c[q], sum[q + 1];
  rep(i, q) { cin >> b[i] >> c[i]; }
  sum[0] = 0;
  rep(i, n) { sum[0] += a[i]; }

  rep(i, q) {
    long count = 0;
    rep(j, n) {
      if (a[j] == b[i]) {
        count++;
        a[j] = c[i];
      }
    }
    sum[i + 1] = sum[i] + (count * (c[i] - b[i]));
    cout << sum[i + 1] << endl;
  }

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

// #include<bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <vector>

#define ll long long
#define uint unsigned int
#define ld long double
#define SAM                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RFOR(i, a, b) for (int i = a; i > b; --i)
#define mod 1000000007
#define mxn 200005
#define pb push_back
#define pii pair<int, int>
#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

using namespace std;

int main() {
  SAM;
  int n, q;
  ll sum = 0;
  cin >> n;
  vector<int> v(mxn);

  FOR(i, 0, n) {
    int x;
    cin >> x;
    sum += x;
    v[x]++;
  }

  cin >> q;
  FOR(i, 0, q) {
    int b, c;
    cin >> b >> c;
    sum += v[b] * (c - b);
    v[c] += v[b];
    v[b] = 0;

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ll long long
#define uint unsigned int
#define ld long double
#define SAM                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RFOR(i, a, b) for (int i = a; i > b; --i)
#define mod 1000000007
#define mxn 200005
#define pb push_back
#define pii pair<int, int>
#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

using namespace std;

int main() {
  SAM;
  ll n, q;
  cin >> n;
  vector<int> v;
  FOR(i, 0, n) {
    int x;
    cin >> x;
    v.pb(x);
  }
  sort(all(v));

  cin >> q;
  FOR(i, 0, q) {
    ll b, c;
    ll sum = 0;
    cin >> b >> c;

    vector<int>::iterator it, u;
    it = find(all(v), b);
    if (it != v.end()) {
      while (it != v.end()) {
        if (*it == b)
          *it = c;
        else
          break;
        it++;
      }
    }
    for (u = v.begin(); u != v.end(); u++) {
      sum += *u;
    }
    cout << sum << "\\n";
  }

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

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

const ll P = 1000000007;
const long long INF = 1LL << 60;
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
vector<int> vec(100001, 0);
int main() {
  cout << fixed << setprecision(10);
  int N;
  cin >> N;
  vector<int> A(N);
  ll sum = 0;
  rep(i, N) {
    cin >> A[i];
    sum += A[i];
    vec[A[i]]++;
  }
  int Q;
  cin >> Q;
  vector<int> B(Q), C(Q);
  rep(i, Q) cin >> B[i] >> C[i];
  rep(i, Q) {
    if (C[i] >= B[i]) {
      sum += vec[B[i]] * (C[i] - B[i]);
    } else {
      sum -= vec[B[i]] * (B[i] - C[i]);
    }
    if (B[i] != C[i]) {
      vec[C[i]] += vec[B[i]];
      vec[B[i]] = 0;
    }
    cout << sum << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

const ll P = 1000000007;
const long long INF = 1LL << 60;
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
vector<int> vec(10001, 0);
int main() {
  cout << fixed << setprecision(10);
  int N;
  cin >> N;
  vector<int> A(N);
  ll sum = 0;
  rep(i, N) {
    cin >> A[i];
    sum += A[i];
    vec[A[i]]++;
  }
  int Q;
  cin >> Q;
  vector<int> B(Q), C(Q);
  rep(i, Q) cin >> B[i] >> C[i];
  rep(i, Q) {
    if (C[i] >= B[i]) {
      sum += vec[B[i]] * (C[i] - B[i]);
    } else {
      sum -= vec[B[i]] * (B[i] - C[i]);
    }
    if (B[i] != C[i]) {
      vec[C[i]] += vec[B[i]];
      vec[B[i]] = 0;
    }
    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<long long> num(110000, 0);
  for (int i = 0; i < N; ++i) {
    long long A;
    cin >> A;
    sum += A;
    num[A]++;
  }

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

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;
  int a[100010];
  int q, b, c;
  int ans;

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

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

#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main(void) {
  int N;
  cin >> N;
  map<int, int> A;
  for (int i = 0; i < N; i++) {
    int e;
    cin >> e;
    A[e]++;
  }
  int Q;
  cin >> Q;
  vector<int> B, C;
  B.resize(Q);
  C.resize(Q);
  for (int i = 0; i < Q; i++)
    cin >> B[i] >> C[i];

  long long tmp = 0;
  for (int j = 0; j < A.size(); j++)
    tmp += (long long)j * (long long)A[j];

  for (int i = 0; i < Q; i++) {
    long long delta = 0;
    delta = (long long)A[B[i]] * (C[i] - B[i]);
    A[C[i]] += A[B[i]];
    A[B[i]] = 0;
    tmp += delta;
    cout << tmp << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main(void) {
  int N;
  cin >> N;
  map<int, int> A;
  for (int i = 0; i < N; i++) {
    int e;
    cin >> e;
    A[e]++;
  }
  int Q;
  cin >> Q;
  vector<int> B, C;
  B.resize(Q);
  C.resize(Q);
  for (int i = 0; i < Q; i++)
    cin >> B[i] >> C[i];

  for (int i = 0; i < Q; i++) {
    A[C[i]] += A[B[i]];
    A[B[i]] = 0;
    long long tmp = 0;
    for (int j = 0; j < A.size(); j++)
      tmp += (long long)j * (long long)A[j];
    cout << tmp << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define endl '\\n'
#define mod 1000000007
typedef long long int ll;
using namespace std;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    int n;
    cin >> n;
    vector<int> a(n);
    for (auto &it : a)
      cin >> it;
    int q;
    cin >> q;
    int b, c;
    // ll sum=accumulate(a.begin(),a.end(),0);
    ll sum = 0;
    for (auto i : a)
      sum = (ll)sum + i;
    unordered_map<int, int> mp;
    for (auto i : a)
      mp[i]++;
    for (int i = 0; i < (int)q; i++) {
      cin >> b >> c;
      // replace(a.begin(),a.end(),b,c);
      if (mp.count(b)) {
        // sum=(sum-mp[b]*b+mp[b]*c);
        sum -= 1LL * mp[b] * b;
        sum += 1LL * mp[b] * c;
        mp[c] += mp[b];
        mp[b] -= mp[b];
      }
      cout << (ll)sum << endl;
      // for(auto j : a)
      //   cout<<j<<" ";
      //  cout<<<<endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl '\\n'
#define mod 1000000007
typedef long long int ll;
using namespace std;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    int n;
    cin >> n;
    vector<int> a(n);
    for (auto &it : a)
      cin >> it;
    int q;
    cin >> q;
    int b, c;
    int sum = accumulate(a.begin(), a.end(), 0);
    unordered_map<int, int> mp;
    for (auto i : a)
      mp[i]++;
    for (int i = 0; i < (int)q; i++) {
      cin >> b >> c;
      replace(a.begin(), a.end(), b, c);

      sum = (sum - mp[b] * b + mp[b] * c);
      mp[c] += mp[b];
      mp[b] -= mp[b];
      cout << sum << endl;
      // for(auto j : a)
      //   cout<<j<<" ";
      //  cout<<<<endl;
    }
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <ctime>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using ppi = pair<pii, int>;

int f_dir[2][4] = {{-1, 0, 1, 0}, {0, 1, 0, -1}}; // ↑→↓←
int e_dir[2][8] = {{-1, -1, -1, 0, 0, 1, 1, 1}, {-1, 0, 1, -1, 1, -1, 0, 1}};
bool is_in_field(int y, int x, int h, int w) {
  return (y >= 0 && y < h && x >= 0 && x < w);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int ti = clock();
  // start-----------------------------------------------
  ll n;
  cin >> n;
  vi a(100000, 0);
  ll s = 0;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    a[t - 1]++;
    s += t;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    s += (c - b) * a[b - 1];
    a[c - 1] += a[b - 1];
    a[b - 1] = 0;
    cout << s << endl;
  }

  // end-----------------------------------------------
  // cerr << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <ctime>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using ppi = pair<pii, int>;

int f_dir[2][4] = {{-1, 0, 1, 0}, {0, 1, 0, -1}}; // ↑→↓←
int e_dir[2][8] = {{-1, -1, -1, 0, 0, 1, 1, 1}, {-1, 0, 1, -1, 1, -1, 0, 1}};
bool is_in_field(int y, int x, int h, int w) {
  return (y >= 0 && y < h && x >= 0 && x < w);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int ti = clock();
  // start-----------------------------------------------
  ll n;
  cin >> n;
  vi a(n, 0);
  ll s = 0;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    a[t - 1]++;
    s += t;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    s += (c - b) * a[b - 1];
    a[c - 1] += a[b - 1];
    a[b - 1] = 0;
    cout << s << endl;
  }

  // end-----------------------------------------------
  // cerr << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define s size
#define pb push_back
#define in insert
#define PI 3.1415926535897932384626433832795
#define MOD 100000007
#define F first
#define S second
#define aLL(str) (str).begin(), (str).end()
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define ll long long
typedef vector<ll> vl;
typedef map<ll, ll> ml;
typedef vector<string> vs;
typedef vector<vector<int>> vvi;
int main() {
  IOS;
  ll t;
  // cin>>t;
  t = 1;
  while (t--) {
    ll n, i, q, j;
    cin >> n;
    vl v(n, 0);
    unordered_map<int, int> Mp;
    ll su = 0;
    for (i = 0; i < n; i++)
      cin >> v[i], su += v[i], Mp[v[i]]++;
    cin >> q;
    ll a, b;
    while (q--) {
      cin >> a >> b;
      su += Mp[a] * (b - a);
      Mp[b] += Mp[a];
      Mp.erase(a);
      cout << su << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define s size
#define pb push_back
#define in insert
#define PI 3.1415926535897932384626433832795
#define MOD 100000007
#define F first
#define S second
#define aLL(str) (str).begin(), (str).end()
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define ll long long
typedef vector<ll> vl;
typedef map<ll, ll> ml;
typedef vector<string> vs;
typedef vector<vector<int>> vvi;
int main() {
  IOS;
  ll t;
  // cin>>t;
  t = 1;
  while (t--) {
    ll n, i, q, j;
    cin >> n;
    vl v(n, 0);
    for (i = 0; i < n; i++)
      cin >> v[i];
    cin >> q;
    vl b(q, 0), c(q, 0);
    for (i = 0; i < q; i++)
      cin >> b[i] >> c[i];
    for (i = 0; i < q; i++) {
      ll sum = 0;
      for (j = 0; j < n; j++) {
        if (v[j] == b[i])
          v[j] = c[i];
        sum += v[j];
      }
      cout << sum << endl;
    }
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

typedef pair<int, int> p;
typedef long long ll;

const int mod = 1000000007;
const int inf = 1000000007;

int main() {
  int n, q;
  cin >> n;
  vector<int> a(n);
  vector<int> cnt(100005);
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    cnt[a[i]]++;
    sum += a[i];
  }
  cin >> q;
  vector<ll> bc(q + 1);
  for (int i = 1; i <= q; i++) {
    int b, c;
    cin >> b >> c;
    bc[i] = (c - b) * cnt[b];
    // cout << (c - b) * cnt[b] << '/';
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }
  // cout << endl;
  vector<ll> ans(q);
  ans[0] = bc[1] + sum;
  for (int i = 1; i < q; i++) {
    ans[i] = ans[i - 1] + bc[i + 1];
  }
  rep(i, q) cout << ans[i] << endl;
}


Error: 0

Fixed submission:

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

typedef pair<int, int> p;
typedef long long ll;

const int mod = 1000000007;
const int inf = 1000000007;

int main() {
  int n, q;
  cin >> n;
  vector<int> a(n);
  vector<int> cnt(n + 1);
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    cnt[a[i]]++;
    sum += a[i];
  }
  cin >> q;
  vector<ll> bc(q + 1);
  for (int i = 1; i <= q; i++) {
    int b, c;
    cin >> b >> c;
    bc[i] = (c - b) * cnt[b];
    // cout << (c - b) * cnt[b] << '/';
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }
  // cout << endl;
  vector<ll> ans(q);
  ans[0] = bc[1] + sum;
  for (int i = 1; i < q; i++) {
    ans[i] = ans[i - 1] + bc[i + 1];
  }
  rep(i, q) cout << ans[i] << endl;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;

const int mod = 1e9 + 7;
const ll INF = 1e18;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll N, Q;
  cin >> N;
  unordered_map<ll, int> m;
  ll sum = 0;
  for (int i = 0; i < N; i++) {
    ll temp;
    cin >> temp;
    m[temp]++;
    sum += temp;
  }
  cin >> Q;
  ll B, C;
  for (int i = 0; i < Q; i++) {
    cin >> B >> C;
    sum += C * m[B];
    sum -= B * m[B];
    m[C] += m[B];
    m.erase(B);
    cout << sum << "\\n";
  }
}


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;

const int mod = 1e9 + 7;
const ll INF = 1e18;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll N, Q;
  cin >> N;
  vector<ll> v(N);
  for (auto &i : v) {
    cin >> i;
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    replace(v.begin(), v.end(), B, C);
    cout << accumulate(v.begin(), v.end(), 0) << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
#define int long long
#define mp make_pair
#define pb push_back
#define pi 3.14159265358979323846
#define reus                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
int32_t main() {
  reus;
  int n, q, s = 0;
  cin >> n;
  int x[100002], y[100002] = {0}, i;
  for (i = 1; i <= n; i++) {
    cin >> x[i];
    s += x[i];
    y[x[i]]++;
  }
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    s -= (y[b] * b);
    s += (y[b] * c);
    y[c] += y[b];
    y[b] = 0;
    cout << s << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
#define int long long
#define mp make_pair
#define pb push_back
#define pi 3.14159265358979323846
#define reus                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
int32_t main() {
  reus;
  int n, q, s = 0;
  cin >> n;
  int x[100002], y[100002] = {0}, i;
  for (i = 1; i <= n; i++) {
    cin >> x[i];
    y[x[i]]++;
  }
  cin >> q;
  while (q--) {
    int b, c, ans = 0;
    cin >> b >> c;
    y[c] += y[b];
    y[b] -= y[b];
    for (i = 1; i < 100002; i++) {
      ans += (i * y[i]);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

lint gcd(lint x, lint y) {
  if (x == 0) {
    return y;
  } else {
    return gcd(y % x, x);
  }
}

lint lcm(lint x, lint y) { return x / gcd(x, y) * y; }

lint C(lint n, lint k) {
  if (n == k) {
    return 1;
  } else if (n < k) {
    return 0;
  } else if (k == 0) {
    return 1;
  } else if (k == 1) {
    return n;
  } else
    return C(n - 1, k - 1) + C(n - 1, k);
}

lint P(lint n, lint k) {
  if (k == 1) {
    return n;
  }
  return (n * (P(n - 1, k - 1) % 1000000007) % 1000000007);
}

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

  lint sum = 0;
  vector<lint> d(100000, 0);
  for (int i = 0; i < n; i++) {
    sum += a[i];
    d[a[i] - 1]++;
  }

  for (int i = 0; i < q; i++) {
    sum += (c[i] - b[i]) * d[b[i] - 1];
    d[c[i] - 1] += d[b[i] - 1];
    d[b[i] - 1] = 0;
    cout << sum << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

lint gcd(lint x, lint y) {
  if (x == 0) {
    return y;
  } else {
    return gcd(y % x, x);
  }
}

lint lcm(lint x, lint y) { return x / gcd(x, y) * y; }

lint C(lint n, lint k) {
  if (n == k) {
    return 1;
  } else if (n < k) {
    return 0;
  } else if (k == 0) {
    return 1;
  } else if (k == 1) {
    return n;
  } else
    return C(n - 1, k - 1) + C(n - 1, k);
}

lint P(lint n, lint k) {
  if (k == 1) {
    return n;
  }
  return (n * (P(n - 1, k - 1) % 1000000007) % 1000000007);
}

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

  lint sum = 0;
  vector<lint> d(n, 0);
  for (int i = 0; i < n; i++) {
    sum += a[i];
    d[a[i] - 1]++;
  }

  for (int i = 0; i < q; i++) {
    sum += (c[i] - b[i]) * d[b[i] - 1];
    d[c[i] - 1] += d[b[i] - 1];
    d[b[i] - 1] = 0;
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

Error: TLE

Fixed submission:

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

template <typename T> class SegmentTree {
private:
  using Fn = function<T(T, T)>;
  int N;
  vector<T> dat;
  T unit;
  Fn func;

public:
  SegmentTree() {}
  SegmentTree(int n_, Fn func_, T unit_) : func(func_), unit(unit_) {
    N = 1;
    while (N < n_)
      N *= 2;
    dat.assign(2 * N - 1, unit);
  }
  SegmentTree(vector<T> v, Fn func_, T unit_) : func(func_), unit(unit_) {
    N = 1;
    int sz = v.size();
    while (N < sz)
      N *= 2;
    dat.resize(2 * N - 1);
    for (int i = 0; i < N; ++i)
      dat[i + N - 1] = (i < sz ? v[i] : unit);
    for (int i = N - 2; i >= 0; --i)
      dat[i] = func(dat[i * 2 + 1], dat[i * 2 + 2]);
  }

  // k 番目の要素を a に変更
  void update(int k, T a) {
    k += N - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = func(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  // k 番目の要素を取得
  T get(int k) { return dat[k + N - 1]; }

  // 区間 [l, r) の和を取得
  T query(int l, int r) {
    T vl = unit, vr = unit;
    for (l += (N - 1), r += (N - 1); l < r; l >>= 1, r >>= 1) {
      if ((l & 1) == 0)
        vl = func(vl, dat[l]);
      if ((r & 1) == 0)
        vr = func(vr, dat[--r]);
    }
    return func(vl, vr);
  }
};

int main() {
  int n, q;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  auto f = [](int a, int b) { return a + b; };
  SegmentTree<int> sg(a, f, 0);
  cin >> q;
  rep(qi, q) {
    int x, y;
    cin >> x >> y;
    rep(i, n) {
      if (sg.get(i) == x)
        sg.update(i, y);
    }
    cout << sg.query(0, n) << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
int main() {
  ll n;
  cin >> n;
  map<ll, ll> a;
  ll ans = 0;
  rep(i, n) {
    ll x;
    cin >> x;
    a[x]++;
    ans += x;
  }
  ll q;
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    ans += (c - b) * a[b];
    a[c] += a[b];
    a[b] = 0;
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int q;
  cin >> q;
  auto swap = [&](int b, int c) {
    rep(i, n) {
      if (a[i] == b)
        a[i] = c;
    }
  };
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    swap(b, c);
    long long ans = 0;
    rep(i, n) ans += a[i];
    cout << ans << endl;
  }
}