Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define mod 1000000007
#define fr first
#define val second
#define N 100000000
#define fo(i, m, n) for (int i = m; i < n; i++)
#define M 7
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
using namespace std;
using ll = long long;
using ul = unsigned long long;
using pii = pair<int, int>;
using vvi = vector<vector<int>>;
using vi = vector<int>;
using pll = pair<ll, ll>;
using vsi = vector<set<int>>;
using vll = vector<ll>;

ll power(ll a, ll n) {
  ll j = 0;
  ll x = 1;
  while (1ll << j <= n) {
    if (1ll << j & n)
      x = (x * a) % mod;

    a = a * a % mod;
    j++;
  }

  return x;
}

int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

int lb(vi &a, int x) {
  int n = a.size();
  int l = 0;
  int r = n - 1;
  int ind = -1;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (x >= a[mid]) {
      l = mid + 1;
      ind = mid;
    } else
      r = mid - 1;
  }

  return ind + 1;
}

int up(vll &a, ll x) {
  int n = a.size();
  int l = 0;
  int r = n - 1;
  int ind = -1;

  while (l <= r) {
    int mid = (l + r) / 2;
    if (a[mid] > x) {
      r = mid - 1;
      ind = mid;
    } else
      l = mid + 1;
  }

  return ind;
}

ll max(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}

ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
template <typename T> void pra(T a[], int n) {
  fo(i, 0, n) cout << a[i] << ' ';
  cout << endl;
}

int b;
bool csf(pair<pii, int> &x, pair<pii, int> &y) {
  if (x.first.first / b == y.first.first / b)
    return x.first.second < y.first.second;

  return x.first.first / b < y.first.first / b;
}
vi c;

void ext(int v[], int &cnt, int l, int r, int cl, int cr) {
  for (int i = cl - 1; i >= l; i--) {
    v[c[i]]++;
    if (v[c[i]] == 1)
      cnt++;
  }

  for (int i = cr + 1; i <= r; i++) {
    // cout<<i<<' ';
    v[c[i]]++;
    if (v[c[i]] == 1)
      cnt++;
  }

  for (int i = cl; i < l; i++) {

    v[c[i]]--;
    if (v[c[i]] == 0)
      cnt--;
  }
}

int main() {

  fast_io int n, q;
  cin >> n >> q;
  c = vi(n + 1);
  fo(i, 1, n + 1) cin >> c[i];

  b = sqrt(q) * 100;

  vector<pair<pii, int>> queries;

  fo(i, 0, q) {
    int l, r;
    cin >> l >> r;
    queries.push_back({{l, r}, i});
  }

  sort(queries.begin(), queries.end(), csf);
  int v[n + 1];
  int cnt = 0;
  int cl = 0;
  int cr = 0;
  fill(v, v + n + 1, 0);
  vi ans(q);
  fo(i, 0, q) {
    int l = queries[i].first.first;
    int r = queries[i].first.second;
    int ind = queries[i].second;
    if (r < cr) {
      fill(v, v + n + 1, 0);
      cl = l;
      cr = l - 1;
      cnt = 0;
    }

    ext(v, cnt, l, r, cl, cr);
    cl = l;
    cr = r;
    // cout<<cnt<<' '<<ind<<endl;
    ans[ind] = cnt;
  }

  fo(i, 0, q) cout << ans[i] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define mod 1000000007
#define fr first
#define val second
#define N 100000000
#define fo(i, m, n) for (int i = m; i < n; i++)
#define M 7
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
using namespace std;
using ll = long long;
using ul = unsigned long long;
using pii = pair<int, int>;
using vvi = vector<vector<int>>;
using vi = vector<int>;
using pll = pair<ll, ll>;
using vsi = vector<set<int>>;
using vll = vector<ll>;

ll power(ll a, ll n) {
  ll j = 0;
  ll x = 1;
  while (1ll << j <= n) {
    if (1ll << j & n)
      x = (x * a) % mod;

    a = a * a % mod;
    j++;
  }

  return x;
}

int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

int lb(vi &a, int x) {
  int n = a.size();
  int l = 0;
  int r = n - 1;
  int ind = -1;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (x >= a[mid]) {
      l = mid + 1;
      ind = mid;
    } else
      r = mid - 1;
  }

  return ind + 1;
}

int up(vll &a, ll x) {
  int n = a.size();
  int l = 0;
  int r = n - 1;
  int ind = -1;

  while (l <= r) {
    int mid = (l + r) / 2;
    if (a[mid] > x) {
      r = mid - 1;
      ind = mid;
    } else
      l = mid + 1;
  }

  return ind;
}

ll max(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}

ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
template <typename T> void pra(T a[], int n) {
  fo(i, 0, n) cout << a[i] << ' ';
  cout << endl;
}

int b;
bool csf(pair<pii, int> &x, pair<pii, int> &y) {
  if (x.first.first / b == y.first.first / b)
    return x.first.second < y.first.second;

  return x.first.first / b < y.first.first / b;
}
vi c;

void ext(int v[], int &cnt, int l, int r, int cl, int cr) {
  for (int i = cl - 1; i >= l; i--) {
    v[c[i]]++;
    if (v[c[i]] == 1)
      cnt++;
  }

  for (int i = cr + 1; i <= r; i++) {
    // cout<<i<<' ';
    v[c[i]]++;
    if (v[c[i]] == 1)
      cnt++;
  }

  for (int i = cl; i < l; i++) {

    v[c[i]]--;
    if (v[c[i]] == 0)
      cnt--;
  }
}

int main() {

  fast_io int n, q;
  cin >> n >> q;
  c = vi(n + 1);
  fo(i, 1, n + 1) cin >> c[i];

  b = sqrt(q) + 500;

  vector<pair<pii, int>> queries;

  fo(i, 0, q) {
    int l, r;
    cin >> l >> r;
    queries.push_back({{l, r}, i});
  }

  sort(queries.begin(), queries.end(), csf);
  int v[n + 1];
  int cnt = 0;
  int cl = 0;
  int cr = 0;
  fill(v, v + n + 1, 0);
  vi ans(q);
  fo(i, 0, q) {
    int l = queries[i].first.first;
    int r = queries[i].first.second;
    int ind = queries[i].second;
    if (r < cr) {
      fill(v, v + n + 1, 0);
      cl = l;
      cr = l - 1;
      cnt = 0;
    }

    ext(v, cnt, l, r, cl, cr);
    cl = l;
    cr = r;
    // cout<<cnt<<' '<<ind<<endl;
    ans[ind] = cnt;
  }

  fo(i, 0, q) cout << ans[i] << endl;
}