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

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

int main() {
  int n;
  cin >> n;
  map<int, int> mp;
  int a = 0;
  cin >> a;
  mp[a] = 1;
  long long s = a;
  for (int i = 0; i < n - 1; i++) {
    cin >> a;
    auto itr = mp.find(a);
    if (itr != mp.end()) {
      mp[a] += 1;
    } else {
      mp[a] = 1;
    }
    s += a;
  }

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

    s += (c - b) * mp[b];

    auto itr = mp.find(a);
    if (itr != mp.end()) {
      mp[c] += mp[b];
    } else {
      mp[c] = mp[b];
    }
    mp[b] = 0;
    cout << s << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<vector<int>> v(1);
  int a = 0;
  cin >> a;
  v.at(0).push_back(a);
  v.at(0).push_back(1);
  for (int i = 0; i < n - 1; i++) {
    cin >> a;
    bool exist = false;
    for (int j = 0; j < v.size(); j++) {
      if (a == v.at(j).at(0)) {
        v.at(j).at(1) += 1;
        exist = true;
      }
    }
    if (exist == false) {
      v.push_back(vector<int>());
      v.at(v.size() - 1).push_back(a);
      v.at(v.size() - 1).push_back(1);
    }
  }

  int q, b, c;
  long long s;
  cin >> q;
  for (int i = 0; i < q; i++) {
    s = 0;
    cin >> b >> c;
    for (int j = 0; j < v.size(); j++) {
      if (v.at(j).at(0) == b) {
        v.at(j).at(0) = c;
      }
      s += v.at(j).at(0) * v.at(j).at(1);
    }
    cout << s << endl;
  }

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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll counter[100001] = {0};

int main() {
  int n;

  cin >> n;

  int arr[n];
  ll sum = 0;

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

  int q;

  cin >> q;

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

    cin >> b >> c;

    sum = sum - counter[b] * b + counter[b] * c;
    counter[c] += counter[b];
    counter[b] = 0;

    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;

  cin >> n;

  int arr[n];
  int sum = 0;

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

  int q;

  cin >> q;

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

    cin >> b >> c;

    for (int j = 0; j < n; j++) {
      if (arr[j] == b) {
        arr[j] = c;
        sum = sum - b + c;
      }
    }

    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N;
  cin >> N;

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

  long S = 0;
  for (int i = 0; i < N; i++) {
    S = S + A.at(i);
  }

  int Q;
  cin >> Q;

  // int sw=0;

  vector<int> B(Q);
  vector<int> C(Q);
  for (int i = 0; i < Q; i++) {
    cin >> B.at(i) >> C.at(i);
    S = S + (C.at(i) - B.at(i)) * D.at(B.at(i));
    D.at(C.at(i)) = D.at(C.at(i)) + D.at(B.at(i));
    D.at(B.at(i)) = 0;
    cout << S << endl;
  }
}


Error: 0

Fixed submission:

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

int main() {
  int N;
  cin >> N;

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

  long S = 0;
  for (int i = 0; i < N; i++) {
    S = S + A.at(i);
  }

  int Q;
  cin >> Q;

  // int sw=0;

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

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

int main() {
  int n;
  cin >> n;
  vector<ll> a(100005);
  ll tot = 0;
  rep(i, n) {
    int k;
    cin >> k;
    a[k]++;
    tot += k;
  }
  vector<ll> ans;
  int q;
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    ll t = a[b];
    tot -= t * b;
    tot += t * c;
    ans.push_back(tot);
    a[b] = 0;
    a[c] += t;
  }
  int s = ans.size();
  rep(i, s) { cout << ans[i] << endl; }
}


Error: TLE

Fixed submission:

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

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

// ABC171D -

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

typedef unsigned long long int ull;
typedef long long int ll;

int main() {
  int N;
  cin >> N;
  map<int, int> A;
  ull sum = 0;
  for (int i = 0; i < N; i++) {
    int t;
    cin >> t;
    ++A[t];
    sum += (ull)t;
  }

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

    map<int, int>::iterator it = A.find(B);
    if (it != A.end()) {
      int t = it->second;
      it = A.erase(it);
      A[C] += t;
      sum += (ull)(C - B) * t;
    }
    cout << sum << endl;

    //		for (auto it = A.begin(); it != A.end(); ) {
    //			if (it->first == B) {
    //				int t = it->second;
    //				it = A.erase(it);
    //				A[C] += t;
    //				sum = sum - B * t + C * t;
    //			} else {
    //				it++;
    //			}
    //		}
    //		cout << sum << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

// ABC171D -

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

typedef unsigned long long int ull;
typedef long long int ll;

int main() {
  int N;
  cin >> N;
  map<ull, ull> A;
  ull sum = 0;
  for (int i = 0; i < N; i++) {
    int t;
    cin >> t;
    ++A[t];
    sum += t;
  }

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

    for (auto it = A.begin(); it != A.end();) {
      if (it->first == B) {
        ull t = it->second;
        it = A.erase(it);
        A[C] += t;
        sum = sum - B * t + C * t;
      } else {
        it++;
      }
    }
    cout << sum << endl;
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;

int main(void) {
  long N;
  cin >> N;

  long A[N];
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  long Q;
  cin >> Q;

  long num[100001] = {0};
  long long ans = 0;
  for (int i = 0; i < N; ++i) {
    num[A[i]] = num[A[i]] + A[i];
    ans = ans + A[i];
  }

  long B[Q], C[Q];

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

  for (int i = 0; i < Q; ++i) {
    ans = ans - num[B[i]] + (C[i] * num[B[i]] / B[i]);
    num[C[i]] = num[C[i]] + (C[i] * num[B[i]] / B[i]);
    num[B[i]] = 0;
    cout << ans << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;

int main(void) {
  long N;
  cin >> N;

  long A[N];
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  long Q;
  cin >> Q;

  long B[Q], C[Q];
  long long ans = 0;

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

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

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

void solve(long long N, std::vector<long long> A, long long Q,
           std::vector<long long> B, std::vector<long long> C) {
  map<int, int> D;
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    if (D.find(A[i]) == D.end()) {
      D[A[i]] = 1;
    } else {
      D[A[i]]++;
    }
    sum += A[i];
  }
  for (int i = 0; i < Q; i++) {
    int count = 0;
    auto it = D.find(B[i]);
    if (it != D.end()) {
      count = D[B[i]];
      sum = sum - (B[i] * count) + (C[i] * count);
      D.erase(it);
      auto it2 = D.find(C[i]);
      if (it2 == D.end()) {
        D.insert(make_pair(C[i], count));
      } else {
        D[C[i]] += count;
      }
    }

    cout << sum << endl;
  }
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  long long Q;
  scanf("%lld", &Q);
  std::vector<long long> B(Q);
  std::vector<long long> C(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &B[i]);
    scanf("%lld", &C[i]);
  }
  solve(N, std::move(A), Q, std::move(B), std::move(C));
  return 0;
}


Error: TLE

Fixed submission:

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

void solve(long long N, std::vector<long long> A, long long Q,
           std::vector<long long> B, std::vector<long long> C) {
  multiset<int> D;
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    D.insert(A[i]);
    sum += A[i];
  }
  for (int i = 0; i < Q; i++) {
    int count = D.erase(B[i]);
    for (int j = 0; j < count; j++) {
      D.insert(C[i]);
    }
    sum = sum - (B[i] * count) + (C[i] * count);
    cout << sum << endl;
  }
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  long long Q;
  scanf("%lld", &Q);
  std::vector<long long> B(Q);
  std::vector<long long> C(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &B[i]);
    scanf("%lld", &C[i]);
  }
  solve(N, std::move(A), Q, std::move(B), std::move(C));
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int cnt[100010];

int main() {
  int n;
  // scanf("%d", &n);
  cin >> n;
  int a[n];
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    // scanf("%d", &a[i]);
    cin >> a[i];
    sum += a[i];
    cnt[a[i]]++;
  }
  int q, b, c;
  // scanf("%d", &q);
  cin >> q;
  for (int i = 0; i < q; i++) {
    // scanf("%d%d", &b, &c);
    cin >> b >> c;
    // if(cnt[b]){
    sum += cnt[b] * (c - b);
    cnt[c] += cnt[b];
    cnt[b] = 0;
    // }
    // printf("%ld\\n", sum);
    cout << sum << "\\n";
  }
}


Error: 0

Fixed submission:

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

int main() {
  int n;
  // scanf("%d", &n);
  cin >> n;
  int a[n], cnt[10010];
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    // scanf("%d", &a[i]);
    cin >> a[i];
    sum += a[i];
    cnt[a[i]]++;
  }
  int q, b, c;
  // scanf("%d", &q);
  cin >> q;
  for (int i = 0; i < q; i++) {
    // scanf("%d%d", &b, &c);
    cin >> b >> c;
    if (cnt[b]) {
      sum += cnt[b] * (c - b);
      cnt[c] += cnt[b];
      cnt[b] = 0;
    }
    // printf("%ld\\n", sum);
    cout << sum << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  int A[N];
  ll sum = 0;
  map<ll, ll> mp;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    sum += A[i];
    mp[A[i]]++;
  }
  sort(A, A + N);
  int Q;
  cin >> Q;
  ll B[Q], C[Q], d[Q];

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

Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  int A[N];
  int sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    sum += A[i];
  }
  int Q;
  cin >> Q;
  int B[Q], C[Q], d[Q];
  for (int i = 0; i < Q; i++) {
    cin >> B[i] >> C[i];
    d[i] = C[i] - B[i];
  }
  for (int i = 0; i < Q; i++) {
    int cnt = 0;
    for (int j = 0; j < N; j++) {
      if (A[j] == B[i]) {
        A[j] = C[i];
        cnt++;
      }
    }
    sum += cnt * d[i];
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define REPR(i, n) for (int i = (int)(n - 1); i >= 0; --i)
#define FOR(i, x, n) for (int i = x; i < (int)(n); ++i)
#define ALL(n) begin(n), end(n)
#define ll long long
#define INF 1e9

vector<int> numcounter(100005);

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  REP(i, n) cin >> a.at(i);
  int q;
  cin >> q;
  vector<int> b(q), c(q);
  REP(i, q) cin >> b.at(i) >> c.at(i);
  ll sum = 0;
  REP(i, n) sum += a.at(i);

  REP(i, n) numcounter.at(a.at(i))++;

  REP(i, q) {
    sum += numcounter.at(b.at(i)) * (c.at(i) - b.at(i));
    numcounter.at(c.at(i)) += numcounter.at(b.at(i));
    numcounter.at(b.at(i)) = 0;
    cout << sum << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define REPR(i, n) for (int i = (int)(n - 1); i >= 0; --i)
#define FOR(i, x, n) for (int i = x; i < (int)(n); ++i)
#define ALL(n) begin(n), end(n)
#define ll long long
#define INF 1e9
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  REP(i, n) cin >> a.at(i);
  int q;
  cin >> q;
  vector<int> b(q), c(q);
  REP(i, q) cin >> b.at(i) >> c.at(i);
  ll sum = 0;
  REP(i, n) sum += a.at(i);

  vector<int> numcounter(1e5);

  REP(i, n) numcounter.at(a.at(i))++;

  REP(i, q) {
    sum += numcounter.at(b.at(i)) * (c.at(i) - b.at(i));
    numcounter.at(c.at(i)) += numcounter.at(b.at(i));
    numcounter.at(b.at(i)) = 0;
    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;

const int NUM = 1e6;

int main() {
  int n;
  ll sum = 0, ans = 0;

  cin >> n;

  map<ll, ll> a;
  for (int i = 0; i < n; i++) {
    ll input;
    cin >> input;
    a[input]++;
    sum += input;
  }
  // cout<<sum<<endl;
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    ll b, c;
    cin >> b >> c;

    sum -= a[b] * b;
    sum += a[b] * c;
    if (c != b) {
      a[c] += a[b];
      a[b] = 0;
    }

    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

const int NUM = 1e6;

int main() {
  ll n, sum = 0;
  cin >> n;

  int a_max = 0;
  vector<ll> v(NUM, 0);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a_max < a) {
      a_max = a;
    }
    v[a] += 1;
    // cout<<a<<":"<<v[a]<<endl;
  }
  /*
  for(int i=0; i<a_max+1; i++){
      cout<<v[i]<<" ";
  }
  cout<<endl;
  */
  int q, b, c;
  cin >> q;
  for (int i = 0; i < q; i++) {
    sum = 0;

    cin >> b >> c;
    if (a_max < b) {
      a_max = b;
    }
    if (a_max < c) {
      a_max = c;
    }
    // cout<<a_max<<endl;
    // cout<<"v["<<b<<"] : "<<v[b]<<endl;
    v[c] += v[b];
    v[b] = 0;
    // cout<<"v["<<c<<"] : "<<v[c]<<endl;
    for (int i = 0; i <= a_max; i++) {
      sum += v[i] * i;
    }
    cout << sum << endl;
  }

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

#include <iostream>
#include <string>

int main() {
  // 入力
  int N, Q;
  std::cin >> N;
  int *A = new int[N];
  int dic[100001] = {};
  int i;
  for (i = 0; i < N; i++) {
    std::cin >> A[i];
  }
  std::cin >> Q;
  int *B = new int[Q];
  int *C = new int[Q];
  for (i = 0; i < Q; i++) {
    std::cin >> B[i] >> C[i];
  }

  // 計算
  unsigned long long int wa = 0;
  for (i = 0; i < N; i++) {
    dic[A[i]] += 1;
    wa = wa + A[i];
  }
  int tmp;
  for (i = 0; i < Q; i++) { // S回目の進行
    tmp = dic[B[i]];
    dic[C[i]] += tmp;
    dic[B[i]] = 0;
    wa += tmp * (C[i] - B[i]);
    std::cout << wa << std::endl;
  }
  delete[] A;
  delete[] B;
  delete[] C;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>

int main() {
  // 入力
  int N, Q;
  std::cin >> N;
  int *A = new int[N];
  int i, j;
  for (i = 0; i < N; i++) {
    std::cin >> A[i];
  }
  std::cin >> Q;
  int *B = new int[Q];
  int *C = new int[Q];
  for (i = 0; i < Q; i++) {
    std::cin >> B[i] >> C[i];
  }

  // 計算
  int count;
  for (j = 0; j < Q; j++) { // S回目の進行
    count = 0;
    for (i = 0; i < N; i++) { // Aの検索
      if (A[i] == B[j]) {
        A[i] = C[j];
      }
      count = count + A[i];
    }
    std::cout << count << std::endl;
  }
  delete[] A;
  delete[] B;
  delete[] C;
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n, s) for (int i = (s); i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define rreps(i, n, s) for (int i = s; i >= n; i--)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using namespace std;
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int MOD = 1000000007;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n);
  map<int, int> num;
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    num[a[i]]++;
    sum += a[i];
  }
  int q;
  cin >> q;

  rep(i, q) {
    int b, c;
    cin >> b >> c;
    ll numb = num[b];
    if (numb > 0) {
      sum -= numb * b;
      sum += numb * c;
      num[c] += numb;
      num[b] = 0;
    }
    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n, s) for (int i = (s); i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define rreps(i, n, s) for (int i = s; i >= n; i--)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using namespace std;
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int MOD = 1000000007;
template <typename T> struct RMQ {
  const T INF = 0;
  int n;
  vector<T> dat;
  RMQ(int n_) : n(), dat(n_ * 4, INF) {
    int x = 1;
    while (n_ > x) {
      x *= 2;
    }
    n = x;
  }

  void update(int i, T x) {
    i += n - 1;
    dat[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;
      dat[i] = dat[i * 2 + 1] + dat[i * 2 + 2];
    }
  }

  T query(int a, int b) { return query_sub(a, b, 0, 0, n); }
  T query_sub(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) {
      return INF;
    } else if (a <= l && r <= b) {
      return dat[k];
    } else {
      T vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
      T vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
      return vl + vr;
    }
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n);
  RMQ<ll> rmq(n);
  rep(i, n) {
    cin >> a[i];
    rmq.update(i, a[i]);
  }
  int q;
  cin >> q;

  rep(i, q) {
    int b, c;
    cin >> b >> c;
    auto it = find(a.begin(), a.end(), b);
    auto idx = distance(a.begin(), it);
    while (idx < n) {
      a[idx] = c;
      rmq.update(idx, c);
      it = find(a.begin() + idx + 1, a.end(), b);
      idx = distance(a.begin(), it);
    }
    cout << rmq.dat[0] << 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++)
using ll = long long;
using namespace std;

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

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

  int q;
  cin >> q;
  vector<ll> b(q);
  vector<ll> c(q);
  rep(i, q) cin >> b[i] >> c[i];

  vector<ll> num(100005, 0);
  rep(i, n) num[a[i]]++;

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

  return 0;
}

Error: TLE

Fixed submission:

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

int search(vector<int> &a, int n, int key, int c) {
  int left = 0;
  int right = n;
  while (left < right) {
    int mid = (left + right) / 2;
    if (a[mid] == key) {
      a[mid] = c;
      return 1;
    } else if (a[mid] > key)
      right = mid;
    else
      left = mid + 1;
  }
  return 0;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int q;
  cin >> q;
  vector<int> b(q);
  vector<int> c(q);

  rep(i, q) cin >> b[i] >> c[i];

  vector<ll> s(q, 0);

  bool flag = true;
  for (int i = 0; i < q; i++) {
    while (flag) {
      sort(a.begin(), a.end());
      if (search(a, n, b[i], c[i]))
        flag = true;
      else
        break;
    }
    s[i] = accumulate(a.begin(), a.end(), 0);
    cout << s[i] << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long int N, s;
  s = 0;
  cin >> N;
  vector<int> A(100001);
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    s += x;
    A.at(x)++;
  }
  int Q;
  cin >> Q;
  for (int j = 0; j < Q; j++) {
    long int B, C;
    cin >> B >> C;
    A.at(C) += A.at(B);
    s += (C - B) * A.at(B);
    cout << s << endl;
    A.at(B) = 0;
  }
}


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;
  for (int j = 0; j < Q; j++) {
    int B, C;
    cin >> B >> C;
    for (int k = 0; k < N; k++) {
      if (A.at(k) > B) {
        sort(A.begin(), A.end());
        break;
      } else if (A.at(k) == B) {
        A.at(k) = C;
      }
    }
    int g = 0;
    for (int m = 0; m < N; m++) {
      g += A.at(m);
    }
    cout << g << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

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

  vector<int> num(100001, 0);
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;

    ans += A;
    num.at(A)++;
  }

  int Q;
  cin >> Q;

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

    ans += (C - B) * num.at(B);
    num.at(C) += num.at(B);
    num.at(B) = 0;

    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

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

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

  vector<int> num(100000, 0);
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;

    ans += A;
    num.at(A)++;
  }

  int Q;
  cin >> Q;

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

    ans += (C - B) * num.at(B);
    num.at(C) += num.at(B);
    num.at(B) = 0;

    cout << ans << endl;
  }
}