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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  ll n, q, res = 0;
  unordered_map<ll, ll> mp;
  cin >> n;
  vector<ll> a(n);
  for (auto &x : a) {
    cin >> x;
    mp[x]++;
    res += x;
  }
  cin >> q;
  while (q--) {
    ll b, c;
    cin >> b >> c;
    if (mp.find(b) != mp.end()) {
      res += (c - b) * mp[b];
      mp[c] += mp[b];
      mp.erase(b);
    }
    cout << res << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll sz = 1e5 + 10;
ll bt[sz], arr[sz];
unordered_map<ll, vector<ll>> mp;
ll n, q;

void upd(ll p, ll x) {
  for (ll i = p; i <= n; i += i & -i) {
    // cout << "ff" ;
    // cout << p << " " << i << "\\n";
    bt[i] += x;
  }
}

ll sum(ll p) {
  ll res = 0;
  for (ll i = p; i; i -= i & -i) {
    // cout << "gg" ;
    res += bt[i];
  }
  return res;
}

int main() {
  ll tmp;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> tmp;
    mp[tmp].push_back(i);
    arr[i] = tmp;
    upd(i, tmp);
  }
  // for(ll i = 1; i <= n ;i++) {
  //   cout << bt[i] << " ";
  // }
  // cout<< sum(1) << " " << sum(2) << " ";
  // cout << "\\n";
  cin >> q;
  while (q--) {
    ll b, c;
    cin >> b >> c;
    if (mp.find(b) != mp.end()) {
      for (int idx : mp[b]) {
        upd(idx, c - b);
        mp[c].push_back(idx);
      }
      mp.erase(b);
    }
    cout << sum(n) << "\\n";
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N, Q;
  cin >> N;
  vector<long> freq(100000, 0);
  long sum = 0L;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    freq[a - 1]++;
    sum += a;
  }

  cin >> Q;
  vector<long> B(Q);
  vector<long> C(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> B[i];
    cin >> C[i];
  }

  for (int i = 0; i < Q; ++i) {
    sum -= B[i] * freq[B[i] - 1];
    sum += C[i] * freq[B[i] - 1];
    freq[C[i] - 1] += freq[B[i] - 1];
    freq[B[i] - 1] = 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);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  cin >> Q;
  vector<int> B(Q);
  vector<int> C(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> B[i];
    cin >> C[i];
  }

  for (int i = 0; i < Q; ++i) {
    long sum = 0L;
    for (int j = 0; j < N; ++j) {
      if (A[j] == B[i])
        A[j] = C[i];
      sum += A[j];
    }
    cout << sum << endl;
  }

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

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
using Graph = vector<vector<int>>;

const int MAX = 100005;
int counter[MAX];

int main() {
  int n;
  cin >> n;
  ll a[n];
  ll fsum = 0;
  for (int i = 0; i < MAX; ++i) {
    counter[i] = 0;
  }
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    counter[a[i]]++;
    fsum += a[i];
  }
  int q;
  cin >> q;
  int b[q], c[q];
  for (int i = 0; i < q; ++i) {
    cin >> b[i] >> c[i];
  }
  for (int i = 0; i < q; ++i) {
    ll cur = counter[b[i]];
    counter[b[i]] = 0;
    counter[c[i]] += cur;
    fsum += (ll)((c[i] - b[i]) * cur);
    cout << fsum << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
using Graph = vector<vector<int>>;

const int MAX = 10005;
int counter[MAX];

int main() {
  int n;
  cin >> n;
  ll a[n];
  ll fsum = 0;
  for (int i = 0; i < MAX; ++i) {
    counter[i] = 0;
  }
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    counter[a[i]]++;
    fsum += a[i];
  }
  int q;
  cin >> q;
  int b[q], c[q];
  for (int i = 0; i < q; ++i) {
    cin >> b[i] >> c[i];
  }
  for (int i = 0; i < q; ++i) {
    ll cur = counter[b[i]];
    counter[b[i]] = 0;
    counter[c[i]] += cur;
    fsum += (ll)((c[i] - b[i]) * cur);
    cout << fsum << endl;
  }

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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, s, n) for (int i = s; i < (n); i++)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
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 namespace std;
using ll = long long;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007;
const ll INF = MOD * MOD;
const int inf = (1 << 29);

int main() {

  int N;
  cin >> N;
  vector<int> A(N);
  ll res = 0;
  REP(i, N) {
    cin >> A[i];
    res += A[i];
  }
  int Q;
  cin >> Q;
  vector<int> B(Q), C(Q);
  REP(i, Q) cin >> B[i] >> C[i];

  vector<ll> num(100001, 0);
  for (int i = 0; i < N; i++) {
    num[A[i]]++;
  }

  for (int i = 0; i < Q; i++) {
    res += num[B[i]] * (C[i] - B[i]);
    num[C[i]] += num[B[i]];
    num[B[i]] = 0;
    cout << res << endl;
  }

  // ll res = 0;
  // for(int i = 0; i < N; i++) {
  //     res += A[i];
  // }

  // for(int i = 0; i < Q; i++) {
  //     for(int j = 0; j < N; j++) {
  //         if (A[j] == B[i]) {
  //             A[j] = C[i];
  //             res += C[i] - B[i];
  //         }
  //     }
  //     cout << res << endl;
  // }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, s, n) for (int i = s; i < (n); i++)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
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 namespace std;
using ll = long long;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007;
const ll INF = MOD * MOD;
const int inf = (1 << 29);

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];

  ll res = 0;
  for (int i = 0; i < N; i++) {
    res += A[i];
  }

  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < N; j++) {
      if (A[j] == B[i]) {
        A[j] = C[i];
        res += C[i] - B[i];
      }
    }
    cout << res << 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;
  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A.at(i);
  }
  // sort(A.begin(), A.end());
  int Q;
  cin >> Q;
  vector<int> B(Q), C(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> B.at(i) >> C.at(i);
  }
  /*
  for (int i = 0; i < Q; ++i){
    vector<int>::iterator it_s = lower_bound(A.begin(),A.end(),B.at(i));
    vector<int>::iterator it_e = upper_bound(A.begin(),A.end(),B.at(i));
    for(vector<int>::iterator itr = it_s; itr < it_e; ++itr){
      *itr = C.at(i);
    }
    cout << accumulate(A.begin(), A.end(), 0) << endl;
  }
  */
  vector<long long> count(10 * 10 * 10 * 10 * 10 + 1, 0);
  for (int i = 0; i < N; ++i) {
    ++count.at(A.at(i));
  }
  long long sum = accumulate(A.begin(), A.end(), 0LL);
  for (int i = 0; i < Q; ++i) {
    sum += (long long)C.at(i) * count.at(B.at(i)) -
           (long long)B.at(i) * count.at(B.at(i));
    count.at(C.at(i)) += count.at(B.at(i));
    count.at(B.at(i)) = 0;
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A.at(i);
  }
  // sort(A.begin(), A.end());
  int Q;
  cin >> Q;
  vector<int> B(Q), C(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> B.at(i) >> C.at(i);
  }
  /*
  for (int i = 0; i < Q; ++i){
    vector<int>::iterator it_s = lower_bound(A.begin(),A.end(),B.at(i));
    vector<int>::iterator it_e = upper_bound(A.begin(),A.end(),B.at(i));
    for(vector<int>::iterator itr = it_s; itr < it_e; ++itr){
      *itr = C.at(i);
    }
    cout << accumulate(A.begin(), A.end(), 0) << endl;
  }
  */
  vector<int> count(10 * 10 * 10 * 10 * 10 + 1, 0);
  for (int i = 0; i < A.size(); ++i) {
    ++count.at(A.at(i));
  }
  for (int i = 0; i < Q; ++i) {
    count.at(C.at(i)) += count.at(B.at(i));
    count.at(B.at(i)) = 0;
    int sum = 0;
    for (int j = 0; j < 10 * 10 * 10 * 10 * 10; ++j) {
      sum += j * count.at(j);
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long N;
  cin >> N;
  map<long long, long long> A;
  long long s = 0;
  for (int i = 0; i < N; i++) {
    long long a;
    cin >> a;
    A[a]++;
    s += a;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    long long b, c;
    cin >> b >> c;
    s -= b * A[b];
    s += c * A[b];
    A[c] += A[b];
    A[b] = 0;
    cout << s << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  long long N;
  cin >> N;
  map<long long, long long> A;
  for (int i = 0; i < N; i++) {
    long long a;
    cin >> a;
    A[a]++;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    long long b, c;
    cin >> b >> c;
    A[c] += A[b];
    A[b] = 0;
    A.erase(b);
    long long s = 0;
    for (auto it = A.begin(); it != A.end(); it++) {
      s += (*it).first * (*it).second;
    }
    cout << s << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main() {
  ll n;
  cin >> n;
  ll a[n];
  ll b[100001] = {0};
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[a[i]]++;
    sum += a[i];
  }
  ll m;
  cin >> m;
  while (m--) {
    ll p, q;
    cin >> p >> q;
    ll ans = 0;

    ans = sum;
    ans -= b[p] * p;
    ans += q * b[p];
    b[q] += b[p];
    b[p] = 0;
    sum = ans;
    cout << ans << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main() {
  int n;
  cin >> n;
  int a[n];
  int b[100001] = {0};

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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
int main() {
  ll n, q, b, c;
  ll count;
  ll sum = 0;
  cin >> n;
  ll a[n];
  ll i;
  ll cnt[100001] = {0};
  for (i = 0; i < n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
    sum += a[i];
  }
  cin >> q;
  while (q--) {
    cin >> b >> c;
    count = cnt[b];
    sum = sum - count * b + count * c;
    cout << sum << '\\n';
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
int main() {
  ll n, b, c;
  ll q;
  cin >> n;
  ll a[n];
  ll i, j;
  for (i = 0; i < n; i++)
    cin >> a[i];
  cin >> q;
  ll s = accumulate(a, a + n, 0);
  for (i = 0; i < q; i++) {
    cin >> b >> c;
    for (j = 0; j < n; j++) {
      if (a[j] == b) {
        a[j] = c;
        s = s - b + c;
      }
    }
    cout << s << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

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

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

  map<long long, long long> cnt;
  for (int x : A) {
    if (cnt.count(x)) {
      cnt.at(x)++;
    } else {
      cnt[x] = 1;
    }
  }

  cin >> Q;
  for (long long i = 0; i < Q; i++) {
    long long B, C;
    cin >> B >> C;
    if (cnt.count(B)) {
      tot += (C - B) * cnt.at(B);
      cout << tot << endl;
      long long sz = cnt.at(B);
      cnt[C] += sz;
      cnt.at(B) -= sz;
    } else {
      cout << tot << endl;
      continue;
    }
  }
}


Error: 0

Fixed submission:

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

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

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

  map<long long, long long> cnt;
  for (long long x : A) {
    if (cnt.count(x)) {
      cnt.at(x)++;
    } else {
      cnt[x] = 1;
    }
  }

  cin >> Q;
  for (int i = 0; i < Q; i++) {
    long long B, C;
    cin >> B >> C;
    tot += (C - B) * cnt.at(B);
    cout << tot << endl;
    if (cnt.at(B) > 0) {
      cnt.at(C) += cnt.at(B);
      cnt.at(B) -= cnt.at(C);
    }
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n;
  long long sum = 0;
  cin >> n;
  int arr[1000001] = {};
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    sum += x;
    arr[x]++;
  }
  int k;
  cin >> k;
  for (int i = 0; i < k; i++) {
    int b, c;
    cin >> b >> c;
    sum += arr[b] * (c - b);
    arr[c] += arr[b];
    arr[b] = 0;
    cout << sum << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int arr[100001] = {};
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    arr[x]++;
  }
  int k;
  cin >> k;
  for (int i = 0; i < k; i++) {
    int b, c, sum = 0;
    cin >> b >> c;
    arr[c] += arr[b];
    arr[b] = 0;
    for (int j = 0; j < 100001; j++)
      sum += j * arr[j];
    cout << sum << "\\n";
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long n, q, b, k, count[100001] = {0};
  long sum = 0;
  cin >> n;
  int a[n];
  for (long i = 0; i < n; i++) {
    cin >> a[i];
    sum = sum + a[i];
    count[a[i] - 0]++;
  }
  cin >> k;
  for (long i = 0; i < k; i++) {
    cin >> b >> q;
    sum = sum + count[b] * (q - b);
    count[q] = count[q] + count[b];
    count[b] = 0;
    cout << sum << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, q, sum = 0, b, k, count[10000] = {0};
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum = sum + a[i];
    count[a[i] - 0]++;
  }
  cin >> k;
  for (int i = 0; i < k; i++) {
    cin >> b >> q;
    sum = sum + count[b] * (q - b);
    count[q] = count[q] + count[b];
    count[b] = 0;
    cout << sum << "\\n";
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  vector<ll> num(100001, 0);
  ll su = 0;
  for (int i = 0; i < n; i++) {
    num[a[i]]++;
    su += a[i];
  }

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


Error: TLE

Fixed submission:

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

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

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

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

int main() {
  int n;
  cin >> n;
  int a[n];
  rep(i, n) cin >> a[i];
  int q;
  cin >> q;
  int b[q], c[q];
  rep(i, q) cin >> b[i] >> c[i];
  map<int, int> count;
  rep(i, n) count[a[i]]++;
  ll sum = 0;
  for (auto v : count)
    sum += (ll)v.first * v.second;
  rep(i, q) {
    if (count[b[i]] != 0) {
      int tmp = count[b[i]];
      count[c[i]] += tmp;
      count[b[i]] = 0;
      sum += (ll)(c[i] - b[i]) * tmp;
    }
    cout << sum << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  int a[n];
  rep(i, n) cin >> a[i];
  int q;
  cin >> q;
  int b[n], c[n];
  rep(i, q) cin >> b[i] >> c[i];
  map<int, int> count;
  rep(i, n) count[a[i]]++;
  ll sum = 0;
  for (auto v : count)
    sum += (ll)v.first * v.second;
  rep(i, q) {
    if (count[b[i]] != 0) {
      int tmp = count[b[i]];
      count[c[i]] += tmp;
      count[b[i]] = 0;
      sum += (ll)(c[i] - b[i]) * tmp;
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(i, a, b) for (int i = a; i < (b); i++)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vpii vector<pii>
#define F first
#define S second

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

const int M = 1e9 + 7;

/*
ll powermod(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p; // Update x if it is more than or
                // equal to p

    if (x == 0) return 0; // In case x is divisible by p;

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;

        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
*/

void solve() {

  int n;
  cin >> n;

  int a;

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

  int x, y;

  for (int i = 0; i < q; i++) {
    cin >> x >> y;

    if (m.find(x) != m.end()) {
      auto itr = m.find(x);

      if (itr->first < y)

        sum += itr->second * (y - itr->first);

      else

        sum -= itr->second * (itr->first - y);

      m[y] += itr->second;

      m.erase(x);
    }
    cout << sum << "\\n";
  }
}

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

  int test = 1;
  // cin>>test;
  while (test--) {
    solve();
    cout << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, a, b) for (int i = a; i < (b); i++)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vpii vector<pii>
#define F first
#define S second

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

const int M = 1e9 + 7;

/*
ll powermod(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p; // Update x if it is more than or
                // equal to p

    if (x == 0) return 0; // In case x is divisible by p;

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;

        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
*/

void solve() {

  int n;
  cin >> n;

  int arr[n + 1];

  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int q;
  cin >> q;
  int a, b;
  ll sum;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    sum = 0;
    for (int i = 0; i < n; i++) {
      if (arr[i] == a) {
        arr[i] = b;
      }
      sum += arr[i];
    }

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

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

  int test = 1;
  // cin>>test;
  while (test--) {
    solve();
    cout << "\\n";
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

typedef long long int ll;

int main() {
  ll cmp[100005] = {0};
  ll n;
  cin >> n;
  ll a, ans = 0;
  for (ll i = 1; i <= n; ++i) {
    cin >> a;
    ans += a;
    ++cmp[a];
  }

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

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
using namespace std;

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

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

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

Error: TLE

Fixed submission:

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

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

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

#include <iostream>
#include <map>
using namespace std;
int main() {
  long long n, m, tong = 0;
  map<long long, long long> a;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a[x]++;
    tong += x;
  }
  cin >> m;
  while (m--) {
    int c, b;
    cin >> b >> c;
    tong -= a[b] * b;
    tong += a[b] * c;
    a[c] += a[b];
    a[b] = 0;
    cout << tong << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n1, n2, a[100000];
  cin >> n1;
  for (int i = 0; i < n1; i++)
    cin >> a[i];
  cin >> n2;
  for (int j = 0; j < n2; j++) {
    int x, y, tong = 0;
    cin >> x >> y;
    for (int i = 0; i < n1; i++)
      if (a[i] == x) {
        tong += y;
        a[i] = y;
      } else
        tong += a[i];
    cout << tong << endl;
  }
}