Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define SORT(s) sort(s.begin(), s.end())
#define P pair<int, int>
#define mp make_pair

const double INF = 1e+09;
const int MOD = 1e9 + 7;

using namespace std;

template <typename _Ty>
ostream &operator<<(ostream &ostr, const vector<_Ty> &v) {
  if (v.empty()) {
    ostr << "{ }";
    return ostr;
  }
  ostr << "{" << v.front();
  for (auto itr = ++v.begin(); itr != v.end(); itr++) {
    ostr << ", " << *itr;
  }
  ostr << "}";
  return ostr;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int gcd(int a, int b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

int lcm(int a, int b) { return a * b / gcd(a, b); }

vector<int> uniq(vector<int> vec) {
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  return vec;
}

vector<ll> divisor(ll n) {
  vector<ll> ret;
  for (int64_t i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}
void permu(int n) {
  vector<int> perm(n);
  REP(i, n) perm[i] = i;
  do {
    cout << perm << endl;
  } while (next_permutation(perm.begin(), perm.end()));
  return;
}

vector<pair<ll, int>> prime_div(ll n) {
  vector<pair<ll, int>> res;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      int a = 0;
      while (n % i == 0) {
        n /= i;
        a++;
      }
      pair<ll, int> tmp;
      tmp.first = i;
      tmp.second = a;
      res.push_back(tmp);
    }
  }
  if (n != 1) {
    pair<ll, int> now;
    now.first = n;
    now.second = 1;
    res.push_back(now);
  }
  return res;
}

// printf("%.10f\\n" , ans);
int main(void) {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int q;
  cin >> q;
  vector<ll> b(q), c(q);
  for (int i = 0; i < q; i++) {
    cin >> b[i] >> c[i];
  }
  ll total = 0;
  for (ll x : a) {
    total += x;
  }

  vector<ll> count(1e+05 + 100, 0);
  REP(i, n) { count[a[i]]++; }
  REP(i, q) {
    ll num = count[b[i]];
    total += num * (c[i] - b[i]);
    cout << total << endl;
    count[b[i]] = 0;
    count[c[i]] += num;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define SORT(s) sort(s.begin(), s.end())
#define P pair<int, int>
#define mp make_pair

const double INF = 1e+09;
const int MOD = 1e9 + 7;

using namespace std;

template <typename _Ty>
ostream &operator<<(ostream &ostr, const vector<_Ty> &v) {
  if (v.empty()) {
    ostr << "{ }";
    return ostr;
  }
  ostr << "{" << v.front();
  for (auto itr = ++v.begin(); itr != v.end(); itr++) {
    ostr << ", " << *itr;
  }
  ostr << "}";
  return ostr;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int gcd(int a, int b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

int lcm(int a, int b) { return a * b / gcd(a, b); }

vector<int> uniq(vector<int> vec) {
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  return vec;
}

vector<ll> divisor(ll n) {
  vector<ll> ret;
  for (int64_t i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}
void permu(int n) {
  vector<int> perm(n);
  REP(i, n) perm[i] = i;
  do {
    cout << perm << endl;
  } while (next_permutation(perm.begin(), perm.end()));
  return;
}

vector<pair<ll, int>> prime_div(ll n) {
  vector<pair<ll, int>> res;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      int a = 0;
      while (n % i == 0) {
        n /= i;
        a++;
      }
      pair<ll, int> tmp;
      tmp.first = i;
      tmp.second = a;
      res.push_back(tmp);
    }
  }
  if (n != 1) {
    pair<ll, int> now;
    now.first = n;
    now.second = 1;
    res.push_back(now);
  }
  return res;
}

// printf("%.10f\\n" , ans);
int main(void) {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int q;
  cin >> q;
  vector<ll> b(q), c(q);
  for (int i = 0; i < q; i++) {
    cin >> b[i] >> c[i];
  }
  ll total = 0;
  ll ma = 0;
  for (ll x : a) {
    total += x;
    ma = max(ma, x);
  }

  vector<ll> count(ma + 10, 0);
  REP(i, n) { count[a[i]]++; }
  REP(i, q) {
    ll num = count[b[i]];
    total += num * (c[i] - b[i]);
    cout << total << endl;
    count[b[i]] = 0;
    count[c[i]] += num;
  }
  return 0;
}