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

#pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()

#pragma region UnionFind
struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};
#pragma endregion
#pragma region GCD
int gcd(int a, int b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
#pragma endregion
#pragma region chmin
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma region chmax
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma endregion

#pragma region BIT
struct BIT {
  vector<int> bit;
  int n;
  // i-indexed
  BIT() { init(-1); }
  BIT(int n_) { init(n_); }
  void init(int n_) {
    n = n_;
    bit.clear();
    bit.resize(n + 1, 0);
  }
  int sum(int i) {
    int s = 0;
    while (i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }
  void add(int i, int x) {
    if (i == 0)
      return;
    while (i <= n) {
      bit[i] += x;
      i += i & -i;
    }
  }
  int sum0(int i) { return sum(i + 1); }
  void add0(int i, int x) { add(i + 1, x); }
};
#pragma endregion

int main() {
  int n, q;
  cin >> n >> q;
  vector<vector<int>> v(500100);
  vector<int> c(n);
  rep(i, n) cin >> c[i];
  vector<pair<int, int>> query(q);
  vector<vector<int>> u(n);
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    --l, --r;
    query[i] = {l, r};
    u[l].push_back(r);
  }
  map<pair<int, int>, int> mp;
  BIT bit(n);
  rrep(i, n) {
    if (v[c[i]].size() > 0)
      bit.add(v[c[i]].back(), 1);
    v[c[i]].push_back(i);
    for (int x : u[i]) {
      mp[{i, x}] = x - i + 1 - bit.sum(x);
    }
  }
  rep(i, q) cout << mp[{query[i].first, query[i].second}] << endl;
}

Error: TLE

Fixed submission:

#pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()

#pragma region UnionFind
struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};
#pragma endregion
#pragma region GCD
int gcd(int a, int b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
#pragma endregion
#pragma region chmin
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma region chmax
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma endregion

int main() {
  int n, q;
  cin >> n >> q;
  vector<vector<int>> v(500100);
  vector<int> c(n);
  rep(i, n) cin >> c[i];
  vector<pair<int, int>> query(q);
  vector<vector<int>> u(n);
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    --l, --r;
    query[i] = {l, r};
    u[l].push_back(r);
  }
  map<pair<int, int>, int> mp;
  multiset<int> s;
  rrep(i, n) {
    if (v[c[i]].size() > 0)
      s.insert(v[c[i]].back());
    v[c[i]].push_back(i);
    for (int x : u[i]) {
      auto it = s.upper_bound(x);
      int pos = distance(s.begin(), it);
      mp[{i, x}] = x - i + 1 - pos;
    }
  }
  rep(i, q) cout << mp[{query[i].first, query[i].second}] << endl;
}