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

#include <bits/stdc++.h>
#define ll long long int
#define scll(x) scanf("%lld", &x)
using namespace std;
ll freq[100000000];
struct boo {
  ll l;
  ll r;
  ll index;
  ll result;
};
ll n;
bool comp(boo A, boo B) {
  ll blocksize = (ll)sqrt(n * 1.0);
  if (A.l / blocksize != B.l / blocksize)
    return A.l / blocksize < B.l / blocksize;
  else
    return A.r < B.r;
}
void query(ll a[], boo st[], ll siz) {
  ll currL = 0, i;
  ll currR = 0;
  ll distinct = 0;
  for (i = 0; i < siz; i++) {
    ll le = st[i].l;
    ll ri = st[i].r;
    while (currL < le) {
      freq[a[currL]]--;
      if (!freq[a[currL]])
        distinct--;
      currL++;
    }
    while (currL > le) {
      freq[a[currL - 1]]++;
      if (freq[a[currL - 1]] == 1)
        distinct++;
      currL--;
    }
    while (currR <= ri) {
      freq[a[currR]]++;
      if (freq[a[currR]] == 1)
        distinct++;
      currR++;
    }
    while (currR > ri + 1) {
      freq[a[currR - 1]]--;
      if (!freq[a[currR - 1]])
        distinct--;
      currR--;
    }
    st[st[i].index].result = distinct;
  }
}
int main() {
  ll i = 0, j, m;
  scll(n);
  scll(m);
  ll a[n];
  for (j = 0; j < n; j++) {
    scll(a[j]);
  }
  ll l1, r1;
  struct boo st[m];
  for (i = 0; i < m; i++) {
    scll(l1);
    scll(r1);
    l1--;
    r1--;
    st[i].l = l1;
    st[i].r = r1;
    st[i].index = i;
  }
  sort(st, st + m, comp);
  query(a, st, m);
  for (i = 0; i < m; i++) {
    printf("%lld\\n", st[i].result);
  }
  // for(j=0;j<i;j++)
  // cout<<st[j].l<<" "<<st[j].r<<"\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define scll(x) scanf("%lld", &x)
using namespace std;
ll freq[100000000];
struct boo {
  ll l;
  ll r;
  ll index;
  ll result;
};
ll n;
bool comp(boo A, boo B) {
  // ll blocksize=(ll)sqrt(n*1.0);
  ll blocksize = 760;
  if (A.l / blocksize != B.l / blocksize)
    return A.l / blocksize < B.l / blocksize;
  else
    return A.r < B.r;
}
bool comp1(boo A, boo B) {
  if (A.index < B.index)
    return true;
  else
    return false;
}
void query(ll a[], boo st[], ll siz) {
  ll currL = 0, i;
  ll currR = 0;
  ll distinct = 0;
  for (i = 0; i < siz; i++) {
    ll le = st[i].l;
    ll ri = st[i].r;
    while (currL < le) {
      freq[a[currL]]--;
      if (!freq[a[currL]])
        distinct--;
      currL++;
    }
    while (currL > le) {
      freq[a[currL - 1]]++;
      if (freq[a[currL - 1]] == 1)
        distinct++;
      currL--;
    }
    while (currR <= ri) {
      freq[a[currR]]++;
      if (freq[a[currR]] == 1)
        distinct++;
      currR++;
    }
    while (currR > ri + 1) {
      freq[a[currR - 1]]--;
      if (!freq[a[currR - 1]])
        distinct--;
      currR--;
    }
    st[i].result = distinct;
  }
}
int main() {
  ll i = 0, j, m;
  scll(n);
  scll(m);
  ll a[n];
  for (j = 0; j < n; j++) {
    scll(a[j]);
  }
  // scll(m);
  ll l1, r1;
  struct boo st[m];
  for (i = 0; i < m; i++) {
    scll(l1);
    scll(r1);
    l1--;
    r1--;
    st[i].l = l1;
    st[i].r = r1;
    st[i].index = i;
  }
  sort(st, st + m, comp);
  query(a, st, m);
  sort(st, st + m, comp1);
  for (i = 0; i < m; i++) {
    printf("%lld\\n", st[i].result);
  }
  // for(j=0;j<i;j++)
  // cout<<st[j].l<<" "<<st[j].r<<"\\n";
  return 0;
}