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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int read() {
  char x;
  while ((x = getchar()) > '9' || x < '0')
    ;
  int u = x - '0';
  while ((x = getchar()) <= '9' && x >= '0')
    u = (u << 3) + (u << 1) + x - '0';
  return u;
}
int buf[105];
inline void write(int i) {
  int p = 0;
  if (i == 0)
    p++;
  else
    while (i) {
      buf[p++] = i % 10;
      i /= 10;
    }
  for (int j = p - 1; j >= 0; j--)
    putchar('0' + buf[j]);
}
int a[500005], Ans[500005], cnt[500005], ans = 0, n, m, bl;
struct node {
  int l, r, id;
} q[500005];

inline bool cmp(const node a, const node b) {
  return (a.l / bl) ^ (b.l / bl) ? a.l < b.l
                                 : (((a.l / bl) & 1) ? a.r < b.r : a.r > b.r);
}

inline void del(int x) {
  --cnt[a[x]];
  if (!cnt[a[x]])
    --ans;
}
inline void add(int x) {
  if (!cnt[a[x]])
    ++ans;
  ++cnt[a[x]];
}

int main() {
  int n, m;
  n = read();
  m = read();
  bl = n / sqrt(m * 2 / 3);
  for (int i = 1; i <= n; ++i)
    a[i] = read();

  for (int i = 1; i <= m; ++i) {
    q[i].l = read(), q[i].r = read();
    q[i].id = i;
  }
  sort(q + 1, q + 1 + m, cmp);

  int l = q[1].l, r = q[1].l - 1;
  for (int i = 1; i <= m; ++i) {
    int stdl = q[i].l, stdr = q[i].r;

    while (l < stdl)
      del(l++);

    while (l > stdl)
      add(--l);

    while (r < stdr)
      add(++r);

    while (r > stdr)
      del(r--);

    Ans[q[i].id] = ans;
  }

  for (int i = 1; i <= m; ++i)
    write(Ans[i]), printf("\\n");
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int read() {
  char x;
  while ((x = getchar()) > '9' || x < '0')
    ;
  int u = x - '0';
  while ((x = getchar()) <= '9' && x >= '0')
    u = (u << 3) + (u << 1) + x - '0';
  return u;
}
int buf[105];
inline void write(int i) {
  int p = 0;
  if (i == 0)
    p++;
  else
    while (i) {
      buf[p++] = i % 10;
      i /= 10;
    }
  for (int j = p - 1; j >= 0; j--)
    putchar('0' + buf[j]);
}
int a[500005], Ans[500005], cnt[500005], ans = 0, n, m, bl;
struct node {
  int l, r, id;
} q[500005];

bool cmp(const node a, const node b) {
  return (a.l / bl) ^ (b.l / bl) ? a.l < b.l
                                 : (((a.l / bl) & 1) ? a.r < b.r : a.r > b.r);
}

inline void del(int x) {
  --cnt[a[x]];
  if (!cnt[a[x]])
    --ans;
}
inline void add(int x) {
  if (!cnt[a[x]])
    ++ans;
  ++cnt[a[x]];
}

int main() {
  int n, m;
  n = read();
  m = read();
  bl = n / sqrt(2 / 3 * m);
  for (int i = 1; i <= n; ++i)
    a[i] = read();

  for (int i = 1; i <= m; ++i) {
    q[i].l = read(), q[i].r = read();
    q[i].id = i;
  }
  sort(q + 1, q + 1 + m, cmp);

  int l = q[1].l, r = q[1].l - 1;
  for (int i = 1; i <= m; ++i) {
    int stdl = q[i].l, stdr = q[i].r;

    while (l < stdl)
      del(l++);

    while (l > stdl)
      add(--l);

    while (r < stdr)
      add(++r);

    while (r > stdr)
      del(r--);

    Ans[q[i].id] = ans;
  }

  for (int i = 1; i <= m; ++i)
    write(Ans[i]), printf("\\n");
  return 0;
}