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

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

typedef unsigned short int us;      // 2  0 to 65,5357
typedef unsigned int ui;            // 4  0 to 4,294,967,295
typedef unsigned long long int ull; // 8  0 to 18,446,744,073,709,551,615
typedef short int si;               // 2  -32,768 to 32,767
typedef int in;                     // 4  -2,147,483,648 to 2,147,483,647
typedef long long int
    lli;          // 8   -9,223,372,036,854,775,807 to 9,223,372,036,854,775,806
typedef char ch;  // 1
typedef float fl; // 4   +/-3.4e +/-38 (~7 digits)
typedef double dou; // 8   +/-1.7e +/-308 (~15 digits)

#define tests                                                                  \\
  ull T_;                                                                      \\
  cin >> T_;                                                                   \\
  rei(T, 1, T_)
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define v(x) std::vector<x>
#define v2(x, name, r, c) std::vector<std::vector<x>> name(r, std::vector<x>(c))
#define p(x, y) std::pair<x, y>
#define s(x) std::set<x>
#define m(x, y) std::map<x, y>
#define um(x, y) std::unordered_map<x, y>
#define ub_i(v, val) std::upper_bound(v.begin(), v.end(), val) - v.begin()
#define ub_in(v, n, val)                                                       \\
  std::upper_bound(v.begin(), v.begin() + n, val) - v.begin()
#define lb_i(v, val) std::lower_bound(v.begin(), v.end(), val) - v.begin()
#define lb_in(v, n, val)                                                       \\
  std::lower_bound(v.begin(), v.begin() + n, val) - v.begin()
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define rei(i, a, n) for (lli i = a; i <= n; ++i)
#define reit(it, a) for (auto it = a.begin(); it != a.end(); ++it)
#define red(i, a, n) for (lli i = a; i >= n; --i)
#define de(x) cout << #x << " is " << x << endl;
#define all(x) x.begin(), x.end()
#define mod 1000000007
#define sizei 100000
#define make_unique(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define te(tep) template <typename tep>
#define te2(tep1, tep2) template <typename tep1, typename tep2>
const double Pi = 2 * acos(0.0);

lli x;

te(t) void vref(v(t) & v, t n, t f(t)) {
  rei(i, 1, n) {
    t te;
    cin >> te;
    v.pb(f(te));
  }
}
te(t) t fx(t a) {
  if (a >= x)
    return a - x;
  else
    return a;
}
te(t) void vre(v(t) & v, t n) {
  rei(i, 1, n) {
    t te;
    cin >> te;
    v.pb(te);
  }
}
te(t) void vdu(v(t) v) {
  if (v.size() == 0) {
    cout << "empty!!\\n";
    return;
  }
  cout << v.size() << "(size)\\n";
  rei(i, 0, v.size() - 1) cout << v[i] << " ";
  cout << "\\n";
}
te2(t, tt) void mdu(m(t, tt) q) {
  /*if(q.size()==0){
    cout<<"empty!!\\n";
    return;
  }*/
  cout << q.size() << "(size)\\n";
  reit(i, q) {
    cout << "(" << i->ff << ":";
    vdu(i->second);
    cout << ")\\n";
  }
  cout << "\\n";
}
te2(t, tt) tt mxmap(m(t, tt) q, char x) {
  p(t, tt) mx;
  mx = {q.begin()->ff, q.begin()->ss};
  reit(i, q) {
    if (mx.x < max(mx.x, i->x))
      mx = {i->ff, i->ss};
  }
  return mx;
}
te(t) t power2(t a) {
  t val = 1, temp = mod - 2;
  while (temp > 0) {
    if (temp & 1) {
      val = (val * a) % mod;
    }
    a = (a * a) % mod;
    temp >>= 1;
  }
  return val;
}
te(t) t ma(t x, t y) { return (((x) % mod + (y) % mod) % mod); }
te(t) t ms(t x, t y) { return ((((x) % mod - (y) % mod) % mod + mod) % mod); }
te(t) t mm(t x, t y) { return (((x) % mod * (y) % mod) % mod); }
te(t) t md(t a, t b) { return (((a) % mod) * ((power2(b)) % mod)) % mod; }

/*const lli N=5*1e5+9;
bool prime_check[N];
v(lli) primes;

void seive(){
  memset(prime_check,true,sizeof(bool)*N);
  for(lli p=2;p*p<=N;++p){
    if(prime_check[p]){
      for(lli i=p*p;i<=N;i+=p){
        prime_check[i]=false;
      }
    }
  }

  rei(i,2,N){
    if(prime_check[i]) primes.pb(i);
  }
}*/

int cntA[100009];

void solve() {
  lli N;
  cin >> N;
  rei(i, 0, 100008) { cntA[i] = 0; }
  rei(i, 1, N) {
    lli tt;
    cin >> tt;
    cntA[tt]++;
  }

  lli sum = 0;
  rei(i, 0, 100008) { sum += i * cntA[i]; }
  lli Q;
  cin >> Q;
  rei(i, 1, Q) {
    lli B, C;
    cin >> B >> C;
    cntA[C] += cntA[B];
    sum += cntA[B] * (C - B);
    cntA[B] = 0;
    cout << sum << endl;
  }
}

int main() {
  /*#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  #endif*/
  // auto start=high_resolution_clock::now();
  solve();

  // auto stop=high_resolution_clock::now(); auto
  // duration=duration_cast<microseconds>(stop-start); cout<<endl<<"Time taken:
  // "<<duration.count()<<" microseconds"<<endl;
  return 0;
}

Error: TLE

Fixed submission:

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

