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

/*
        author:: tamajit banerjee
*/

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define pll pair<ll, ll>
ll c[500001];
ll n, q, cnt;
int vis[500001];
ll l, r;
int B = 720;
pair<int, pair<int, int>> query[500001];
int ans[500001];

void fastscan(ll &number) {
  // variable to indicate sign of input number
  bool negative = false;
  register int c;

  number = 0;

  // extract current character from buffer
  c = getchar();

  // Keep on extracting characters if they are integers
  // i.e ASCII Value lies from '0'(48) to '9' (57)
  for (; (c > 47 && c < 58); c = getchar())
    number = number * 10 + c - 48;

  // if scanned input has a negative sign, negate the
  // value of the input number
  if (negative)
    number *= -1;
}

bool cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
  if (a.first / B == b.first / B) {
    return a.second.first < b.second.first;
  }
  return a.first < b.first;
}

void add(int x) {
  if (vis[x] == 0) {
    ++cnt;
  }
  ++vis[x];
}

void subtract(int x) {
  if (vis[x] == 1) {
    --cnt;
  }
  --vis[x];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  fastscan(n);
  fastscan(q);
  for (int i = 0; i < n; i++) {
    fastscan(c[i]);
    --c[i];
    vis[i] = 0;
  }
  for (int i = 0; i < q; i++) {
    fastscan(l);
    fastscan(r);
    --l, --r;
    query[i] = {l, {r, i}};
  }
  sort(query, query + q, cmp);
  l = 0, r = -1;
  cnt = 0;
  for (int i = 0; i < q; i++) {
    while (r < query[i].second.first) {
      r++;
      add(c[r]);
    }
    while (r > query[i].second.first) {
      subtract(c[r]);
      r--;
    }
    while (l < query[i].first) {
      subtract(c[l]);
      l++;
    }
    while (l > query[i].first) {
      l--;
      add(c[l]);
    }
    ans[query[i].second.second] = cnt;
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

/*
        author:: tamajit banerjee
*/

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define pll pair<ll, ll>
ll c[500001];
ll n, q, cnt;
int vis[500001];
ll l, r;
ll B;
pair<int, pair<int, int>> query[500001];
int ans[500001];

void fastscan(ll &number) {
  // variable to indicate sign of input number
  bool negative = false;
  register int c;

  number = 0;

  // extract current character from buffer
  c = getchar();

  // Keep on extracting characters if they are integers
  // i.e ASCII Value lies from '0'(48) to '9' (57)
  for (; (c > 47 && c < 58); c = getchar())
    number = number * 10 + c - 48;

  // if scanned input has a negative sign, negate the
  // value of the input number
  if (negative)
    number *= -1;
}

bool cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
  if (a.first / B == b.first / B) {
    return a.second.first < b.second.first;
  }
  return a.first < b.first;
}

void add(int x) {
  if (vis[x] == 0) {
    ++cnt;
  }
  ++vis[x];
}

void subtract(int x) {
  if (vis[x] == 1) {
    --cnt;
  }
  --vis[x];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  fastscan(n);
  fastscan(q);
  B = sqrt(n) + 15;
  for (int i = 0; i < n; i++) {
    fastscan(c[i]);
    --c[i];
    vis[i] = 0;
  }
  for (int i = 0; i < q; i++) {
    fastscan(l);
    fastscan(r);
    --l, --r;
    query[i] = {l, {r, i}};
  }
  sort(query, query + q, cmp);
  l = 0, r = -1;
  cnt = 0;
  for (int i = 0; i < q; i++) {
    while (r < query[i].second.first) {
      r++;
      add(c[r]);
    }
    while (r > query[i].second.first) {
      subtract(c[r]);
      r--;
    }
    while (l < query[i].first) {
      subtract(c[l]);
      l++;
    }
    while (l > query[i].first) {
      l--;
      add(c[l]);
    }
    ans[query[i].second.second] = cnt;
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << '\\n';
  }
  return 0;
}