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

// from https://blog.csdn.net/qq_37383726/article/details/79564065
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

const int N = 5e5 + 11;
const int M = 500000 + 11;
const int MAX = 5e5 + 11;

int readint() // 用于整数
{
  char c;
  while (c = getchar(), '-' != c && !isdigit(c))
    if (c == EOF)
      return EOF;
  int f = 1;
  if ('-' == c)
    f = -1, c = getchar();
  int x = c - '0';
  while (isdigit(c = getchar()))
    x = x * 10 + c - '0';
  return x * f;
}

void write(int a) { // 用于正整数
  if (a > 9)
    write(a / 10);
  putchar(a % 10 + '0');
}

int B;
struct Query {
  int le, ri;
  int id;
  bool operator<(const Query &b) const {
    if (le / B == b.le / B)
      return ri < b.ri;
    else
      return le / B < b.le / B;
  }
} Q[M];
int ANS[M];

int cnt[MAX];
int a[N];
int ans;
void Add(int x) {
  x = a[x];
  if (cnt[x] == 0)
    ans++;
  cnt[x]++;
}
void Del(int x) {
  x = a[x];
  cnt[x]--;
  if (cnt[x] == 0)
    ans--;
}

void solve(int q) {
  sort(Q + 1, Q + q + 1);
  // memset(cnt, 0, sizeof(cnt));
  int L = 1, R = 0;
  ans = 0;
  for (int i = 1; i <= q; i++) {
    while (R < Q[i].ri)
      Add(++R);
    while (R > Q[i].ri)
      Del(R--);
    while (L < Q[i].le)
      Del(L++);
    while (L > Q[i].le)
      Add(--L);

    ANS[Q[i].id] = ans;
  }
  for (int i = 1; i <= q; i++) {
    write(ANS[i]);
    puts("");
  }
}

int n, q;
int main() {
#ifdef LOCAL
  freopen("in", "r", stdin);
  // freopen("out", "w", stdout);
#endif
  n = readint();
  q = readint();
  B = (int)sqrt(n * 1.0);
  for (int i = 1; i <= n; i++)
    a[i] = readint();
  for (int i = 1; i <= q; i++) {
    Q[i].le = readint();
    Q[i].ri = readint();
    // scanf("%d%d", &Q[i].le, &Q[i].ri);
    Q[i].id = i;
  }
  solve(q);
  return 0;
}

Error: TLE

Fixed submission:

// from https://blog.csdn.net/qq_37383726/article/details/79564065

#include <bits/stdc++.h>
using namespace std;
#define LL long long

const int N = 5e5 + 11;
const int M = 500000 + 11;
const int MAX = 1e6 + 11;

const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int inff = 0x3f3f3f3f3f3f3f3f;

int B;
struct Query {
  int le, ri;
  int id;
  bool operator<(const Query &b) const {
    if (le / B == b.le / B)
      return ri < b.ri;
    else
      return le / B < b.le / B;
  }
} Q[M];
int ANS[M];

int cnt[MAX];
int a[N];
int ans;
void Add(int x) {
  x = a[x];
  if (cnt[x] == 0)
    ans++;
  cnt[x]++;
}
void Del(int x) {
  x = a[x];
  cnt[x]--;
  if (cnt[x] == 0)
    ans--;
}

void solve(int q) {
  sort(Q + 1, Q + q + 1);
  memset(cnt, 0, sizeof(cnt));
  int L = 1, R = 0;
  ans = 0;
  for (int i = 1; i <= q; i++) {
    while (R < Q[i].ri)
      Add(++R);
    while (R > Q[i].ri)
      Del(R--);
    while (L < Q[i].le)
      Del(L++);
    while (L > Q[i].le)
      Add(--L);

    ANS[Q[i].id] = ans;
  }
  for (int i = 1; i <= q; i++)
    printf("%d\\n", ANS[i]);
}

int n, q;
int main() {
#ifdef LOCAL
  freopen("in", "r", stdin);
  // freopen("out", "w", stdout);
#endif
  scanf("%d%d", &n, &q);
  B = (int)sqrt(n * 1.0);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &Q[i].le, &Q[i].ri);
    Q[i].id = i;
  }
  solve(q);
  return 0;
}