Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define ll long long
#define sz(a) (int)(a.size())
#define pb push_back
#define all(c) c.begin(), c.end()
#define tr(it, c) for (auto it = c.begin(); it != c.end(); it++)
#define fr(i, n) for (int i = 0; i < n; i++)
#define present(c, x) (c.find(x) != c.end()) // for set/map etc.
#define cpresent(c, x) (find(all(c), x) != c.end())
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pair<int, int>> vp;
int N = 100001;
int mod = 1e9 + 7;
int min(int a, int b) {
  if (a < b)
    return a;
  else
    return b;
}
int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
int power(int a, int b) {
  int res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
int gcd(int p, int q) {
  if (p % q == 0)
    return q;
  else {
    return gcd(q, p % q);
  }
}
bool comp(int a, int b) { return (a < b); }

void solve() {
  int n;
  cin >> n;
  vi v(n);
  int count2[200000];
  ll sum = 0;
  fr(i, n) {
    cin >> v[i];
    sum = sum + v[i];
    count2[v[i]]++;
  }
  int q;
  cin >> q;

  int x, y;
  // int count1=0;
  // sort(all(v));
  fr(i, q) {
    cin >> x >> y;
    // count1=count(all(v),x)
    // count2[x]=0
    // count1=0;
    // auto it= find(all(v),x);
    // while(it!=v.end()&&*it==x)
    // {
    // 	*it=y;
    // 	it++;
    // 	count1++;
    // }
    sum = sum - (x - y) * count2[x];
    count2[y] = count2[y] + count2[x];
    count2[x] = 0;
    cout << sum << endl;
    // sort(all(v));
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // int t;
  // cin>>t;
  // while(t--)
  // {
  // 	solve();
  // 	cout<<endl;
  // }
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define ll long long
#define sz(a) (int)(a.size())
#define pb push_back
#define all(c) c.begin(), c.end()
#define tr(it, c) for (auto it = c.begin(); it != c.end(); it++)
#define fr(i, n) for (int i = 0; i < n; i++)
#define present(c, x) (c.find(x) != c.end()) // for set/map etc.
#define cpresent(c, x) (find(all(c), x) != c.end())
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pair<int, int>> vp;
int N = 100001;
int mod = 1e9 + 7;
int min(int a, int b) {
  if (a < b)
    return a;
  else
    return b;
}
int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
int power(int a, int b) {
  int res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
int gcd(int p, int q) {
  if (p % q == 0)
    return q;
  else {
    return gcd(q, p % q);
  }
}
bool comp(int a, int b) { return (a < b); }

void solve() {
  int n;
  cin >> n;
  vi v(n);
  int count2[200000];
  ll sum = 0;
  fr(i, n) {
    cin >> v[i];
    sum = sum + v[i];
    count2[v[i]]++;
  }
  int q;
  cin >> q;

  int x, y;
  // int count1=0;
  sort(all(v));
  fr(i, q) {
    cin >> x >> y;
    // count1=count(all(v),x)
    // count2[x]=0
    // count1=0;
    // auto it= find(all(v),x);
    // while(it!=v.end()&&*it==x)
    // {
    // 	*it=y;
    // 	it++;
    // 	count1++;
    // }
    sum = sum - (x - y) * count2[x];
    count2[y] = count2[y] + count2[x];
    count2[x] = 0;
    cout << sum << endl;
    sort(all(v));
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // int t;
  // cin>>t;
  // while(t--)
  // {
  // 	solve();
  // 	cout<<endl;
  // }
  solve();
  return 0;
}