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

/*
 *  Created by Andrei Arhire 23/07/2020
 *  Copyright © 2020 Andrei Arhire. All rights reserved.
 */
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define lll long long
#define llf __float128
#define lld long double
using namespace std;
const int N = 5e5 + 10, oo = 1e9 + 10, MOD = 1e9 + 7;
const long double pi = 2 * acos(0.0);

int read() {
  int x = 0, f = 1;
  char ch;
  while (!isdigit(ch = getchar()))
    (ch == '-') && (f = -f);
  for (x = ch ^ 48; isdigit(ch = getchar());
       x = (x << 3) + (x << 1) + (ch ^ 48))
    ;
  return x * f;
}
int n, m, len, a[N], bel[N], cnt[N];
lll res, ans[N];
struct node {
  int l, r, id;
} q[N];
// bool cmp(node x, node y) {return bel[x.l] == bel[y.l] ? bel[x.l] < bel[y.l] :
// x.r < y.r;}
bool cmp(node x, node y) {
  return (bel[x.l] == bel[y.l]) ? (bel[x.l] % 2 == 1 ? x.r < y.r : x.r > y.r)
                                : x.l < y.l;
}
signed main() {
  int x, y, i, temp;
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  n = read();
  m = read();
  len = sqrt(n);
  for (int i = 1; i <= n; ++i)
    a[i] = read(), bel[i] = (i - 1) / len + 1;
  for (int i = 1; i <= m; ++i)
    q[i].l = read(), q[i].r = read(), q[i].id = i;
  int l = 1, r = 0;
  sort(q + 1, q + m + 1, cmp);
  for (int i = 1; i <= m; ++i) {
    while (l > q[i].l) {
      x = --l;
      cnt[a[x]]++;
      if (cnt[a[x]] == 1)
        ++res;
    }
    while (r < q[i].r) {
      x = ++r;
      cnt[a[x]]++;
      if (cnt[a[x]] == 1)
        ++res;
    }
    while (l < q[i].l) {
      x = l++;
      cnt[a[x]]--;
      if (cnt[a[x]] == 0)
        --res;
    }
    while (r > q[i].r) {
      x = r--;
      cnt[a[x]]--;
      if (cnt[a[x]] == 0)
        --res;
    }
    ans[q[i].id] = res;
  }
  for (int i = 1; i <= m; ++i)
    printf("%lld\\n", ans[i]);

  return 0;
}

Error: TLE

Fixed submission:

/*
 *  Created by Andrei Arhire 23/07/2020
 *  Copyright © 2020 Andrei Arhire. All rights reserved.
 */
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define lll long long
#define llf __float128
#define lld long double
using namespace std;
const int NR = 5e5 + 10, oo = 1e9 + 10, MOD = 1e9 + 7;
const long double pi = 2 * acos(0.0);

int n, q, a[NR], rad, viz[NR], st, dr;
int ans[NR], sum, curr;

struct segment {
  int x, y, i;
} seg[NR];

bool cmp(segment i, segment j) {
  return i.x / rad < j.x / rad || (i.y < j.y && i.x / rad == j.x / rad);
}

signed main() {
  int x, y, i;
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q;
  rad = sqrt(n);
  for (i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (i = 1; i <= q; ++i) {
    cin >> seg[i].x >> seg[i].y;
    seg[i].i = i;
  }
  sort(seg + 1, seg + q + 1, cmp);
  st = 0;
  dr = 0;
  curr = 0;
  for (i = 1; i <= q; ++i) {

    for (; st < seg[i].x; ++st) {
      viz[a[st]]--;
      if (viz[a[st]] == 0)
        curr--;
    }
    for (; dr < seg[i].y;) {
      ++dr;
      viz[a[dr]]++;
      if (viz[a[dr]] == 1)
        curr++;
    }
    for (; st > seg[i].x;) {
      --st;
      viz[a[st]]++;
      if (viz[a[st]] == 1)
        curr++;
    }
    for (; dr > seg[i].y; --dr) {
      viz[a[dr]]--;
      if (viz[a[dr]] == 0)
        curr--;
    }
    ans[seg[i].i] = curr;
  }
  for (i = 1; i <= q; ++i) {
    cout << ans[i] << '\\n';
  }

  return 0;
}