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

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

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<string> vst;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define all(ct) ct.begin(), ct.end()
#define endl "\\n"
#define fr(i, a) for (auto i : a)
#define f(i, a, b) for (int i = a; i < b; ++i)
#define fd(i, a, b) for (int i = a; i >= b; --i)
#define pb push_back
#define in(d, v) d.find(v) != d.end()
#define mp make_pair
#define size(a) int(a.size())

const ll mod = 1e9 + 7;
const ll inf = ll(1e18);
const double PI = acos(-1);

ll modexp(ll x, ll n, int md) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return modexp((x * x) % md, n / 2, md);
  return (x * modexp((x * x) % md, n / 2, md)) % md;
}

const int MAX = 1000001;
struct Query {
  int l, r, idx;
};

bool cmp(Query x, Query y) { return x.r < y.r; }

void update(int idx, int val, int bit[], int n) {
  for (; idx <= n; idx += idx & -idx)
    bit[idx] += val;
}

int query(int idx, int bit[], int n) {
  int sum = 0;
  for (; idx > 0; idx -= idx & -idx)
    sum += bit[idx];
  return sum;
}

void answeringQueries(int arr[], int n, Query queries[], int q) {
  // initialising bit array
  int bit[n + 1];
  memset(bit, 0, sizeof(bit));

  // holds the rightmost index of any number
  // as numbers of a[i] are less than or equal to 10^6
  int last_visit[MAX];
  memset(last_visit, -1, sizeof(last_visit));

  // answer for each query
  int ans[q];
  int query_counter = 0;
  for (int i = 0; i < n; i++) {
    // If last visit is not -1 update -1 at the
    // idx equal to last_visit[arr[i]]
    if (last_visit[arr[i]] != -1)
      update(last_visit[arr[i]] + 1, -1, bit, n);

    // Setting last_visit[arr[i]] as i and updating
    // the bit array accordingly
    last_visit[arr[i]] = i;
    update(i + 1, 1, bit, n);

    // If i is equal to r of any query  store answer
    // for that query in ans[]
    while (query_counter < q && queries[query_counter].r == i) {
      ans[queries[query_counter].idx] =
          query(queries[query_counter].r + 1, bit, n) -
          query(queries[query_counter].l, bit, n);
      query_counter++;
    }
  }

  // print answer for each query
  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
}
int main() {
  fastio;
  // clock_t tm = clock();
  int n, m;
  cin >> n >> m;
  int a[n];
  f(i, 0, n) cin >> a[i];
  Query q[m];
  f(i, 0, m) {
    cin >> q[i].l >> q[i].r;
    --q[i].l;
    --q[i].r;
    q[i].idx = i;
  }
  sort(q, q + m, cmp);
  answeringQueries(a, n, q, m);
  // cout<<((clock()-tm)*1.0)/CLOCKS_PER_SEC<<endl;
}

Error: TLE

Fixed submission:

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

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<string> vst;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define all(ct) ct.begin(), ct.end()
#define endl "\\n"
#define fr(i, a) for (auto i : a)
#define f(i, a, b) for (int i = a; i < b; ++i)
#define fd(i, a, b) for (int i = a; i >= b; --i)
#define pb push_back
#define in(d, v) d.find(v) != d.end()
#define mp make_pair
#define size(a) int(a.size())

const ll mod = 1e9 + 7;
const ll inf = ll(1e18);
const double PI = acos(-1);

ll modexp(ll x, ll n, int md) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return modexp((x * x) % md, n / 2, md);
  return (x * modexp((x * x) % md, n / 2, md)) % md;
}
vector<pair<pii, int>> q[750];
int main() {
  fastio;
  // clock_t tm = clock();
  int n, m;
  cin >> n >> m;
  vi a(n + 1);
  f(i, 1, n + 1) cin >> a[i];
  vii qr(m + 1);
  vi ans(m + 1, 0);
  f(i, 1, m + 1) cin >> qr[i].first >> qr[i].second;

  int block = m / sqrt(m);
  f(i, 1, m + 1) { q[qr[i].first / block].pb({qr[i], i}); }
  int currl = -1, currt = -1;
  vi fq(n + 1, 0);
  int curr = 0;
  f(z, 0, 750) {
    if (q[z].empty())
      continue;
    sort(all(q[z]));
    fr(e, q[z]) {
      int l = e.first.first, r = e.first.second, i = e.second;
      while (currl < l - 1) {
        ++currl;
        fq[a[currl]]--;
        if (fq[a[currl]] == 0)
          curr--;
      }
      while (currl > l - 1) {
        fq[a[currl]]++;
        if (fq[a[currl]] == 1)
          curr++;
        --currl;
      }
      while (currt < r) {
        currt++;
        fq[a[currt]]++;
        if (fq[a[currt]] == 1)
          curr++;
      }
      while (currt > r) {
        fq[a[currt]]--;
        if (fq[a[currt]] == 0)
          curr--;
        currt--;
      }
      ans[i] = curr;
    }
  }
  f(i, 1, m + 1) cout << ans[i] << endl;
  // cout<<((clock()-tm)*1.0)/CLOCKS_PER_SEC<<endl;
}