Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
    Auther: ghoshashis545 Ashis Ghosh
    College: Jalpaiguri Govt Enggineering College
*/
#include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll int
#define int long long
#define ld long double
#define ff first
#define ss string
#define se second
#define endl "\n"
#define alt(v) v.begin(), v.end()
#define ralt(v) v.rbegin(), v.rend()
#define pb emplace_back
#define mp make_pair
#define fab(i, a, b) for (int i = (a); i < (b); i++)
#define fba(i, a, b) for (int i = (b); i >= (a); i--)
#define level 20
int mod = 1000000007;
// int mod=998244353;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};
bool test = 0;

struct query {
  int l, r, i, ans;
};

int n, q, x, y;
void solve() {

  cin >> n >> q;
  int sqrtn = (int)sqrt(n) + 1;
  int a[n + 5];

  for (int i = 0; i < n; i++)
    cin >> a[i];

  query v[q];

  for (int i = 0; i < q; i++) {
    cin >> v[i].l >> v[i].r;
    v[i].i = i;
    v[i].l--;
    v[i].r--;
  }
  sort(v, v + q, [&sqrtn](query &a, query &b) {
    if (a.l / sqrtn != b.l / sqrtn)
      return a.l / sqrtn < b.l / sqrtn;
    return a.r < b.r;
  });

  map<int, int> m;
  int curl = 1, curr = 0;
  int cnt = 0;
  for (int i = 0; i < q; i++) {

    while (curr < v[i].r) {
      curr++;
      if (m[a[curr]] == 0)
        cnt++;
      m[a[curr]]++;
    }
    while (curl > v[i].l) {
      curl--;
      if (m[a[curl]] == 0)
        cnt++;
      m[a[curl]]++;
    }
    while (curr > v[i].r) {
      if (m[a[curr]] == 1)
        cnt--;
      m[a[curr]]--;
      curr--;
    }
    while (curl < v[i].l) {
      if (m[a[curl]] == 1)
        cnt--;
      m[a[curl]]--;
      curl++;
    }
    v[i].ans = cnt;
  }
  sort(v, v + q, [&](query &a, query &b) { return a.i < b.i; });
  for (int i = 0; i < q; i++)
    cout << v[i].ans << "\n";
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  if (test)
    cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
/*
    Auther: ghoshashis545 Ashis Ghosh
    College: Jalpaiguri Govt Enggineering College
*/
#include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll int
#define int long long
#define ld long double
#define ff first
#define ss string
#define se second
#define endl "\n"
#define alt(v) v.begin(), v.end()
#define ralt(v) v.rbegin(), v.rend()
#define pb emplace_back
#define mp make_pair
#define fab(i, a, b) for (int i = (a); i < (b); i++)
#define fba(i, a, b) for (int i = (b); i >= (a); i--)
#define level 20
int mod = 1000000007;
// int mod=998244353;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};
bool test = 0;

struct query {
  int l, r, i, ans;
};

int n, q, x, y;
void solve() {

  cin >> n >> q;
  int sqrtn = (int)sqrt(n) + 1;
  int a[n + 5];

  for (int i = 0; i < n; i++)
    cin >> a[i];

  query v[q];

  for (int i = 0; i < q; i++) {
    cin >> v[i].l >> v[i].r;
    v[i].i = i;
    v[i].l--;
    v[i].r--;
  }
  sort(v, v + q, [&sqrtn](query &a, query &b) {
    if (a.l / sqrtn != b.l / sqrtn)
      return a.l / sqrtn < b.l / sqrtn;
    return a.r < b.r;
  });

  vector<int> m(n + 1);
  int curl = 1, curr = 0;
  int cnt = 0;
  for (int i = 0; i < q; i++) {

    while (curr < v[i].r) {
      curr++;
      if (m[a[curr]] == 0)
        cnt++;
      m[a[curr]]++;
    }
    while (curl > v[i].l) {
      curl--;
      if (m[a[curl]] == 0)
        cnt++;
      m[a[curl]]++;
    }
    while (curr > v[i].r) {
      if (m[a[curr]] == 1)
        cnt--;
      m[a[curr]]--;
      curr--;
    }
    while (curl < v[i].l) {
      if (m[a[curl]] == 1)
        cnt--;
      m[a[curl]]--;
      curl++;
    }
    v[i].ans = cnt;
  }
  sort(v, v + q, [&](query &a, query &b) { return a.i < b.i; });
  for (int i = 0; i < q; i++)
    cout << v[i].ans << "\n";
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  if (test)
    cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}