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


// Problem : Replacing
// Contest : Virtual Judge - AtCoder
// URL : https://vjudge.net/problem/AtCoder-abc171_d
// Memory Limit : 1048 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#pragma GCC target("avx,avx2,fma")
// #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast") // disable
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define lll __int128
#define lp(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define vec(v, type, sz) vector<type> v(sz)
#define in(v)                                                                  \\
  for (auto &i : v)                                                            \\
  cin >> i
#define all(v) v.begin(), v.end()
#define prv(v)                                                                 \\
  for (auto i : v)                                                             \\
    cout << i << " ";                                                          \\
  cout << "\\n"
#define MOD 1000000007
#define INF 1000000000
#define INFL 1000000000000000000ll
#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define vi vector<int>
#define vl vector<ll>
#define vii(v, n, m, val) vector<vector<int>> v(n, vector<int>(m, val))
#define viii(v, i, j, k, val)                                                  \\
  vector<vector<vector<int>>> v(i, vector<vector<int>>(j, vector<int>(k, val)))
#define pii pair<int, int>
#define pb push_back
#define file_io                                                                \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define testcases                                                              \\
  int tcc;                                                                     \\
  cin >> tcc;                                                                  \\
  for (int tc = 1; tc <= tcc; tc++)
vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifdef DB
#define debug(...)                                                             \\
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

int main() {
  fast_io;
  int n;
  cin >> n;
  map<int, int> mp;
  ll sum = 0;
  lp(i, 0, n) {
    int t;
    cin >> t;
    mp[t]++;
    sum += t;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    sum -= (ll)b * mp[b];
    mp[c] += mp[b];
    sum += (ll)c * mp[b];
    mp[b] = 0;
    cout << sum << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#include <iostream>
#define pb push_back
#define sort_incr(v) sort(v.begin(), v.end());
#define sort_decr(v)                                                           \\
  {                                                                            \\
    sort(v.begin(), v.end());                                                  \\
    reverse(v.begin(), v.end());                                               \\
  }
#define take_input(v, n)                                                       \\
  for (ll i = 0, temp; i < n; i++) {                                           \\
    cin >> temp;                                                               \\
    v.pb(temp);                                                                \\
  }
#define fastio                                                                 \\
  {                                                                            \\
    ios_base::sync_with_stdio(false);                                          \\
    cin.tie(NULL);                                                             \\
  }
#define ALL(v) v.begin(), v.end()

typedef long long ll;
using namespace std;

int main() {
  fastio;
  ll n;
  cin >> n;
  vector<ll> v;
  take_input(v, n);
  ll q;
  cin >> q;
  while (q--) {
    ll a, b, su = 0;
    cin >> a >> b;
    for (ll j = 0; j < n; j++) {
      if (v[j] == a)
        v[j] = b;
      su = su + v[j];
    }
    cout << su << "\\n";
  }

  return 0;
}