alexjercan/bugnet huggingface dataset:
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;
using ll = long long;

const int MAX = 1e5 + 2;

int main() {
  ll sum = 0;
  int n, q;
  cin >> n;
  vector<int> a(MAX);
  rep(i, n) {
    int b;
    cin >> b;
    sum += b;
    a[b]++;
  };
  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: TLE

Fixed submission:

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

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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long N;
  cin >> N;
  vector<long long> A(N);
  vector<long long> cnt(100001);
  long long sum = 0;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    sum += A[i];
    ++cnt[A[i]];
  }

  int Q;
  cin >> Q;
  vector<long long> ans(Q);
  for (int i = 0; i < Q; ++i) {
    long long B, C;
    cin >> B >> C;

    if (cnt[B] == 0) {
      ans[i] = sum;
      continue;
    }

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

  for (int i = 0; i < Q; ++i)
    cout << ans[i] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <string>
#include <vector>
using namespace std;

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

  int Q;
  cin >> Q;
  vector<long long> ans(Q);
  for (int i = 0; i < Q; ++i) {
    int B, C;
    cin >> B >> C;

    cnt[C] += cnt[B];
    cnt[B] = 0;

    for (int j = 0; j < cnt.size(); ++j) {
      if (cnt[j] == 0)
        continue;
      ans[i] += j * cnt[j];
    }
  }

  for (int i = 0; i < Q; ++i)
    cout << ans[i] << endl;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;

int t = 1;
#define N 100001

void solve() {
  ll n, q, sum = 0;
  cin >> n;
  vector<ll> f(N);
  set<ll> numbers;
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    sum += a;
    if (numbers.count(a) < 1) {
      numbers.insert(a);
      f[a] = 1;
    } else {
      f[a]++;
    }
  }
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll b, c, a;
    cin >> b >> c;
    a = f[b];
    f[c] += a;
    f[b] = 0;
    sum += (a * c - a * b);
    cout << sum << endl;
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  // freopen("data.in", "r", stdin);

  // cin >> t;
  while (t--)
    solve();

  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;

int t = 1;

void solve() {
  ll n, q;
  cin >> n;
  vector<ll> f(n);
  for (int i = 0; i < n; i++)
    cin >> f[i];
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll a, b;
    cin >> a >> b;
    replace(f.begin(), f.end(), a, b);
    cout << accumulate(f.begin(), f.end(), 0) << endl;
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  // freopen("data.in", "r", stdin);

  // cin >> t;
  while (t--)
    solve();

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

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

int main() {
  int n;
  cin >> n;
  int a[500000];
  long long int sum = 0;
  long long int m[200000];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
    sum += a[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int c, b;
    cin >> c >> b;
    if (m[c] > 0) {
      sum = sum - c * m[c];
      sum += b * m[c];
      m[b] += m[c];
      m[c] = 0;
    }

    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

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

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

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

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


Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define ll long long
#define pb push_back
#define pf push_front

using namespace std;

void solve() {
  ll int n;
  cin >> n;
  ll int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll int m[100001];
  for (int i = 0; i <= 100000; i++)
    m[i] = 0;
  // map<ll int, ll int>m;
  for (int i = 0; i < n; i++)
    m[a[i]]++;
  ll int q;
  cin >> q;
  ll int b, c, sum = 0;
  for (int i = 0; i < n; i++)
    sum += a[i];
  while (q--) {
    cin >> b >> c;
    sum -= (m[b] * b);
    sum += (m[b] * c);
    m[c] += m[b];
    m[b] = 0;

    cout << sum << endl;
  }
}

int main() {
  int t = 1; // cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define ll long long
#define pb push_back
#define pf push_front

using namespace std;

void solve() {
  ll int n;
  cin >> n;
  ll int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  multiset<ll int> s;
  for (int i = 0; i < n; i++)
    s.insert(a[i]);
  ll int q;
  cin >> q;
  ll int b, c, sum = 0;
  for (int i = 0; i < n; i++)
    sum += a[i];
  while (q--) {
    cin >> b >> c;
    sum -= (s.count(b) * b);
    sum += (s.count(b) * c);
    for (int i = 1; i <= s.count(b); i++)
      s.insert(c);
    s.erase(b);
    cout << sum << endl;
  }
}

int main() {
  int t = 1; // cin>>t;
  while (t--) {
    solve();
  }

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

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

int main() {
  long sum = 0;
  int N;
  cin >> N;

  map<int, int> numMap;

  for (int i = 0; i < N; i++) {
    int inputx;
    cin >> inputx;
    if (numMap.find(inputx) != numMap.end()) {
      numMap[inputx] += 1;
    } else {
      numMap[inputx] = 1;
    }
    sum += inputx;
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int b, c;
    cin >> b >> c;
    int bNum = numMap[b];
    if (numMap.find(c) != numMap.end()) {
      numMap[c] += bNum;
    } else {
      numMap[c] = bNum;
    }
    numMap[b] = 0;

    sum += (c - b) * bNum;

    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  multiset<int> a;

  for (int i = 0; i < N; i++) {
    int inputx;
    cin >> inputx;
    a.insert(inputx);
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int b, c;
    cin >> b >> c;
    int bNum = a.count(b);
    a.erase(b);
    for (int s = 0; s < bNum; s++) {
      a.insert(c);
    }
    auto itr = a.begin();
    int sum = *itr;
    for (int s = 1; s < a.size(); s++) {
      itr++;
      sum += *itr;
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int inf = 100100;
int main() {
  int n;
  cin >> n;
  vector<int> m(100100);
  int maxi = 0;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }

  for (int i = 0; i < n; i++) {
    if (maxi < a.at(i)) {
      maxi = a.at(i);
    }
  }

  for (int i = 0; i < n; i++) {
    m.at(a.at(i))++;
  }

  ll initial = 0;
  for (int i = 0; i < inf; i++) {
    initial += i * m.at(i);
  }
  // cout << initial << endl;

  ll sum = initial;
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * m.at(b);
    m.at(c) += m.at(b);
    m.at(b) = 0;
    cout << sum << endl;
  }

  return (0);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int inf = 100100;
int main() {
  int n;
  cin >> n;
  vector<int> m(100100);
  int maxi = 0;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }

  for (int i = 0; i < n; i++) {
    // cout << a.at(i) << endl;
    if (maxi < a.at(i)) {
      maxi = a.at(i);
    }
  }

  for (int i = 0; i < n; i++) {
    m.at(a.at(i))++;
    // cout << a.at(i) << " " << m.at(a.at(i)) << endl;
  }

  int q;
  cin >> q;

  int b, c;

  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    int sum = 0;
    m.at(c) += m.at(b);
    m.at(b) = 0;
    if (maxi < c) {
      // cout << maxi << " " << c << endl;
      maxi = c;
    }
    for (int i = 0; i <= maxi; i++) {
      // cout << m.at(i) << endl;
      sum += m.at(i) * i;
    }
    cout << sum << endl;
  }

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

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long n;
  cin >> n;
  vector<long> v(n);
  long long sum = 0;
  vector<long int> count(100001, 0);
  for (long i = 0; i < n; i++) {
    cin >> v[i];
    sum = sum + v[i];
    count[v[i]]++;
  }
  int q;
  cin >> q;
  while (q--) {
    long int b, c;
    cin >> b >> c;
    long cb = count[b];
    count[b] = 0;
    count[c] = count[c] + cb;
    long sum_s = cb * b;
    long sum_a = c * cb;
    sum = sum + sum_a - sum_s;
    cout << sum << "\\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 n;
  cin >> n;
  vector<long> v(n);
  for (long i = 0; i < n; i++) {
    cin >> v[i];
  }
  int q;
  cin >> q;
  while (q--) {
    long b, c;
    cin >> b >> c;
    long long sum = 0;
    for (auto i = 0; i < n; i++) {
      if (v[i] != b) {
        sum = sum + v[i];
      } else {
        v[i] = c;
        sum = sum + v[i];
      }
    }
    cout << sum << "\\n";
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef string str;

const int mxn = (int)10e5;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n;
  cin >> n;

  ll freq[mxn + 1], sum = 0;

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

  int q;
  cin >> q;

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

    sum = sum - freq[b] * b + freq[b] * c;
    freq[c] += freq[b];
    freq[b] = 0;
    cout << sum << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef string str;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n;
  cin >> n;

  vector<ll> arr(n);
  for (ll i = 0; i < n; i++)
    cin >> arr[i];

  ll q;
  cin >> q;

  for (ll i = 0; i < q; i++) {
    ll a, b;
    cin >> a >> b;
    ll sum = 0;
    for (ll i = 0; i < n; i++) {
      if (arr[i] == a)
        arr[i] = b;
      sum += arr[i];
    }
    cout << sum << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> a(100010);
ll sum;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    sum += x;
    a[x]++;
  }

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> a(100010);
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    a[x]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int b, c;
    cin >> b >> c;
    a[c] += a[b];
    a[b] = 0;
    int sum = 0;
    for (int j = 1; j < 100001; ++j) {
      int xx = a[j] * j;
      sum += xx;
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>

#define db double
#define vi vector<int>
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ld long double
#define maxn 5005
#define mod 998244353
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpi vector<pii>
#define vpll vector<pll>
#define vv(z, a, b) vector<vector<int>> z(a, vector<int>(b))
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) begin(c), end(c)
#define pp pair<int, int>
#define fix(n) fixed << setprecision(n)
#define gmax(a, b) __gcd(a, b)
#define gmin(a, b) a / __gcd(a, b) * b
using namespace std;

int main() {
  int n;
  cin >> n;
  vi a(n);
  vi suu(100000);
  ll ans = 0;
  rep(i, n) {
    cin >> a.at(i);

    suu.at(a.at(i) - 1) += 1;

    ans += a.at(i);
  }

  /*rep(i,n){
      rep(j,100000){
          if(a.at(i)==j+1){
          suu.at(j)+=1;
          }
      }
      ans+=a.at(i);
  }*/
  // cout<<ans<<endl;

  int q;
  cin >> q;

  rep(i, q) {
    int b, c;
    cin >> b >> c;
    int h = c - b;
    ans = ans + (suu.at(b - 1) * h);

    suu.at(c - 1) += suu.at(b - 1);
    suu.at(b - 1) = 0;
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>

#define db double
#define vi vector<int>
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ld long double
#define maxn 5005
#define mod 998244353
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpi vector<pii>
#define vpll vector<pll>
#define vv(z, a, b) vector<vector<int>> z(a, vector<int>(b))
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) begin(c), end(c)
#define pp pair<int, int>
#define fix(n) fixed << setprecision(n)
#define gmax(a, b) __gcd(a, b)
#define gmin(a, b) a / __gcd(a, b) * b
using namespace std;

int main() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a.at(i);
  vi suu(100000);
  ll ans = 0;
  rep(i, n) {
    rep(j, 100000) {
      if (a.at(i) == j + 1) {
        suu.at(j) += 1;
      }
    }
    ans += a.at(i);
  }
  // cout<<ans<<endl;

  int q;
  cin >> q;

  rep(i, q) {
    int b, c;
    cin >> b >> c;
    int h = c - b;
    ans = ans + (suu.at(b - 1) * h);

    suu.at(c - 1) += suu.at(b - 1);
    suu.at(b - 1) = 0;
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

int main() {
  long long N, Q, ans = 0;
  cin >> N;
  vector<long long> A(N), P(1e5 + 100);
  for (int i = 0; i < N; ++i) {
    cin >> A.at(i);
    ans += A.at(i);
    ++P.at(A.at(i));
  }
  cin >> Q;
  for (int i = 0; i < Q; ++i) {
    long long b, c;
    cin >> b >> c;
    ans -= b * P.at(b);
    ans += c * P.at(b);
    P.at(c) += P.at(b);
    P.at(b) = 0;
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N, Q, ans = 0;
  vector<int> A(N), P(1e5 + 100);
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> A.at(i);
    ans += A.at(i);
    ++P.at(A.at(i));
  }
  cin >> Q;
  for (int i = 0; i < Q; ++i) {
    int b, c;
    cin >> b >> c;
    ans -= b * P.at(b);
    ans += c * P.at(b);
    P.at(c) += P.at(b);
    P.at(b) = 0;
    cout << ans << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

typedef long long int ll;

int main(void) {
  int N, b, c, Q, a;
  ll sum = 0;
  std::cin >> N;
  std::vector<int> num(100001, 0);
  for (int i = 0; i < N; i++) {
    std::cin >> a;
    num[a]++;
    sum += a;
  }

  std::cin >> Q;
  for (int i = 0; i < Q; i++) {
    std::cin >> b >> c;
    sum += (c - b) * num[b];
    num[c] += num[b];
    num[b] = 0;
    std::cout << sum << std::endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

int main(void) {
  int n, b, c, q;
  ll sum = 0;
  cin >> n;
  vector<int> a(n, 0);

  for (int i = 0; i < n; i++)
    cin >> a[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    sum = 0;
    int j = 0;
    cin >> b >> c;
    while (a[j] <= b) {
      if (a[j] == b)
        a[j] = c;
      j++;
    }
    sum = accumulate(a.begin(), a.end(), 0);
    sort(a.begin(), a.end());
    cout << sum << endl;
  }

  return 0;
}
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++)
#define _GLIBCXX_DEBUG
typedef long long int ll;

int main() {
  ll n, q;
  cin >> n;

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

  cin >> q;

  vector<ll> b, c;
  rep(i, q) {
    ll s, d;
    cin >> s >> d;
    b.push_back(s);
    c.push_back(d);
  }

  sort(a.begin(), a.end());

  map<ll, ll> map;
  for (ll i = 0; i < n; i++) {
    auto itr = map.find(a[i]);
    if (itr != map.end())
      map[a[i]]++;
    else
      map[a[i]] = 1;
  }

  // for (auto x : map)
  //   cout << x.first << " " << x.second << endl;
  ll sum = accumulate(a.begin(), a.end(), 0LL);
  // cout << sum << endl;

  vector<ll> s;
  for (ll i = 0; i < q; i++) {
    sum += (c[i] - b[i]) * (map[b[i]]);
    s.push_back(sum);
    map[c[i]] += map[b[i]];
    map[b[i]] = 0;

    // for (auto x : map)
    //   cout << x.first << " " << x.second << endl;
  }

  for (auto ans : s)
    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++)
#define _GLIBCXX_DEBUG
typedef long long int ll;

int main() {
  ll n, q;
  cin >> n;

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

  cin >> q;

  vector<ll> b, c;
  rep(i, q) {
    ll s, d;
    cin >> s >> d;
    b.push_back(s);
    c.push_back(d);
  }

  sort(a.begin(), a.end());

  vector<ll> s;
  for (ll i = 0; i < q; i++) {
    // for (ll j = 0; j < n; j++)
    // {
    //   if (a[j] == b[i])
    //     a[j] = c[i];
    // }
    replace(a.begin(), a.end(), b[i], c[i]);
    ll sum = accumulate(a.begin(), a.end(), 0LL);
    s.push_back(sum);
  }

  for (auto ans : s)
    cout << ans << 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> mp;

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

    sum += a;
  }

  int q;
  cin >> q;
  vector<long long> ans(q);

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

    sum = sum - b * mp[b] + c * mp[b];

    ans[i] = sum;

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

  for (long long i = 0; i < q; i++) {
    cout << ans[i] << 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> mp;

  for (long long i = 0; i < n; i++) {
    long long a;
    cin >> a;

    mp[a]++;
  }

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

    mp[c] += mp[b];
    mp[b] = 0;

    for (auto h : mp) {
      ans[i] += h.first * h.second;
    }
  }

  for (long long i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

const int MAX = 510000;
const int MOD = 1000000007;

int main() {
  ll N;
  cin >> N;
  vector<ll> a(N);
  rep(i, N) cin >> a[i];
  ll Q;
  cin >> Q;
  vector<ll> b(Q);
  vector<ll> c(Q);
  rep(i, Q) cin >> b[i] >> c[i];

  ll ans = 0;
  rep(i, N) ans += a[i];
  map<ll, ll> m;
  rep(i, N) m[a[i]]++;

  rep(i, Q) {
    if (m[b[i]] > 0) {
      // 変更あり
      ans += (c[i] - b[i]) * m[b[i]];
      m[c[i]] += m[b[i]];
      m[b[i]] = 0;
    }
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

const int MAX = 510000;
const int MOD = 1000000007;

int main() {
  ll N;
  cin >> N;
  vector<ll> a(N);
  rep(i, N) cin >> a[i];
  ll Q;
  cin >> Q;
  vector<ll> b(N);
  vector<ll> c(N);
  rep(i, Q) cin >> b[i] >> c[i];

  ll ans = 0;
  rep(i, N) ans += a[i];
  map<ll, ll> m;
  rep(i, N) m[a[i]]++;

  rep(i, Q) {
    if (m[b[i]] > 0) {
      // 変更あり
      ans += (c[i] - b[i]) * m[b[i]];
      m[c[i]] += m[b[i]];
      m[b[i]] = 0;
    }
    cout << ans << endl;
  }
}