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

#include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int dx[] = {0, 1, -1, 0, 1, -1, 1, -1}; // i<4:4way i<8:8way
int dy[] = {1, 0, 0, -1, 1, -1, -1, 1};

const ll mod = 1e9 + 7;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);

template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
  // 下の2個の操作を使う時は上の2個の操作が使えない事に注意
  T getmax(int i) { // get max(d[i]) for i = 0,1,...,i-1,i
    T x = 0;
    for (i++; i; i -= i & -i) {
      x = max(x, d[i]);
    }
    return x;
  }
  void setmax(int i, int v) { // set d[i] = max(d[i], v)
    for (i++; i <= n; i += i & -i) {
      d[i] = max(d[i], v);
    }
  }
};

vector<int> pos(500005, -1);

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int n, q;
  cin >> n >> q;
  BIT<int> bit(500005);
  vector<int> c(n), ans(q);
  rep(i, n) { cin >> c[i]; }
  vector<tuple<int, int, int>> p;
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    p.push_back({l, r, i});
  }
  sort(ALL(p), [](auto a, auto b) { return get<1>(a) < get<1>(b); });
  int nowpos = 0;
  rep(i, q) {
    int ret = 0;
    int l = get<0>(p[i]);
    int r = get<1>(p[i]);
    for (int j = nowpos; j < r; j++) {
      int now = c[j];
      if (pos[now] == -1) {
        pos[now] = j;
      } else {
        bit.add(pos[now] + 1, -1);
        pos[now] = j;
      }
      bit.add(j + 1, 1);
    }
    nowpos = r;
    ans[get<2>(p[i])] = bit.sum(r) - bit.sum(l - 1);
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int dx[] = {0, 1, -1, 0, 1, -1, 1, -1}; // i<4:4way i<8:8way
int dy[] = {1, 0, 0, -1, 1, -1, -1, 1};

const ll mod = 1e9 + 7;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);

template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
  // 下の2個の操作を使う時は上の2個の操作が使えない事に注意
  T getmax(int i) { // get max(d[i]) for i = 0,1,...,i-1,i
    T x = 0;
    for (i++; i; i -= i & -i) {
      x = max(x, d[i]);
    }
    return x;
  }
  void setmax(int i, int v) { // set d[i] = max(d[i], v)
    for (i++; i <= n; i += i & -i) {
      d[i] = max(d[i], v);
    }
  }
};

vector<int> pos(500005, -1);

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int n, q;
  cin >> n >> q;
  BIT<int> bit(500005);
  vector<int> c(n), ans(q);
  rep(i, n) { cin >> c[i]; }
  vector<tuple<int, int, int>> p;
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    p.push_back({l, r, i});
  }
  sort(ALL(p), [](auto a, auto b) { return get<1>(a) < get<1>(b); });
  int nowpos = 0;
  rep(i, q) {
    int ret = 0;
    int l = get<0>(p[i]);
    int r = get<1>(p[i]);
    for (int j = nowpos; j < r; j++) {
      int now = c[j];
      if (pos[now] == -1) {
        pos[now] = j;
      } else {
        bit.add(pos[now] + 1, -1);
        pos[now] = j;
      }
      bit.add(j + 1, 1);
    }
    ans[get<2>(p[i])] = bit.sum(r) - bit.sum(l - 1);
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
}