alexjercan/bugnet huggingface dataset:
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using ll = long long;
using namespace std;

constexpr int MOD = 1e9 + 7;
constexpr ll MOD_LL = ll(1e9 + 7);

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

  vector<ll> a(n);
  map<ll, ll> mp;
  ll sum = 0LL;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    mp[a[i]]++;
    sum += a[i];
  }

  int Q;
  cin >> Q;

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

  for (int i = 0; i < Q; ++i) {
    if (mp[b[i]] > 0) {
      sum += (c[i] - b[i]) * mp[b[i]];
      ll tmp = mp[b[i]];

      mp[b[i]] = 0;
      mp[c[i]] += tmp;
    }

    cout << sum << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using ll = long long;
using namespace std;

constexpr int MOD = 1e9 + 7;
constexpr ll MOD_LL = ll(1e9 + 7);

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

  vector<ll> a(n);
  map<ll, int> mp;
  ll sum = 0LL;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    mp[a[i]]++;
    sum += a[i];
  }

  int Q;
  cin >> Q;

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

  for (int i = 0; i < Q; ++i) {
    if (mp[b[i]] > 0) {
      sum += (c[i] - b[i]) * mp[b[i]];
      ll tmp = mp[b[i]];

      mp[b[i]] = 0;
      mp[c[i]] += tmp;
    }

    cout << sum << endl;
  }

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

