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

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;

struct Query {
  int l, r, i;
  int ans;
  Query(int _l, int _r, int _i) : l(_l), r(_r), i(_i){};
};

class BIT {
public:
  int N;
  vi arr;
  BIT(int n) {
    N = n;
    arr = vi(N + 1, 0);
  }
  int sum(int x) {
    int s = 0;
    for (int i = x + 1; i > 0; i -= i & (-i)) {
      s += arr[i];
    }
    return s;
  }
  void add(int x, int k) {
    for (int i = x + 1; i <= N; i += i & (-i)) {
      arr[i] += k;
    }
  }
  int get(int l, int r) { return sum(r) - sum(l - 1); }
};

int main() {
  int N, q;
  cin >> N >> q;
  vvi pos(N);
  vi A(N);
  rep(i, N) {
    cin >> A[i];
    A[i]--;
    pos[A[i]].push_back(i);
  }
  vector<Query> query;
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    query.emplace_back(l, r, i);
  }
  sort(rng(query), [](Query l, Query r) { return l.l < r.l; });
  // 各色において、範囲内のうちもっとも左側にある頂点
  BIT imp(N);
  vi isimp(N, 0);
  int nl = query[0].l;
  {
    int nr = query[0].r;
    srep(k, nl, N) {
      int c = A[k];
      int p = lower_bound(rng(pos[c]), k) - pos[c].begin();
      if (p == 0) {
        imp.add(k, 1);
        isimp[k]++;
      }
    }
    query[0].ans = imp.get(nl, nr);
  }
  srep(i, 1, q) {
    int l = query[i].l;
    int r = query[i].r;
    srep(k, nl, l) {
      if (!isimp[k])
        continue;
      int c = A[k];
      int p = lower_bound(rng(pos[c]), k) - pos[c].begin();
      if (p == sz(pos[c]) - 1)
        continue;
      p++;
      isimp[pos[c][p]]++;
      imp.add(pos[c][p], 1);
    }
    query[i].ans = imp.get(l, r);
    nl = l;
  }
  sort(rng(query), [&](Query l, Query r) { return l.i < r.i; });
  for (Query qu : query) {
    cout << qu.ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;

class BIT {
public:
  int N;
  vi arr;
  BIT(int n) {
    N = n;
    arr = vi(N + 1, 0);
  }
  int sum(int x) {
    int s = 0;
    for (int i = x + 1; i > 0; i -= i & (-i)) {
      s += arr[i];
    }
    return s;
  }
  void add(int x, int k) {
    for (int i = x + 1; i <= N; i += i & (-i)) {
      arr[i] += k;
    }
  }
  int get(int l, int r) { return sum(r) - sum(l - 1); }
};

int main() {
  int N, q;
  cin >> N >> q;
  vvi pos(N);
  vi A(N);
  rep(i, N) {
    cin >> A[i];
    A[i]--;
    pos[A[i]].push_back(i);
  }
  vp query(q);
  map<P, int> id;
  rep(i, q) {
    cin >> query[i].first >> query[i].second;
    query[i].first--;
    query[i].second--;
    id[query[i]] = i;
  }
  sort(rng(query));
  // 各色において、範囲内のうちもっとも左側にある頂点
  BIT imp(N);
  vi isimp(N, 0);
  vector<pair<P, int>> ca;
  int nl = query[0].first;
  {
    int nr = query[0].second;
    srep(k, nl, N) {
      int c = A[k];
      int p = lower_bound(rng(pos[c]), k) - pos[c].begin();
      if (p == 0) {
        imp.add(k, 1);
        isimp[k]++;
      }
    }
    ca.emplace_back(query[0], imp.get(nl, nr));
  }
  srep(i, 1, q) {
    int l = query[i].first;
    int r = query[i].second;
    srep(k, nl, l) {
      if (!isimp[k])
        continue;
      int c = A[k];
      int p = lower_bound(rng(pos[c]), k) - pos[c].begin();
      if (p == sz(pos[c]) - 1)
        continue;
      p++;
      isimp[pos[c][p]]++;
      imp.add(pos[c][p], 1);
    }
    ca.emplace_back(query[i], imp.get(l, r));
    nl = l;
  }
  sort(rng(ca), [&](pair<P, int> l, pair<P, int> r) {
    return id[l.first] < id[r.first];
  });
  for (pair<P, int> x : ca) {
    cout << x.second << endl;
  }
  return 0;
}