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

#include <bits/stdc++.h>
// cervello|Phoenix
using namespace std;
#define ll long long int
#define eb emplace_back
#define mk make_pair
#define all(x) x.begin(), x.end()
#define mod 1000000007

void merge(vector<int> tree[], int treeNode) {
  int len1 = tree[2 * treeNode].size();
  int len2 = tree[2 * treeNode + 1].size();
  int index1 = 0, index2 = 0;
  while (index1 < len1 && index2 < len2) {
    if (tree[2 * treeNode][index1] > tree[2 * treeNode + 1][index2]) {

      tree[treeNode].push_back(tree[2 * treeNode + 1][index2]);
      index2++;
    } else {
      tree[treeNode].push_back(tree[2 * treeNode][index1]);
      index1++;
    }
  }
  while (index1 < len1) {
    tree[treeNode].push_back(tree[2 * treeNode][index1]);
    index1++;
  }
  while (index2 < len2) {
    tree[treeNode].push_back(tree[2 * treeNode + 1][index2]);
    index2++;
  }
  return;
}

void build(vector<int> tree[], int *arr, int start, int end, int treeNode) {
  // Base case
  if (start == end) {
    tree[treeNode].push_back(arr[start]);
    return;
  }
  int mid = (start + end) / 2;

  // Building the left tree
  build(tree, arr, start, mid, 2 * treeNode);

  // Building the right tree
  build(tree, arr, mid + 1, end, 2 * treeNode + 1);

  // Merges the right tree
  // and left tree
  merge(tree, treeNode);
  return;
}

// Function similar to query() method
// as in segment tree
int query(vector<int> tree[], int treeNode, int start, int end, int left,
          int right) {
  // Current segment is out of the range
  if (start > right || end < left) {
    return 0;
  }
  if (start >= left && end <= right) {
    return tree[treeNode].end() -
           upper_bound(tree[treeNode].begin(), tree[treeNode].end(), right);
  }

  int mid = (start + end) / 2;

  // Query on the left tree
  int op1 = query(tree, 2 * treeNode, start, mid, left, right);
  // Query on the Right tree
  int op2 = query(tree, 2 * treeNode + 1, mid + 1, end, left, right);
  return op1 + op2;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int next_right[n];
  vector<int> tree[4 * n];

  map<int, int> ump;
  for (int i = n - 1; i >= 0; i--) {
    if (ump[arr[i]] == 0) {
      next_right[i] = n;
      ump[arr[i]] = i;
    } else {
      next_right[i] = ump[arr[i]];
      ump[arr[i]] = i;
    }
  }
  build(tree, next_right, 0, n - 1, 1);

  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << query(tree, 1, 0, n - 1, l - 1, r - 1) << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// cervello|Phoenix
using namespace std;
#define ll long long int
#define eb emplace_back
#define mk make_pair
#define all(x) x.begin(), x.end()
#define mod 1000000007

void merge(vector<int> tree[], int treeNode) {
  int len1 = tree[2 * treeNode].size();
  int len2 = tree[2 * treeNode + 1].size();
  int index1 = 0, index2 = 0;
  while (index1 < len1 && index2 < len2) {
    if (tree[2 * treeNode][index1] > tree[2 * treeNode + 1][index2]) {

      tree[treeNode].push_back(tree[2 * treeNode + 1][index2]);
      index2++;
    } else {
      tree[treeNode].push_back(tree[2 * treeNode][index1]);
      index1++;
    }
  }
  while (index1 < len1) {
    tree[treeNode].push_back(tree[2 * treeNode][index1]);
    index1++;
  }
  while (index2 < len2) {
    tree[treeNode].push_back(tree[2 * treeNode + 1][index2]);
    index2++;
  }
  return;
}

void build(vector<int> tree[], int *arr, int start, int end, int treeNode) {
  // Base case
  if (start == end) {
    tree[treeNode].push_back(arr[start]);
    return;
  }
  int mid = (start + end) / 2;

  // Building the left tree
  build(tree, arr, start, mid, 2 * treeNode);

  // Building the right tree
  build(tree, arr, mid + 1, end, 2 * treeNode + 1);

  // Merges the right tree
  // and left tree
  merge(tree, treeNode);
  return;
}

// Function similar to query() method
// as in segment tree
int query(vector<int> tree[], int treeNode, int start, int end, int left,
          int right) {

  // Current segment is out of the range
  if (start > right || end < left) {
    return 0;
  }
  if (start >= left && end <= right) {
    return tree[treeNode].end() -
           upper_bound(tree[treeNode].begin(), tree[treeNode].end(), right);
  }

  int mid = (start + end) / 2;

  // Query on the left tree
  int op1 = query(tree, 2 * treeNode, start, mid, left, right);
  // Query on the Right tree
  int op2 = query(tree, 2 * treeNode + 1, mid + 1, end, left, right);
  return op1 + op2;
}

// Driver Code
int main() {

  int n, q;
  cin >> n >> q;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int next_right[n];
  // Initialising the tree
  vector<int> tree[4 * n];

  unordered_map<int, int> ump;
  for (int i = n - 1; i >= 0; i--) {
    if (ump[arr[i]] == 0) {
      next_right[i] = n;
      ump[arr[i]] = i;
    } else {
      next_right[i] = ump[arr[i]];
      ump[arr[i]] = i;
    }
  }
  build(tree, next_right, 0, n - 1, 1);

  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << query(tree, 1, 0, n - 1, l - 1, r - 1) << "\\n";
  }
}