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

/*input
2
1 2
3
1 100
2 100
100 1000
*/

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tgmath.h>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define ar array
#define scd(t) scanf("%d", &t)
#define scld(t) scanf("%ld", &t)
#define sclld(t) scanf("%lld", &t)
#define scc(t) scanf("%c", &t)
#define scs(t) scanf("%s", t)
#define scf(t) scanf("%f", &t)
#define sclf(t) scanf("%lf", &t)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define tests                                                                  \\
  int r;                                                                       \\
  cin >> r;                                                                    \\
  while (r--)
#define MP make_pair
#define PI acos(-1)
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int, int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
#define godspeed                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(0);

#define M(x) ((x) % (1000000007))

int arr[1010][1010];

int h;
int w;

bool legal(int x, int y) {
  if (x >= 1 && x <= h) {
    if (y >= 1 && y <= w) {
      if (arr[x][y]) {
        return true;
      }
    }
  }
  return false;
}

int main() {
  int64 n;
  cin >> n;
  int64 arr[n];
  memset(arr, 0, sizeof(arr));
  map<int, int> mp;
  int64 sum = 0;
  for (int64 i = 0; i < n; i++) {
    int a;
    cin >> a;
    arr[i] = a;
    mp[a]++;
    sum += a;
  }
  int q;
  cin >> q;
  rep(i, q) {
    int b;
    int c;
    cin >> b >> c;
    int g = c - b;
    if (mp.find(b) != mp.end()) {
      int p = mp[b];
      sum += p * g;
      auto it = mp.find(b);
      mp.erase(it);
      mp[c] += p;
    }
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

/*input
2
1 2
3
1 100
2 100
100 1000
*/

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define ar array
#define scd(t) scanf("%d", &t)
#define scld(t) scanf("%ld", &t)
#define sclld(t) scanf("%lld", &t)
#define scc(t) scanf("%c", &t)
#define scs(t) scanf("%s", t)
#define scf(t) scanf("%f", &t)
#define sclf(t) scanf("%lf", &t)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define tests                                                                  \\
  int r;                                                                       \\
  cin >> r;                                                                    \\
  while (r--)
#define MP make_pair
#define PI acos(-1)
typedef pair<long long, long long> PII;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int, int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
#define godspeed                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(0);

#define M(x) ((x) % (1000000007))

int64 n;
int64 q;
int64 segment[400009];
int64 arr[400009];

int64 sum(int64 a, int64 b) {
  a = a + n;
  b = b + n;
  int64 s = 0;
  while (a <= b) {
    if (a % 2 == 1) {
      s = s + segment[a];
      a++;
      // this is because if a is odd, then it is a right child. However, a
      // itself is supposed to be the left part, so you have to consider it, and
      // then move a to propogate upwards.
    }
    if (b % 2 == 0) {
      s = s + segment[b];
      b--;
      // similar to above, if b is even, it is a left child, but b itself is
      // supposed to be the right part. Thus, you have to consider that element,
      // and then move it to one before to propogate upwards.
    }
    a = a / 2;
    b = b / 2;
  }
  return s;
}

void add(int64 k, int64 x) {
  k += n;
  segment[k] += x;
  for (k /= 2; k >= 1; k /= 2) {
    segment[k] = segment[2 * k] + segment[2 * k + 1];
  }
}

int main() {
  godspeed;
  cin >> n;
  memset(segment, 0, sizeof(segment));
  memset(arr, 0, sizeof(arr));
  for (int i = 1; i <= n; i++) {
    int64 a;
    cin >> a;
    arr[i] = a;
    add(i, a);
  }
  cin >> q;
  rep(i, q) {
    int b;
    int c;
    cin >> b >> c;
    for (int i = 1; i <= n; i++) {
      if (arr[i] == b) {
        add(i, c - b);
        arr[i] = c;
      }
    }
    cout << sum(1, n) << endl;
  }
}