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

#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < n; i++)
#define forr(i, a, n) for (int i = a; i <= n; i++)
#define __                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fi first
#define se second
#define pb push_back
#define all(u) u.begin(), u.end()
#define lli long long int
#define ld long double

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<vi> vvi;
const int maxn = 5e5 + 100;
int rep[maxn], num[maxn], cont = 0, n, m, bit[maxn], lstvst[maxn];
struct Query {
  int l, r, indx;
  bool operator<(Query q) const { return r < q.r; }
} Q[maxn];
void update(int p, int v) {
  for (; p <= maxn; p += (p & -p))
    bit[p] += v;
}
int query(int p) {
  if (p < 0)
    return 0;
  int ans = 0;
  for (; p; p -= (p & -p))
    ans += bit[p];
  return ans;
}
vi go() {
  vi answer(m);
  sort(Q, Q + m);
  memset(lstvst, -1, sizeof(lstvst));
  int qrAct = 0;
  forn(i, n) {
    if (lstvst[num[i]] != -1)
      update(lstvst[num[i]] + 1, -1);
    lstvst[num[i]] = i;
    update(lstvst[num[i]] + 1, 1);
    while (qrAct < m && Q[qrAct].r == i) {
      answer[Q[qrAct].indx] = query(Q[qrAct].r + 1) - query(Q[qrAct].l);
      qrAct++;
    }
  }
  return answer;
}
int main() {
  __ cin >> n >> m;
  forn(i, n) cin >> num[i];
  forn(i, m) {
    cin >> Q[i].l >> Q[i].r;
    Q[i].l--;
    Q[i].r--;
    Q[i].indx = i;
  }
  auto ans = go();
  for (int x : ans)
    cout << x << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < n; i++)
#define forr(i, a, n) for (int i = a; i <= n; i++)
#define __                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fi first
#define se second
#define pb push_back
#define all(u) u.begin(), u.end()
#define lli long long int
#define ld long double

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<vi> vvi;
const int maxn = 5e5 + 100;
int rep[maxn], num[maxn], cont = 0, n, m, blockSize;
struct Query {
  int l, r, indx;
  bool operator<(Query q) const {
    return make_pair(l / blockSize, r) < make_pair(q.l / blockSize, q.r);
  }
} Q[maxn];
void add(int i) {
  if (!rep[num[i]])
    cont++;
  rep[num[i]]++;
}
void remove(int i) {
  if (rep[num[i]] == 1)
    cont--;
  rep[num[i]]--;
}
vi mosAlgo() {
  vi answer(m);
  sort(Q, Q + m);
  int L = 0;
  int R = -1;
  forn(i, m) {
    while (L > Q[i].l) {
      L--;
      add(L);
    }
    while (R < Q[i].r) {
      R++;
      add(R);
    }
    while (L < Q[i].l) {
      remove(L);
      L++;
    }
    while (R > Q[i].r) {
      remove(R);
      R--;
    }
    answer[Q[i].indx] = cont;
  }
  return answer;
}
int main() {
  __ cin >> n >> m;
  forn(i, n) cin >> num[i];
  blockSize = (int)sqrt(m + .0) + 1;
  forn(i, m) {
    cin >> Q[i].l >> Q[i].r;
    Q[i].l--;
    Q[i].r--;
    Q[i].indx = i;
  }
  auto ans = mosAlgo();
  for (int x : ans)
    cout << x << '\\n';
  return 0;
}