#include <algorithm>
#include <iostream>
#define ll long long
using namespace std;
ll a[500000];
ll sum = 0;
int main() {
  ll n;
  cin >> n;
  for (ll i = 0; i < n; i++) {
    ll k;
    cin >> k;
    sum += k;
    a[k]++;
  }
  ll q;
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll b, c;
    cin >> b >> c;
    ll x = a[b];
    sum = sum - x * b + x * c;
    a[b] = 0;
    a[c] = a[c] + x;
    cout << sum << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <stdio.h>
using namespace std;
long long A[10010];
long long sum = 0;
int n;
int main() {
  scanf("%d", &n);
  for (int i = 1, x; i <= n; i++) {
    scanf("%d", &x);
    A[x] += x;
    sum += x;
  }

  int q;
  scanf("%d", &q);
  while (q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    long long temp = A[a] / a * b;
    sum += temp - A[a];
    A[b] += A[a] / a * b;
    A[a] = 0;
    printf("%lld\\n", sum);
  }

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

#include <algorithm>
#include <iostream>
using namespace std;
long a[100010];
long cont[100010] = {};
int main() {
  long long n, q, x, y;
  while (~scanf("%lld", &n)) {
    long long total = 0;
    for (int i = 0; i < n; i++) {
      scanf("%ld", &a[i]);
      cont[a[i]]++;
      total += a[i];
    }
    scanf("%lld", &q);
    for (int i = 0; i < q; i++) {
      scanf("%lld %lld", &x, &y);
      total += (cont[x]) * (y - x);
      cont[y] += cont[x];
      cont[x] = 0;
      cout << total << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  long long A[10010], B[10010], C[10010];
  long long N;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  long long t;
  cin >> t;
  for (int k = 0; k < t; k++) {
    cin >> B[k] >> C[k];

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

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

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

typedef long long ll;
const int N = 1e5 + 10;
int cnt[N];

int main() {
  ios::sync_with_stdio(false);
  int n, x;
  cin >> n;
  ll sum = 0;
  for (int i = 0; i < n; i++)
    cin >> x, sum += x, cnt[x]++;
  ll q, b, c;
  cin >> q;
  while (q--) {
    cin >> b >> c;
    sum += (c - b) * cnt[b];
    cnt[c] += cnt[b];
    cnt[b] = 0;
    cout << sum << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int a[10010];
int cont[10010] = {};
int main() {
  int n, q, x, y;
  scanf("%d", &n);
  long total, temp = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    cont[a[i]]++;
    temp += a[i];
  }
  scanf("%d", &q);
  while (q--) {
    total = temp;
    scanf("%d %d", &x, &y);
    total += (cont[x]) * (y - x);
    cont[y] += cont[x];
    cont[x] = 0;
    cout << total << endl;
    temp = total;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int n, q;
int a[200010];
int main() {
  cin >> n;
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    a[x]++;
    sum += x;
  }
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    sum -= 1ll * x * a[x];
    sum += 1ll * a[x] * y;
    a[y] += a[x];
    a[x] = 0;
    cout << sum << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int a[10010];
int main() {
  int n, q, x, y;
  cin >> n;
  int total;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> q;
  while (q--) {
    total = 0;
    cin >> x >> y;
    for (int i = 0; i < n; i++) {
      if (a[i] == x) {
        a[i] = y;
      }
      total += a[i];
    }
    cout << total << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lld long double
#define vc vector<ll>
typedef pair<ll, ll> pairs;
int main() {
  // your code goes here
  std::ios::sync_with_stdio(false);
  ll t, n, i, j, k, l, c, f;
  cin >> n;
  i = 1;
  vc a(n + 1);
  c = 0;
  map<ll, ll> m;
  stack<ll> temp;
  while (i <= n) {
    cin >> a[i];
    c += a[i];
    if (m.find(a[i]) == m.end())
      m.insert({a[i], 1});
    else
      m[a[i]]++;
    i++;
    // temp.pop();
  }
  ll q, ind;
  cin >> q;
  while (q--) {
    cin >> j >> k;
    /*l=m[j].size();
    while(l>0){
        c-=j;
        c+=k;
        ind=m[j].top();
        temp.push(ind);
        if(m.find(k)==m.end())
        m.insert({k,temp});
        else
        m[k].push(ind);
    m[j].pop();
    temp.pop();
    l--;
    }*/
    ind = m[j];
    c -= j * m[j];
    m.erase(j);
    c += ind * k;
    if (m.find(k) == m.end())
      m.insert({k, ind});
    else
      m[k] += ind;
    // a[j]=k;
    cout << c << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lld long double
#define vc vector<ll>
typedef pair<ll, ll> pairs;
int main() {
  // your code goes here
  std::ios::sync_with_stdio(false);
  ll t, n, i, j, k, l, c, f;
  cin >> n;
  i = 1;
  vc a(n + 1);
  c = 0;
  unordered_map<ll, stack<ll>> m;
  stack<ll> temp;
  while (i <= n) {
    cin >> a[i];
    c += a[i];
    temp.push(i);
    if (m.find(a[i]) == m.end())
      m.insert({a[i], temp});
    else
      m[a[i]].push(i);
    i++;
    temp.pop();
  }
  ll q, ind;
  cin >> q;
  while (q--) {
    cin >> j >> k;
    l = m[j].size();
    while (l > 0) {
      c -= j;
      c += k;
      ind = m[j].top();
      temp.push(ind);
      if (m.find(k) == m.end())
        m.insert({k, temp});
      else
        m[k].push(ind);
      m[j].pop();
      temp.pop();
      l--;
    }
    m.erase(j);
    // a[j]=k;
    cout << c << "\\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;
  cin >> N;
  int l = 100001;
  ll v[l]{};
  ll A[N];
  ll s = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    v[A[i]]++;
    s += A[i];
  }
  int Q;
  cin >> Q;
  ll B[Q], C[Q];
  for (int i = 0; i < Q; i++) {
    cin >> B[i] >> C[i];
  }
  for (int i = 0; i < Q; i++) {
    ll e = C[i] * v[B[i]] - B[i] * v[B[i]];
    s += e;
    v[C[i]] += v[B[i]];
    v[B[i]] = 0;
    cout << s << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n, q, oldv = 0, newv = 0, sum = 0;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  sort(a, a + n);
  cin >> q;
  while (q--) {
    cin >> oldv >> newv;
    long int x = count(a, a + n, oldv);
    replace(a, a + n, oldv, newv);
    sum = sum - (oldv * x) + (newv * x);
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void My_Replace(map<int, int> *mp, int B, int C, int N, ll *sum) {

  int kazu = (*mp)[B];
  int diff = C - B;
  (*mp)[B] = 0;
  (*mp)[C] += kazu;

  *sum += (diff * kazu);
  cout << *sum << endl;
}
int main() {

  int N, Q, A, B, C;
  cin >> N;
  ll sum = 0;
  map<int, int> mp;
  for (int i = 0; i < N; i++) {
    cin >> A;
    if (mp.count(A) == 0)
      mp[A] = 1;
    else
      mp[A]++;

    sum += A;
  }

  cin >> Q;

  for (int i = 0; i < Q; i++) {
    cin >> B >> C;
    My_Replace(&mp, B, C, N, &sum);
  }
}

Error: TLE

Fixed submission:

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

void My_Replace(int data[], int B, int C, int N, ll *sum) {

  int count = 0;
  int diff = C - B;

  for (int i = 0; i < N; i++) {
    if (data[i] == B) {
      data[i] = C;
      count++;
    }
  }

  *sum += (diff * count);
  cout << *sum << endl;
}
int main() {

  int N, Q, B, C;
  cin >> N;
  int data[N];
  ll sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> data[i];
    sum += data[i];
  }

  cin >> Q;

  for (int i = 0; i < Q; i++) {
    cin >> B >> C;
    My_Replace(data, B, C, N, &sum);
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

int main() {
  int n;
  std::cin >> n;

  long sum = 0;
  std::vector<int> abox(100000);
  for (int i = 0; i < n; ++i) {
    int v;
    std::cin >> v;

    sum += v;
    ++abox[v];
  }

  int q;
  std::cin >> q;

  std::vector<std::pair<int, int>> bc(q);
  for (int i = 0; i < q; ++i) {
    std::cin >> bc[i].first;
    std::cin >> bc[i].second;
  }

  std::vector<long> s(q);
  for (int i = 0; i < q; ++i) {
    const int delta = abox[bc[i].first];
    abox[bc[i].first] = 0;
    abox[bc[i].second] += delta;

    sum += delta * (bc[i].second - bc[i].first);

    std::cout << sum << std::endl;
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

int main() {
  int n;
  std::cin >> n;

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

  int q;
  std::cin >> q;

  std::vector<std::pair<int, int>> bc(q);
  for (int i = 0; i < q; ++i) {
    std::cin >> bc[i].first;
    std::cin >> bc[i].second;
  }

  std::vector<long> s(q);
  for (int i = 0; i < q; ++i) {
    for (int j = 0; j < n; ++j) {
      if (a[j] == bc[i].first) {
        a[j] = bc[i].second;
      }

      s[i] += a[j];
    }
  }

  for (int j = 0; j < q; ++j)
    std::cout << s[j] << std::endl;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define ll long long
const int maxi = 2000000000;
const int maxq = 1000000000;
const ll mod = 1000000007LL;
#define f(i, a, b) for (ll i = (a); i < (b); ++i)
/*
unsigned int countSetBits(unsigned int n)
{
    unsigned int count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}
*/

/*
const int maxx=9000000;
void SieveOfEratosthenes(vector<int> &primes){
    bool IsPrime[maxx+1];
    memset(IsPrime, true, sizeof(IsPrime));
    for(int p=2;p*p<=maxx ;p++){
        if(IsPrime[p]){
            for(int i=p*p;i<maxx ;i+=p) IsPrime[i]=false;
        }
    }
    f(p,2,maxx +1){
        if(IsPrime[p]) primes.push_back(p);
    }
    }
*/
int aa[1000001];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  /*ll n,sum=0;
  cin>>n;
  ll a[n];
  f(i,0,n){
      cin>>a[i];
      sum+=a[i];
  }
  ll x;
  cin>>x;
  while(x--){
      ll c,d;
      cin>>c>>d;
      f(i,0,n){
      if(a[i]==c){
          a[i]=d;
          sum=sum+d-c;
      }

      } cout<<sum<<endl;
  } */
  ll n, sum = 0;
  cin >> n;
  ll a[n];
  f(i, 0, n) {
    cin >> a[i];
    aa[a[i]]++;
    sum += a[i];
    // cout<<aa[a[i]]<<' ';
  }
  ll q;
  cin >> q;
  while (q--) {
    ll b, c;
    cin >> b >> c;
    ll num1 = aa[b];
    // int num2=aa[c];
    // cout<<aa[b]*b<<' '<<aa[b]*;
    sum = sum - (num1 * b) + (num1 * c);
    aa[b] = 0;
    aa[c] += num1;
    cout << sum << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define ll long long
const int maxi = 2000000000;
const int maxq = 1000000000;
const ll mod = 1000000007LL;
#define f(i, a, b) for (ll i = (a); i < (b); ++i)
/*
unsigned int countSetBits(unsigned int n)
{
    unsigned int count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}
*/

/*
const int maxx=9000000;
void SieveOfEratosthenes(vector<int> &primes){
    bool IsPrime[maxx+1];
    memset(IsPrime, true, sizeof(IsPrime));
    for(int p=2;p*p<=maxx ;p++){
        if(IsPrime[p]){
            for(int i=p*p;i<maxx ;i+=p) IsPrime[i]=false;
        }
    }
    f(p,2,maxx +1){
        if(IsPrime[p]) primes.push_back(p);
    }
    }
*/
// int b[100];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, sum = 0;
  cin >> n;
  ll a[n];
  f(i, 0, n) {
    cin >> a[i];
    sum += a[i];
  }
  ll x;
  cin >> x;
  while (x--) {
    ll c, d;
    cin >> c >> d;
    f(i, 0, n) {
      if (a[i] == c) {
        a[i] = d;
        sum = sum + d - c;
      }
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007

#define NMAX 1000000

signed main() {
  int N;
  multiset<int> A;
  int cnt[200000] = {0};
  int Q;
  int ans = 0;

  cin >> N;
  for (int i = 0; i < N; i++) {
    int Ain;
    cin >> Ain;
    ans += Ain;
    A.insert(Ain);
    cnt[Ain]++;
  }

  cin >> Q;

  for (int i = 0; i < Q; i++) {
    int a, b;
    cin >> a >> b;
    ans -= cnt[a] * a;
    ans += cnt[a] * b;
    cnt[b] += cnt[a];
    cnt[a] = 0;
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

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

/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007

#define NMAX 1000000

signed main() {
  int N;
  multiset<int> A;
  int Q;
  int ans = 0;

  cin >> N;
  for (int i = 0; i < N; i++) {
    int Ain;
    cin >> Ain;
    ans += Ain;
    A.insert(Ain);
  }

  cin >> Q;

  for (int i = 0; i < Q; i++) {
    int a, b;
    cin >> a >> b;
    while (A.find(a) != A.end()) {
      A.erase(A.find(a));
      A.insert(b);
      ans -= a;
      ans += b;
    }
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

// #include<map>
#include <iostream>
#include <unordered_map>
#include <vector>
void stupid() {
  int N;
  std::unordered_map<long long, long long> cnts;
  int Q;
  std::vector<std::vector<int>> converts;
  long long sum;
  std::cin >> N;
  for (int i = 0; i < N; ++i) {
    int buf;
    std::cin >> buf;
    cnts[buf]++;
  }
  std::cin >> Q;
  converts = std::vector<std::vector<int>>(Q, std::vector<int>(2));
  for (int i = 0; i < Q; ++i) {
    std::cin >> converts[i][0] >> converts[i][1];
  }
  sum = 0;
  for (auto itr = cnts.begin(); itr != cnts.end(); ++itr) {
    sum += itr->first * itr->second;
  }
  for (int i = 0; i < Q; ++i) {
    auto itr1 = cnts.find(converts[i][0]);
    if (itr1 != cnts.end()) {
      cnts[converts[i][1]] += itr1->second;
      sum += (converts[i][1] - itr1->first) * itr1->second;
      std::cout << sum << std::endl;
      cnts.erase(itr1);
    } else {
      std::cout << sum << std::endl;
    }
  }
}
int main() {
  stupid();
  return 0;
}

Error: TLE

Fixed submission:

// #include<map>
#include <iostream>
#include <unordered_map>
#include <vector>
void stupid() {
  int N;
  std::unordered_map<long long, long long> cnts;
  int Q;
  std::vector<std::vector<int>> converts;
  long long sum;
  std::cin >> N;
  for (int i = 0; i < N; ++i) {
    int buf;
    std::cin >> buf;
    cnts[buf]++;
  }
  std::cin >> Q;
  converts = std::vector<std::vector<int>>(Q, std::vector<int>(2));
  for (int i = 0; i < Q; ++i) {
    std::cin >> converts[i][0] >> converts[i][1];
  }
  sum = -1;
  for (int i = 0; i < Q; ++i) {
    auto itr1 = cnts.find(converts[i][0]);
    if (itr1 != cnts.end()) {
      cnts[converts[i][1]] += cnts[converts[i][0]];
      cnts.erase(itr1);
      sum = 0;
      for (auto itr = cnts.begin(); itr != cnts.end(); ++itr) {
        sum += itr->first * itr->second;
      }
      std::cout << sum << std::endl;
    } else {
      if (sum == -1) {
        sum = 0;
        for (auto itr = cnts.begin(); itr != cnts.end(); ++itr) {
          sum += itr->first * itr->second;
        }
      }
      std::cout << sum << std::endl;
    }
  }
}
int main() {
  stupid();
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>

typedef unsigned long long ULONG;
typedef long long LONG;
using namespace std;

#define PI 3.1415926535897932384626433
#define _DBG_

int main(void) {
  map<ULONG, int> memo;
  ULONG N, Q;
  cin >> N;

  ULONG sum = 0;
  for (ULONG i = 0; i < N; i++) {
    ULONG tmpi;
    cin >> tmpi;
    sum += tmpi;
    memo[tmpi]++;
  }

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

  for (ULONG i = 0; i < Q; i++) {
    long long tmpsum = 0;
    int num = memo[B[i]];
    if (num > 0) {
      memo[B[i]] = 0;
      memo[C[i]] += num;
    }
    tmpsum = (C[i] - B[i]) * num;
    sum += tmpsum;

    cout << sum << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>

typedef unsigned long long ULONG;
typedef long long LONG;
using namespace std;

#define PI 3.1415926535897932384626433
#define _DBG_

int main(void) {
  ULONG N, Q;
  cin >> N;
  vector<ULONG> A(N);
  for (ULONG i = 0; i < N; i++) {
    cin >> A[i];
  }
  cin >> Q;
  vector<ULONG> B(Q);
  vector<ULONG> C(Q);
  for (ULONG i = 0; i < Q; i++) {
    cin >> B[i];
    cin >> C[i];
  }

  ULONG sum = accumulate(A.begin(), A.end(), 0);
  for (ULONG i = 0; i < Q; i++) {
    long long tmpsum = 0;
    for (ULONG j = 0; j < N; j++) {
      if (A[j] == B[i]) {
        A[j] = C[i];
        tmpsum += ((long long)C[i] - (long long)B[i]);
      }
    }
    sum += tmpsum;

    cout << sum << endl;
  }

  return 0;
}
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 pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef pair<int, Pii> P1;

#define rep(i, n) for (int i = 0; i < n; i++)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define mp1(a, b, c) P1(a, P(b, c))
#define get_arsize(a) (sizeof(a) / sizeof(a[0]))

const ll MOD = 1000000007;
const double EPS = 1e-10;
const int MAX = 200000;

int main() {
  ll n, q, k;
  cin >> n;
  vector<ll> a(n, 0);
  map<ll, ll> m;
  ll sum = 0;
  rep(i, n) {
    cin >> k;
    auto itr = m.find(k);
    if (itr != m.end()) {
      (itr->second)++;
    } else {
      m.insert(make_pair(k, 1));
    }
    sum += k;
  }
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    sum += m[b] * (c - b);
    cout << sum << endl;
    m[c] += m[b];
    m[b] = 0;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef pair<int, Pii> P1;

#define rep(i, n) for (int i = 0; i < n; i++)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define mp1(a, b, c) P1(a, P(b, c))
#define get_arsize(a) (sizeof(a) / sizeof(a[0]))

const ll MOD = 1000000007;
const double EPS = 1e-10;
const int MAX = 200000;

int main() {
  ll n, q, k;
  cin >> n;
  vector<ll> a(n, 0);
  map<ll, vector<ll>> m;
  ll sum = 0;
  rep(i, n) {
    cin >> k;
    auto itr = m.find(k);
    if (itr != m.end()) {
      (itr->second).push_back(i);
    } else {
      vector<ll> v;
      v.push_back(i);
      m.insert(make_pair(k, v));
    }
    sum += k;
  }
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    sum += m[b].size() * (c - b);
    cout << sum << endl;
    rep(j, m[b].size()) { m[c].push_back(m[b][j]); }
    m[b].clear();
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>

using namespace std;

int main() {
  vector<int> num(100001, 0);
  vector<long long> ans;
  int n, k, q, b, c;
  long long sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> k;
    num[k]++;
    sum += k;
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    k = num[b];
    num[b] = 0;
    num[c] += k;
    sum += (long long)((long long)c - (long long)b) * (long long)k;
    ans.emplace_back(sum);
  }

  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << "\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>

using namespace std;

int main() {
  vector<int> num(10001, 0);
  vector<long long> ans;
  int n, k, q, b, c;
  long long sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> k;
    num[k]++;
    sum += k;
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    k = num[b];
    num[b] = 0;
    num[c] += k;
    sum += (long long)((long long)c - (long long)b) * (long long)k;
    ans.emplace_back(sum);
  }

  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << "\\n";
  }

  return 0;
}