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

#include <bits/stdc++.h>
using namespace std;
#define FORLE(start, end, leap) for (int i(start); i <= end; i += leap)
#define FORL(start, end, leap) for (int i(start); i < end; i += leap)
#define FORGE(start, end, leap) for (int i(start); i >= end; i -= leap)
#define FORG(start, end, leap) for (int i(start); i > end; i -= leap)
#define lowbit(x) (x & (-x))
namespace BST { // Basic formulas
#define ll long long
inline ll QuickPower(ll x, ll y, ll mod) // 快速幂
{
  ll res = 1;
  while (y) {
    if (y & 1)
      res = res * x % mod;
    y >>= 1;
    x = (x * x) % mod;
  }
  return res;
}
#undef ll
template <typename T> inline void read(T &t) {
  t = 0;
  char c = getchar();
  int f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -f;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    t = (t << 3) + (t << 1) + c - '0';
    c = getchar();
  }
  t *= f;
}
template <typename T, typename... Args> inline void read(T &t, Args &...args) {
  read(t);
  read(args...);
}
template <typename T> inline void put(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x > 9)
    put(x / 10);
  putchar(x % 10 + '0');
}
template <typename T, typename... Args> inline void put(T &t, Args &...args) {
  put(t);
  putchar(' ');
  put(args...);
}
};              // namespace BST
namespace BIT { // Binary Indexed Tree
#define MAXN ((int)1e5)
int a[MAXN], c[MAXN];
inline int ask(int x) { // Ask the sum of a[1] ~ a[x]
  int ans = 0;
  while (x) {
    ans += c[x];
    x -= lowbit(x);
  }
  return ans;
}
inline void update(int x, int val) // add val to a[x]
{
  while (x <= MAXN) {
    c[x] += val;
    x += lowbit(x);
  }
}
#undef MAXN
};               // namespace BIT
namespace SEGT { // Segment Tree
#define MAXN ((int)1e6)
#define ll long long
#define ls(x) x << 1
#define rs(x) x << 1 | 1
unsigned long long a[MAXN], ans[MAXN << 2], tag[MAXN << 2];
inline void push_up(ll p) { ans[p] = ans[ls(p)] + ans[rs(p)]; }
void build(ll p, ll l, ll r) {
  tag[p] = 0;
  if (l == r) {
    ans[p] = a[l];
    return;
  }
  ll mid = (l + r) >> 1;
  build(ls(p), l, mid);
  build(rs(p), mid + 1, r);
  push_up(p);
}
inline void f(ll p, ll l, ll r, ll k) {
  tag[p] = tag[p] + k;
  ans[p] = ans[p] + k * (r - l + 1);
}
inline void push_down(ll p, ll l, ll r) {
  ll mid = (l + r) >> 1;
  f(ls(p), l, mid, tag[p]);
  f(rs(p), mid + 1, r, tag[p]);
  tag[p] = 0;
}
inline void update(ll nl, ll nr, ll l, ll r, ll p, ll k) {
  if (nl <= l && r <= nr) {
    ans[p] += k * (r - l + 1);
    tag[p] += k;
    return;
  }
  push_down(p, l, r);
  ll mid = (l + r) >> 1;
  if (nl <= mid)
    update(nl, nr, l, mid, ls(p), k);
  if (nr > mid)
    update(nl, nr, mid + 1, r, rs(p), k);
  push_up(p);
}
ll query(ll q_x, ll q_y, ll l, ll r, ll p) {
  ll res = 0;
  if (q_x <= l && r <= q_y)
    return ans[p];
  ll mid = (l + r) >> 1;
  push_down(p, l, r);
  if (q_x <= mid)
    res += query(q_x, q_y, l, mid, ls(p));
  if (q_y > mid)
    res += query(q_x, q_y, mid + 1, r, rs(p));
  return res;
}
#undef MAXN
#undef ll
#undef rs
#undef ls
}; // namespace SEGT
using namespace BST;
int n, a[100005], b, c, q;
int times[100005];
int main(void) {
  read(n);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    read(a[i]);
    sum += a[i];
    times[a[i]]++;
  }
  read(q);
  while (q--) {
    read(b, c);
    sum += times[b] * (c - b), times[c] += times[b], times[b] = 0;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FORLE(start, end, leap) for (int i(start); i <= end; i += leap)
#define FORL(start, end, leap) for (int i(start); i < end; i += leap)
#define FORGE(start, end, leap) for (int i(start); i >= end; i -= leap)
#define FORG(start, end, leap) for (int i(start); i > end; i -= leap)
#define lowbit(x) (x & (-x))
namespace BST { // Basic formulas
#define ll long long
inline ll QuickPower(ll x, ll y, ll mod) // 快速幂
{
  ll res = 1;
  while (y) {
    if (y & 1)
      res = res * x % mod;
    y >>= 1;
    x = (x * x) % mod;
  }
  return res;
}
#undef ll
template <typename T> inline void read(T &t) {
  t = 0;
  char c = getchar();
  int f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -f;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    t = (t << 3) + (t << 1) + c - '0';
    c = getchar();
  }
  t *= f;
}
template <typename T, typename... Args> inline void read(T &t, Args &...args) {
  read(t);
  read(args...);
}
template <typename T> inline void put(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x > 9)
    put(x / 10);
  putchar(x % 10 + '0');
}
template <typename T, typename... Args> inline void put(T &t, Args &...args) {
  put(t);
  putchar(' ');
  put(args...);
}
};              // namespace BST
namespace BIT { // Binary Indexed Tree
#define MAXN ((int)1e5)
int a[MAXN], c[MAXN];
inline int ask(int x) { // Ask the sum of a[1] ~ a[x]
  int ans = 0;
  while (x) {
    ans += c[x];
    x -= lowbit(x);
  }
  return ans;
}
inline void update(int x, int val) // add val to a[x]
{
  while (x <= MAXN) {
    c[x] += val;
    x += lowbit(x);
  }
}
#undef MAXN
};               // namespace BIT
namespace SEGT { // Segment Tree
#define MAXN ((int)1e6)
#define ll long long
#define ls(x) x << 1
#define rs(x) x << 1 | 1
unsigned long long a[MAXN], ans[MAXN << 2], tag[MAXN << 2];
inline void push_up(ll p) { ans[p] = ans[ls(p)] + ans[rs(p)]; }
void build(ll p, ll l, ll r) {
  tag[p] = 0;
  if (l == r) {
    ans[p] = a[l];
    return;
  }
  ll mid = (l + r) >> 1;
  build(ls(p), l, mid);
  build(rs(p), mid + 1, r);
  push_up(p);
}
inline void f(ll p, ll l, ll r, ll k) {
  tag[p] = tag[p] + k;
  ans[p] = ans[p] + k * (r - l + 1);
}
inline void push_down(ll p, ll l, ll r) {
  ll mid = (l + r) >> 1;
  f(ls(p), l, mid, tag[p]);
  f(rs(p), mid + 1, r, tag[p]);
  tag[p] = 0;
}
inline void update(ll nl, ll nr, ll l, ll r, ll p, ll k) {
  if (nl <= l && r <= nr) {
    ans[p] += k * (r - l + 1);
    tag[p] += k;
    return;
  }
  push_down(p, l, r);
  ll mid = (l + r) >> 1;
  if (nl <= mid)
    update(nl, nr, l, mid, ls(p), k);
  if (nr > mid)
    update(nl, nr, mid + 1, r, rs(p), k);
  push_up(p);
}
ll query(ll q_x, ll q_y, ll l, ll r, ll p) {
  ll res = 0;
  if (q_x <= l && r <= q_y)
    return ans[p];
  ll mid = (l + r) >> 1;
  push_down(p, l, r);
  if (q_x <= mid)
    res += query(q_x, q_y, l, mid, ls(p));
  if (q_y > mid)
    res += query(q_x, q_y, mid + 1, r, rs(p));
  return res;
}
#undef MAXN
#undef ll
#undef rs
#undef ls
}; // namespace SEGT
using namespace BST;
int n, a[100005], b, c, q;
int main(void) {
  read(n);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    read(a[i]);
    sum += a[i];
  }
  read(q);
  while (q--) {
    read(b, c);
    for (int i = 0; i < n; i++) {
      if (a[i] == b)
        sum += (c - a[i]), a[i] = c;
    }
    cout << sum << endl;
  }
}