Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define PI acos(-1)
#define pb push_back
#define mp make_pair
#define int long long int
#define pi pair<int, int>
#define pii pair<int, pi>
#define fir first
#define sec second
#define MAXN 500001
#define MAXL 20
#define INF 200001
#define mod 1000000007

int n, q, block;
int m[MAXN];
int v[MAXN];
int ans[MAXN];
vector<pii> queries;

bool cmp(pii x, pii y) {
  if (x.fir / block != y.fir / block)
    return x.fir / block < y.fir / block;
  return x.sec.fir < y.sec.fir;
}
void sqrt_decomposition() {
  block = (int)sqrt(n);
  sort(queries.begin(), queries.end(), cmp);
  int curr_left = 0, curr_right = 0, curr_sum = 0;
  for (int i = 0; i < queries.size(); i++) {
    int l = queries[i].fir, r = queries[i].sec.fir;
    while (curr_left < l) {
      m[v[curr_left]]--;
      if (m[v[curr_left]] == 0)
        curr_sum--;
      curr_left++;
    }
    while (curr_left > l) {
      m[v[curr_left - 1]]++;
      if (m[v[curr_left - 1]] == 1)
        curr_sum++;
      curr_left--;
    }
    while (curr_right <= r) {
      m[v[curr_right]]++;
      if (m[v[curr_right]] == 1)
        curr_sum++;
      curr_right++;
    }
    while (curr_right > r + 1) {
      m[v[curr_right - 1]]--;
      if (m[v[curr_right - 1]] == 0)
        curr_sum--;
      curr_right--;
    }
    ans[queries[i].sec.sec] = curr_sum;
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> v[i];
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    queries.pb(mp(l, mp(r, i)));
  }
  sqrt_decomposition();
  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define PI acos(-1)
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define pii pair<int, pi>
#define fir first
#define sec second
#define MAXN 500001
#define MAXL 20
#define INF 200001
#define mod 1000000007

int n, q, block;
int m[MAXN];
int v[MAXN];
int ans[MAXN];
vector<pii> queries;

bool cmp(pii x, pii y) {
  if (x.fir / block != y.fir / block)
    return x.fir / block < y.fir / block;
  return x.sec.fir < y.sec.fir;
}
void sqrt_decomposition() {
  block = (int)sqrt(n);
  sort(queries.begin(), queries.end(), cmp);
  int curr_left = 0, curr_right = 0, curr_sum = 0;
  for (int i = 0; i < queries.size(); i++) {
    int l = queries[i].fir, r = queries[i].sec.fir;
    while (curr_left < l) {
      m[v[curr_left]]--;
      if (m[v[curr_left]] == 0)
        curr_sum--;
      curr_left++;
    }
    while (curr_left > l) {
      m[v[curr_left - 1]]++;
      if (m[v[curr_left - 1]] == 1)
        curr_sum++;
      curr_left--;
    }
    while (curr_right <= r) {
      m[v[curr_right]]++;
      if (m[v[curr_right]] == 1)
        curr_sum++;
      curr_right++;
    }
    while (curr_right > r + 1) {
      m[v[curr_right - 1]]--;
      if (m[v[curr_right - 1]] == 0)
        curr_sum--;
      curr_right--;
    }
    ans[queries[i].sec.sec] = curr_sum;
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> v[i];
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    queries.pb(mp(l, mp(r, i)));
  }
  sqrt_decomposition();
  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
  return 0;
}