typedef unsigned short int us;      // 2  0 to 65,5357
typedef unsigned int ui;            // 4  0 to 4,294,967,295
typedef unsigned long long int ull; // 8  0 to 18,446,744,073,709,551,615
typedef short int si;               // 2  -32,768 to 32,767
typedef int in;                     // 4  -2,147,483,648 to 2,147,483,647
typedef long long int
    lli;          // 8   -9,223,372,036,854,775,807 to 9,223,372,036,854,775,806
typedef char ch;  // 1
typedef float fl; // 4   +/-3.4e +/-38 (~7 digits)
typedef double dou; // 8   +/-1.7e +/-308 (~15 digits)

#define tests                                                                  \\
  ull T_;                                                                      \\
  cin >> T_;                                                                   \\
  rei(T, 1, T_)
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define v(x) std::vector<x>
#define v2(x, name, r, c) std::vector<std::vector<x>> name(r, std::vector<x>(c))
#define p(x, y) std::pair<x, y>
#define s(x) std::set<x>
#define m(x, y) std::map<x, y>
#define um(x, y) std::unordered_map<x, y>
#define ub_i(v, val) std::upper_bound(v.begin(), v.end(), val) - v.begin()
#define ub_in(v, n, val)                                                       \\
  std::upper_bound(v.begin(), v.begin() + n, val) - v.begin()
#define lb_i(v, val) std::lower_bound(v.begin(), v.end(), val) - v.begin()
#define lb_in(v, n, val)                                                       \\
  std::lower_bound(v.begin(), v.begin() + n, val) - v.begin()
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define rei(i, a, n) for (lli i = a; i <= n; ++i)
#define reit(it, a) for (auto it = a.begin(); it != a.end(); ++it)
#define red(i, a, n) for (lli i = a; i >= n; --i)
#define de(x) cout << #x << " is " << x << endl;
#define all(x) x.begin(), x.end()
#define mod 1000000007
#define sizei 100000
#define make_unique(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define te(tep) template <typename tep>
#define te2(tep1, tep2) template <typename tep1, typename tep2>
const double Pi = 2 * acos(0.0);

lli x;

te(t) void vref(v(t) & v, t n, t f(t)) {
  rei(i, 1, n) {
    t te;
    cin >> te;
    v.pb(f(te));
  }
}
te(t) t fx(t a) {
  if (a >= x)
    return a - x;
  else
    return a;
}
te(t) void vre(v(t) & v, t n) {
  rei(i, 1, n) {
    t te;
    cin >> te;
    v.pb(te);
  }
}
te(t) void vdu(v(t) v) {
  if (v.size() == 0) {
    cout << "empty!!\\n";
    return;
  }
  cout << v.size() << "(size)\\n";
  rei(i, 0, v.size() - 1) cout << v[i] << " ";
  cout << "\\n";
}
te2(t, tt) void mdu(m(t, tt) q) {
  /*if(q.size()==0){
    cout<<"empty!!\\n";
    return;
  }*/
  cout << q.size() << "(size)\\n";
  reit(i, q) {
    cout << "(" << i->ff << ":";
    vdu(i->second);
    cout << ")\\n";
  }
  cout << "\\n";
}
te2(t, tt) tt mxmap(m(t, tt) q, char x) {
  p(t, tt) mx;
  mx = {q.begin()->ff, q.begin()->ss};
  reit(i, q) {
    if (mx.x < max(mx.x, i->x))
      mx = {i->ff, i->ss};
  }
  return mx;
}
te(t) t power2(t a) {
  t val = 1, temp = mod - 2;
  while (temp > 0) {
    if (temp & 1) {
      val = (val * a) % mod;
    }
    a = (a * a) % mod;
    temp >>= 1;
  }
  return val;
}
te(t) t ma(t x, t y) { return (((x) % mod + (y) % mod) % mod); }
te(t) t ms(t x, t y) { return ((((x) % mod - (y) % mod) % mod + mod) % mod); }
te(t) t mm(t x, t y) { return (((x) % mod * (y) % mod) % mod); }
te(t) t md(t a, t b) { return (((a) % mod) * ((power2(b)) % mod)) % mod; }

/*const lli N=5*1e5+9;
bool prime_check[N];
v(lli) primes;

void seive(){
  memset(prime_check,true,sizeof(bool)*N);
  for(lli p=2;p*p<=N;++p){
    if(prime_check[p]){
      for(lli i=p*p;i<=N;i+=p){
        prime_check[i]=false;
      }
    }
  }

  rei(i,2,N){
    if(prime_check[i]) primes.pb(i);
  }
}*/

void solve() {
  lli N;
  cin >> N;
  m(lli, lli) A_map_val_size;
  rei(i, 1, N) {
    lli tval;
    cin >> tval;
    A_map_val_size[tval]++;
  }

  lli Q;
  cin >> Q;
  rei(i, 1, Q) {
    lli B, C;
    cin >> B >> C;
    A_map_val_size[C] +=
        (A_map_val_size.find(B) != A_map_val_size.end() ? A_map_val_size[B]
                                                        : 0);
    (A_map_val_size.find(B) != A_map_val_size.end() ? A_map_val_size.erase(B)
                                                    : 0);
    lli sum = 0;
    reit(it, A_map_val_size) { sum += (it->ff) * (it->ss); }
    cout << sum << endl;
  }
}

int main() {
  /*#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  #endif*/
  // auto start=high_resolution_clock::now();
  solve();

  // auto stop=high_resolution_clock::now(); auto
  // duration=duration_cast<microseconds>(stop-start); cout<<endl<<"Time taken:
  // "<<duration.count()<<" microseconds"<<endl;
  return 0;
}