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

#include <iostream>
using namespace std;
int main() {
  long long int n, q, i, j, k, tmp, sum = 0;
  int index[100001] = {0};
  cin >> n;
  long long int a[n];

  for (i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    index[a[i]]++;
  }

  cin >> q;
  int b[q][2];

  for (i = 0; i < q; i++)
    for (j = 0; j < 2; j++)
      cin >> b[i][j];

  for (i = 0; i < q; i++) {
    tmp = index[b[i][0]];
    if (tmp) {
      sum = sum - (tmp * (b[i][0]));
      index[b[i][0]] = 0;
      sum = sum + (tmp * (b[i][1]));
      index[b[i][1]] += tmp;
    }
    cout << sum << "\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  long long int n, q, i, j, k, sum = 0;

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

  cin >> q;
  long long int b[q][2];
  for (i = 0; i < q; i++)
    for (j = 0; j < 2; j++)
      cin >> b[i][j];

  for (i = 0; i < q; i++) {
    sort(a, a + n);
    if (binary_search(a, a + n, b[i][0])) {
      for (k = 0; k < n; k++) {
        if (a[k] == b[i][0]) {
          sum -= b[i][0];
          a[k] = b[i][1];
          sum += b[i][1];
        }
      }
    }
    cout << sum << endl;
  }

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

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

#define PI acos(-1)
#define pb push_back
#define int long long int
#define mp make_pair
#define pi pair<int, int>
#define pii pair<int, pi>
#define fir first
#define sec second
#define MAXN 100001
#define MAXL 20
#define mod 998244353
#define INF 9999999

int n;
vector<int> m[MAXN];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  vector<int> v(n);
  int curr = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    curr += v[i];
    m[v[i]].pb(i);
  }
  int q, l = 0, r = n - 1;
  cin >> q;
  while (q--) {
    int c, b;
    cin >> b >> c;
    for (auto const &i : m[b]) {
      curr += (c - b);
      m[c].pb(i);
    }
    m[b].clear();
    cout << curr << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define PI acos(-1)
#define pb push_back
#define int long long int
#define mp make_pair
#define pi pair<int, int>
#define pii pair<int, pi>
#define fir first
#define sec second
#define MAXN 100001
#define MAXL 20
#define mod 998244353
#define INF 9999999

int n;
vector<int> bit;
vector<int> m[MAXN];

int sum(int r) {
  int ret = 0;
  for (; r >= 0; r = (r & (r + 1)) - 1)
    ret += bit[r];
  return ret;
}
void add(int idx, int delta) {
  for (; idx < n; idx = idx | (idx + 1))
    bit[idx] += delta;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  bit.assign(n, 0);
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    add(i, v[i]);
    m[v[i]].pb(i);
  }
  int q, l = 0, r = n - 1;
  cin >> q;
  while (q--) {
    int c, b;
    cin >> b >> c;
    for (auto const &i : m[b]) {
      add(i, c - b);
      m[c].pb(i);
    }
    m[b].clear();
    cout << (sum(r) - sum(l - 1)) << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <iterator>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;

int main() {
  int N;
  long long s = 0;
  cin >> N;
  vector<int> A(N);
  vector<int> ap(10 * 10 * 10 * 10 * 10 + 1);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    s += A.at(i);
    ap.at(A.at(i))++;
  }

  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++) {
    s += (long long)ap.at(b.at(i)) * ((long long)c.at(i) - (long long)b.at(i));
    ap.at(c.at(i)) += ap.at(b.at(i));
    ap.at(b.at(i)) = 0;
    cout << s << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;

int main() {
  int N;
  long long s = 0;
  cin >> N;
  vector<int> A(N);
  vector<int> ap(10 * 10 * 10 * 10 * 10);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    s += A.at(i);
    ap.at(A.at(i))++;
  }

  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++) {
    s += (long long)ap.at(b.at(i)) * ((long long)c.at(i) - (long long)b.at(i));
    ap.at(c.at(i)) += ap.at(b.at(i));
    ap.at(b.at(i)) = 0;
    cout << s << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long long N;
  cin >> N;
  vector<long long> A(N, 0);
  map<int, long long> mp;
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    mp[A[i]]++;
    ans += A[i];
  }
  long long Q;
  cin >> Q;
  vector<long long> B(Q);
  vector<long long> C(Q);
  for (int i = 0; i < Q; i++) {
    cin >> B[i] >> C[i];
    long long cont = mp[B[i]];
    mp[C[i]] += mp[B[i]];
    mp[B[i]] = 0;
    ans += (C[i] - B[i]) * cont;
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N, 0);
  map<int, int> mp;
  int ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    mp[A[i]]++;
    ans += A[i];
  }
  int Q;
  cin >> Q;
  vector<int> B(N);
  vector<int> C(N);
  for (int i = 0; i < Q; i++) {
    cin >> B[i] >> C[i];
    int cont = mp[B[i]];
    mp[C[i]] += mp[B[i]];
    mp[B[i]] = 0;
    ans += (C[i] - B[i]) * cont;
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;
using ll = long long;
const long long INF = 1LL << 60;

typedef pair<ll, ll> Pair;
int main() {

  ll N, Q;
  ll sum = 0;

  cin >> N;

  vector<ll> A(N);
  vector<ll> cnt(100001, 0);

  for (ll i = 0; i < N; ++i) {
    cin >> A[i];
    sum += A[i];
    ++cnt[A[i]];
  }

  cin >> Q;

  vector<ll> B(Q);
  vector<ll> C(Q);
  vector<ll> S(Q, 0);

  for (ll i = 0; i < Q; ++i) {

    cin >> B[i] >> C[i];

    sum += (C[i] - B[i]) * cnt[B[i]];
    cnt[C[i]] += cnt[B[i]];
    cnt[B[i]] = 0;

    S[i] = sum;
  }

  for (ll i = 0; i < Q; ++i) {

    cout << S[i] << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

using namespace std;
using ll = long long;
const long long INF = 1LL << 60;

typedef pair<ll, ll> Pair;
int main() {

  ll N, Q;
  ll sum = 0;

  cin >> N;

  vector<ll> A(N);
  vector<ll> cnt(N + 1);

  for (ll i = 0; i < N; ++i) {
    cin >> A[i];
    sum += A[i];
    ++cnt[A[i]];
  }

  cin >> Q;

  vector<ll> B(Q);
  vector<ll> C(Q);
  vector<ll> S(Q, 0);

  for (ll i = 0; i < Q; ++i) {

    cin >> B[i] >> C[i];

    sum += (C[i] - B[i]) * cnt[B[i]];
    cnt[C[i]] += cnt[B[i]];
    cnt[B[i]] = 0;

    S[i] = sum;
  }

  for (ll i = 0; i < Q; ++i) {

    cout << S[i] << endl;
  }

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

#include <iostream>
#include <unordered_map>

using namespace std;

int main() {
  int n;
  cin >> n;
  int a[n];
  long long int sum = 0;
  unordered_map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
    sum = sum + a[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int search, val;
    cin >> search >> val;
    sum = sum - (search * m[search]);
    sum = sum + (val * m[search]);
    m[val] = m[val] + m[search];
    m[search] = 0;
    cout << sum << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <unordered_map>

using namespace std;

int main() {
  int n;
  cin >> n;
  int a[n];
  long long int sum = 0;
  unordered_map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
    sum = sum + a[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int search, val;
    cin >> search >> val;
    while (m[search]--) {
      sum = sum - search;
      sum = sum + val;
      m[val]++;
    }
    cout << sum << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using ll = long long;
#define rep(i, a, b) for (ll i = a; i < ll(b); i++)
#define repr(i, a, b) for (ll i = a; i >= ll(b); i--)
#define endl "\\n"
#define ALL(x) x.begin(), x.end()
#define ALLR(x) x.rbegin(), x.rend()
using namespace std;

int N, Q, p[100010], b[100010], c[100010];
ll sum;
int main() {
  scanf("%d", &N);
  rep(i, 0, N) {
    int tmp;
    scanf("%d", &tmp);
    p[tmp]++;
    sum += tmp;
  }
  scanf("%d", &Q);
  rep(i, 0, Q) scanf("%d%d", &b[i], &c[i]);

  rep(i, 0, Q) {
    int _b = b[i], _c = c[i];
    sum -= p[_b] * _b;
    sum += p[_b] * _c;
    p[_c] += p[_b];
    p[_b] = 0;
    printf("%lld\\n", sum);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using ll = long long;
#define rep(i, a, b) for (ll i = a; i < ll(b); i++)
#define repr(i, a, b) for (ll i = a; i >= ll(b); i--)
#define endl "\\n"
#define ALL(x) x.begin(), x.end()
#define ALLR(x) x.rbegin(), x.rend()
using namespace std;

int N, Q, p[100010], b[100010], c[10010];
ll sum;
int main() {
  scanf("%d", &N);
  rep(i, 0, N) {
    int tmp;
    scanf("%d", &tmp);
    p[tmp]++;
    sum += tmp;
  }
  scanf("%d", &Q);
  rep(i, 0, Q) scanf("%d%d", &b[i], &c[i]);

  rep(i, 0, Q) {
    int _b = b[i], _c = c[i];
    sum -= p[_b] * _b;
    sum += p[_b] * _c;
    p[_c] += p[_b];
    p[_b] = 0;
    printf("%lld\\n", sum);
  }
  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)
using namespace std;
#define pri(str) cout << str << endl
using ll = long long;
using P = pair<int, int>;

const ll MX = 1e18;
const long double PI = acos(-1);

int main() {
  int n, q;
  cin >> n;
  vector<int> a(100005);
  ll ans = 0;
  rep(i, n) {
    int num;
    cin >> num;
    ans += num;
    a[num] += 1;
  }
  cin >> q;

  rep(i, q) {
    int b, c;
    cin >> b >> c;
    if (a[b] != 0) {
      a[c] += a[b];
      ans += a[b] * (c - b);
      a[b] = 0;
    }

    pri(ans);
  }
  return 0;
}


Error: TLE

Fixed submission:

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

const ll MX = 1e18;
const long double PI = acos(-1);

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

  rep(i, q) {
    int b, c;
    cin >> b >> c;
    rep(j, n) {
      if (a[j] == b) {
        a[j] = c;
        ans += (c - b);
      }
    }
    pri(ans);
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int a[100001];
int main() {
  long long n;
  cin >> n;
  long long s = 0;
  for (int i = 1; i <= n; i++) {
    long long o;
    cin >> o;
    a[o]++;
    s += o;
  }
  int m;
  cin >> m;
  for (int i = 1; i <= m; i++) {
    long long x, y;
    cin >> x >> y;
    s += (y - x) * a[x];
    a[y] += a[x];
    a[x] = 0;
    cout << s << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long a[7001];
int main() {
  long long n;
  cin >> n;
  long long s = 0;
  for (int i = 1; i <= n; i++) {
    long long o;
    cin >> o;
    a[o]++;
    s += o;
  }
  int m;
  cin >> m;
  for (int i = 1; i <= m; i++) {
    long long x, y;
    cin >> x >> y;
    s += (y - x) * a[x];
    a[y] += a[x];
    a[x] = 0;
    cout << s << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define N 100010
#define M 1010

using namespace std;
int n, q;
ll ci[N];
ll ans = 0;

ll read() {
  ll s = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch))
    f |= (ch == '-'), ch = getchar();
  while (isdigit(ch))
    s = s * 10 + (ch ^ 48), ch = getchar();
  return f ? -s : s;
}

int main() {
  n = read();
  for (int i = 1, x; i <= n; i++) {
    x = read();
    ans += x;
    ci[x]++;
  }
  q = read();
  for (int i = 1, b, c; i <= q; i++) {
    b = read(), c = read();
    ans += ci[b] * (c - b);
    ci[c] += ci[b], ci[b] = 0;
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define lson rt << 1
#define rson rt << 1 | 1
#define N 100010
#define M 1010

using namespace std;
int n, q;
struct node {
  ll sum;
} tree[N << 2];

int read() {
  int s = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch))
    f |= (ch == '-'), ch = getchar();
  while (isdigit(ch))
    s = s * 10 + (ch ^ 48), ch = getchar();
  return f ? -s : s;
}

void push_up(int rt) { tree[rt].sum = tree[lson].sum + tree[rson].sum; }

void build(int rt, int l, int r) {
  if (l == r) {
    tree[rt].sum = read();
    return;
  }
  int mid = (l + r) >> 1;
  build(lson, l, mid);
  build(rson, mid + 1, r);
  push_up(rt);
}

void update(int rt, int b, int c, int l, int r) {
  if (l == r) {
    if (tree[rt].sum == b)
      tree[rt].sum = c;
    return;
  }
  int mid = (l + r) >> 1;
  update(lson, b, c, l, mid);
  update(rson, b, c, mid + 1, r);
  push_up(rt);
}

int main() {
  n = read();
  build(1, 1, n);
  q = read();
  for (int i = 1, b, c; i <= q; i++) {
    b = read(), c = read();
    update(1, b, c, 1, n);
    printf("%lld\\n", tree[1].sum);
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

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

  return 0;
}

Error: 0

Fixed submission:

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

vector<ll> prime; // 素因数分解で出てきた素数を全て挿入するvector

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

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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define MAX 50
#define LARGENUM 1000000007
#define INF 9223372036854775807
#define ull unsigned long long
typedef pair<int, int> P;

int main() {
  int N, Q, A, B, C;
  ull S = 0;
  cin >> N;
  map<int, int> M;
  map<int, int>::iterator it, it2;
  rep(i, N) {
    cin >> A;
    it = M.find(A);
    if (it == M.end()) {
      M.insert(P(A, 1));
    } else
      it->second += 1;
  }
  for (auto it = M.begin(); it != M.end(); it++) {
    S += it->first * it->second;
  }
  cin >> Q;
  rep(i, Q) {
    cin >> B >> C;
    it = M.find(B);
    if (it != M.end()) {
      it2 = M.find(C);
      if (it2 == M.end()) {
        M.insert(P(C, it->second));
      } else {
        it2->second += it->second;
      }
      S += (C - it->first) * it->second;
      M.erase(it);
    }
    cout << S << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define MAX 50
#define LARGENUM 1000000007
#define INF 9223372036854775807
#define ull unsigned long long
typedef pair<int, int> P;

int main() {
  int N, Q, A, B, C;
  ull S;
  cin >> N;
  map<int, int> M;
  map<int, int>::iterator it, it2;
  rep(i, N) {
    cin >> A;
    it = M.find(A);
    if (it == M.end()) {
      M.insert(P(A, 1));
    } else
      it->second += 1;
  }
  cin >> Q;
  rep(i, Q) {
    cin >> B >> C;
    it = M.find(B);
    if (it != M.end()) {
      it2 = M.find(C);
      if (it2 == M.end()) {
        M.insert(P(C, it->second));
      } else {
        it2->second += it->second;
      }
      M.erase(it);
    }
    S = 0;
    for (auto it = M.begin(); it != M.end(); it++) {
      S += it->first * it->second;
    }
    cout << S << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
long long arr[100005];
int main() {

  int n;
  cin >> n;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    arr[x] += 1;
    sum += x;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    arr[c] += arr[b];
    sum = sum + arr[b] * c - arr[b] * b;
    arr[b] = 0;
    cout << sum << '\\n';
  }
}


Error: TLE

Fixed submission:

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

using namespace std;
int main() {
  int a, d, e, f, i;
  cin >> a;
  int s[a];
  for (i = 0; i < a; i++) {
    cin >> s[i];
  }
  cin >> d;
  while (d--) {
    cin >> e >> f;
    for (i = 0; i < a; i++) {
      if (e == s[i])
        s[i] = f;
    }
    int sum = 0;
    for (i = 0; i < a; i++) {
      sum = sum + s[i];
    }

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  // your code goes here
  int n, q;
  cin >> n;
  int a[n];
  int h[100001];
  long long int s = 0;
  memset(h, 0, sizeof(h));
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    h[a[i]]++;
    s = s + a[i];
  }
  cin >> q;
  while (q--) {
    long long int b, c;
    cin >> b >> c;
    if (h[b] > 0) {
      h[c] = h[c] + h[b];

      s = s - (b * h[b]) + (c * h[b]);
      h[b] = 0;
    }
    cout << s << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  // your code goes here
  int n, q;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    long long int s = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] == b) {
        a[i] = c;
      }
      s = s + a[i];
    }
    cout << s << endl;
  }

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

#include <iostream>
using namespace std;

#include <map>
#include <string>
#include <vector>
int main() {
  int n;
  cin >> n;
  map<int, int> dates;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    dates[x]++;
    sum += x;
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * dates[b];
    dates[c] += dates[b];
    dates[b] = 0;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

#include <map>
int main() {
  int n;
  cin >> n;
  map<int, int> dates;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    dates[x]++;
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int b, c;
    cin >> b >> c;
    dates[c] += dates[b];
    dates.erase(b);
    long long ans = 0;
    for (auto &a : dates) {
      ans += (long long)a.first * a.second;
    }
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <numeric>
#include <utility>
#include <vector>

using namespace std;

const int MX = 100005;

int main(void) {
  int n, q;
  vector<int> cnt(MX);
  long long sum = 0;

  cin >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    sum += tmp;
    ++cnt[tmp];
  }

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

    sum += cnt[b] * (c - b);
    cnt[c] += cnt[b];
    cnt[b] = 0;
    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <numeric>
#include <utility>
#include <vector>

using namespace std;

int main(void) {
  int n, q;
  vector<long long> a;
  vector<pair<int, int>> v;
  long long sum = 0;

  cin >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    sum += tmp;
    a.push_back(tmp);
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    v.push_back(make_pair(b, c));
  }

  vector<long long> sum_log;
  for (auto p : v) {
    int b = p.first;
    int c = p.second;

    size_t cnt = 0;
    for (size_t i = 0; i < a.size(); i++) {
      if (a[i] == b) {
        a[i] = c;
        cnt++;
      }
    }

    int diff = b - c;

    sum = sum - cnt * diff;

    cout << sum << endl;
  }

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

#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()
#define dump(x) cout << #x << " = " << (x) << endl

using namespace std;
using ll = long long;

int main() {
  const int MAX = 100100;
  vector<int> cnt(MAX, 0);
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    cnt[a[i]]++;
  }

  sort(all(a));

  ll s = accumulate(all(a), 0ll);

  int q;
  cin >> q;
  vector<pair<int, int>> bc;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    bc.push_back({b, c});
  }

  rep(i, q) {
    int before = bc[i].first;
    int after = bc[i].second;

    int sa = after - before;

    int c = cnt[before];
    s += (c * sa);
    cout << s << endl;

    cnt[before] = 0;
    cnt[after] += c;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()
#define dump(x) cout << #x << " = " << (x) << endl

using namespace std;
using ll = long long;

int main() {
  const int MAX = 1000;
  vector<int> cnt(MAX, 0);
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    cnt[a[i]]++;
  }

  sort(all(a));

  ll s = accumulate(all(a), 0ll);

  int q;
  cin >> q;
  vector<pair<int, int>> bc;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    bc.push_back({b, c});
  }

  rep(i, q) {
    int before = bc[i].first;
    int after = bc[i].second;

    int sa = after - before;

    int c = cnt[before];
    s += (c * sa);
    cout << s << endl;

    cnt[before] = 0;
    cnt[after] += c;
  }
}
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 < (int)(n); i++)
using ll = long long;

const int MX = 100001;

int main() {
  int n;
  cin >> n;
  vector<ll> cnt(MX);
  rep(i, n) {
    int a;
    cin >> a;
    cnt[a]++;
  }
  ll tot = 0;
  rep(i, MX) tot += i * cnt[i];

  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    tot += (c - b) * cnt[b];
    cnt[c] += cnt[b];
    cnt[b] = 0;
    cout << tot << endl;
  }

  return 0;
}

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;

int main() {
  int n;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A.at(i);
  }

  int q;
  cin >> q;
  vector<int> B(q);
  vector<int> C(q);
  for (int i = 0; i < q; i++) {
    cin >> B.at(i) >> C.at(i);
  }

  int sum = 0;
  for (int i = 0; i < q; i++) {
    for (int j = 0; j < n; j++) {
      if (A.at(j) == B.at(i)) {
        A.at(j) = C.at(i);
      }
    }
    sum = 0;
    for (int j = 0; j < n; j++) {
      sum += A.at(j);
    }
    cout << sum << endl;
  }
}