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

#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
typedef unsigned long long int uLL;

inline unsigned int _Int() {
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  unsigned x = (c - '0');
  while ((c = getchar()) >= '0' && c <= '9')
    x = ((x << 3) + (x << 1)) + c - '0';
  return x;
}
/**
MO's Algorithm
Given an array of N integers and Q intervals
In each interval how many distinct values are there
**/
const int M = 5 * 1e5 + 7; /// maximum array size

int n, qryC;
int A[M];
int blockSize;

struct interval {
  int index;
  int u, v;
  void set(int a, int b, int c) { index = a, u = b, v = c; }
  bool operator<(const interval &R) const {
    int b1 = u / blockSize, b2 = R.u / blockSize;
    if (b1 != b2)
      return (b1 < b2);
    return v < R.v;
  }
} q[M];

int fr[1000000 + 7];

int distinctValues;
void element(int x, int f) /// f{1,-1} = add or remove flag
{
  fr[x] = fr[x] + f;
  if (f == +1 && fr[x] == 1)
    distinctValues++;
  if (f == -1 && fr[x] == 0)
    distinctValues--;
}
int Ans[M];

void Main() {
  n = _Int();
  blockSize = sqrt(n + 1);
  qryC = _Int();
  for (int i = 1; i <= n; i++)
    A[i] = _Int();
  for (int i = 0, u, v; i < qryC; i++) {
    u = _Int();
    v = _Int();
    q[i].set(i, u, v);
  }
  sort(q, q + qryC);
  int left = 0, right = 0;
  distinctValues = 0;
  for (int x = 0; x < qryC; x++) {
    while (right < q[x].v)
      element(A[++right], 1);
    while (right > q[x].v)
      element(A[right--], -1);
    while (left < q[x].u)
      element(A[left++], -1);
    while (left > q[x].u)
      element(A[--left], 1);
    Ans[q[x].index] = distinctValues;
  }
  for (int i = 0; i < qryC; i++) {
    printf("%d", Ans[i]);
    putchar(10);
  }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  Main();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double db;
#define endl '\\n'
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
const int siz = 5e5 + 7;
vector<int> v;
int answer;
vector<int> ans;
map<int, int> m;
int n;
struct node {
  int l, r, idx;
};
bool comp(node a, node b) {
  int block_size = sqrt(n) + 7;
  if (a.l / block_size != b.l / block_size)
    return (b.l / block_size > a.l / block_size);
  return (b.r > a.r);
}
void add(int index) {
  m[v[index]]++;
  if (m[v[index]] == 1)
    answer++;
}
void remove(int index) {
  m[v[index]]--;
  if (m[v[index]] == 0)
    answer--;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int query;
  scanf("%d", &n);
  scanf("%d", &query);
  v.resize(n + 1);
  ans.resize(query + 1);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &v[i]);
  }
  vector<node> q(query + 1);
  for (int i = 1; i <= query; i++) {
    scanf("%d%d", &q[i].l, &q[i].r);
    q[i].idx = i;
  }
  sort(q.begin() + 1, q.end(), comp);
  int mo_left = 0, mo_right = 0;
  for (int i = 1; i <= query; i++) {
    int u = q[i].l;
    int r = q[i].r;
    while (u > mo_left) {
      remove(mo_left);
      mo_left++;
    }
    while (u < mo_left) {
      mo_left--;
      add(mo_left);
    }
    while (r > mo_right) {
      mo_right++;
      add(mo_right);
    }
    while (mo_right > r) {
      remove(mo_right);
      mo_right--;
    }
    ans[q[i].idx] = answer;
  }
  for (int i = 1; i <= query; i++) {
    printf("%d\\n", ans[i]);
  }
}