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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, s, t) for (register int i = s; i <= t; ++i)
#define _rep(i, s, t) for (register int i = s; i >= t; --i)
#define Rep(i, s, t) for (register int i = s; i < t; ++i)
#define go(x) for (register int e = las[x]; e; e = nxt[e])
#define re register
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define is insert
#define es erase
#define pii pair<int, int>
#define ms(f, x) memset(f, x, sizeof f)
#define mc(f, x) memcpy(f, x, sizeof f)
#define open(x) freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
#define gi(x) read(x)
#define gii(x, y) read(x), read(y)
#define giii(x, y, z) read(x), read(y), read(z)
namespace IO {
#define gc getchar()
#define pc(x) putchar(x)
template <typename T> inline void read(T &x) {
  x = 0;
  int f = 1;
  char ch = gc;
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      f = -1;
    ch = gc;
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = gc;
  x *= f;
  return;
}
template <typename T> inline void write(T x = 0) {
  T wr[51];
  wr[0] = 0;
  if (x < 0)
    pc('-'), x = -x;
  if (!x)
    pc(48);
  while (x)
    wr[++wr[0]] = x % 10, x /= 10;
  while (wr[0])
    pc(48 + wr[wr[0]--]);
  return;
}
} // namespace IO
using IO::read;
using IO::write;
typedef long long ll;
typedef double db;
typedef long double ld;
typedef unsigned long long ull;
using namespace std;
const int N = 2e6 + 11;
int n, q, a[N], vis[N];
struct node {
  int l, r, id;
  void in(int i) {
    gi(l), gi(r);
    id = i;
  }
  bool operator<(node A) const { return r < A.r; }
} p[N];
int s[N], ans[N];
void add(int x, int v) {
  for (; x > 0; x -= x & -x)
    s[x] += v;
}
int query(int x) {
  int ans = 0;
  for (; x <= n; x += x & -x)
    ans += s[x];
  return ans;
}
int main() {
  gi(n), gi(q);
  rep(i, 1, n) gi(a[i]);
  rep(i, 1, q) p[i].in(i);
  sort(p + 1, p + q + 1);
  int now = 1;
  rep(i, 1, n) {
    if (vis[a[i]])
      add(vis[a[i]], -1);
    add(i, 1);
    vis[a[i]] = i;
    while (i == p[now].r) {
      ans[p[now].id] = query(p[now].l);
      ++now;
    }
  }
  rep(i, 1, q) printf("%d\\n", ans[i]);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <math.h>
#include <stdio.h>
#define FOR(i, s, t) for (register int i = s; i <= t; ++i)
using std::sort;
using std::swap;
const int N = 1e6 + 11, M = 1e6 + 11;
int blo, n, m, l, r, tot, now, res;
struct qs {
  int l, r, id;
  inline bool operator<(qs A) const {
    if (l / blo != A.l / blo)
      return l < A.l;
    if (r / blo != A.r / blo)
      return r < A.r;
    return id < A.id;
  }
} q[N];
int a[N], ans[N], hv[M];
inline void add(int x) {
  if (!hv[x])
    ++res;
  ++hv[x];
}
inline void del(int x) {
  --hv[x];
  if (!hv[x])
    --res;
}
int main() {
  scanf("%d%d", &n, &m);
  FOR(i, 1, n) scanf("%d", a + i);
  FOR(i, 1, m) {
    scanf("%d%d", &l, &r);
    q[++tot] = (qs){l, r, i};
  }
  blo = (int)sqrt(n);
  sort(q + 1, q + tot + 1);
  l = 1;
  r = 0;
  now = 0;
  FOR(i, 1, m) {
    while (l < q[i].l)
      del(a[l++]);
    while (q[i].l < l)
      add(a[--l]);
    while (r < q[i].r)
      add(a[++r]);
    while (q[i].r < r)
      del(a[r--]);
    ans[q[i].id] = res;
  }
  FOR(i, 1, tot) printf("%d\\n", ans[i]);
  return 0;
}