Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
// Author : Prakhar Asaiya
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = a; i < b; ++i)
#define REPN(i, a, b) for (int i = a; i <= b; ++i)
#define REPV(i, a, b) for (int i = a; i >= b; --i)
#define db1(x) cout << #x << "=" << x << '\n'
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << '\n'
#define db3(x, y, z)                                                           \
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \
       << '\n'
#define ll long long
#define INF 100000
#define ff first
#define ss second
const int M = 1e9 + 7;
const int N = 500005;
struct query {
  int l, r, i;
};
const int BLK = 700;
query Q[N];
int A[N], ans[N], freq[N];
int cnt = 0;

bool cmp(query a, query b) {
  if (a.l / BLK != b.l / BLK)
    return a.l / BLK < b.l / BLK;

  return a.r < b.r;
}

void add(int pos) {
  ++freq[A[pos]];
  if (freq[A[pos]] == 1)
    ++cnt;
}

void remove(int pos) {
  --freq[A[pos]];
  if (freq[A[pos]] == 0)
    --cnt;
}

void solve() {
  int n, q;
  cin >> n >> q;
  REP(i, 0, n)
  cin >> A[i];
  REP(i, 0, q) {
    cin >> Q[i].l >> Q[i].r;
    --Q[i].l, --Q[i].r;
    Q[i].i = i;
  }
  sort(Q, Q + q, cmp);
  int ml = 0, mr = -1;
  REP(i, 0, q) {
    int l = Q[i].l;
    int r = Q[i].r;

    while (ml > l) {
      --ml;
      add(ml);
    }

    while (mr < r) {
      ++mr;
      add(mr);
    }

    while (ml < l) {
      remove(ml);
      ++ml;
    }

    while (mr > r) {
      remove(mr);
      --mr;
    }

    ans[Q[i].i] = cnt;
  }
  REP(i, 0, q) { cout << ans[i] << "\n"; }
}

int main() { /*
            #ifndef ONLINE_JUDGE
                freopen("input.txt","r",stdin);
                freopen("output.txt","w",stdout);
            #*/
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  solve();

  return 0;
}


Fixed submission:
// Author : Prakhar Asaiya
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = a; i < b; ++i)
#define REPN(i, a, b) for (int i = a; i <= b; ++i)
#define REPV(i, a, b) for (int i = a; i >= b; --i)
#define db1(x) cout << #x << "=" << x << '\n'
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << '\n'
#define db3(x, y, z)                                                           \
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \
       << '\n'
#define ll long long
#define INF 100000
#define ff first
#define ss second
const int M = 1e9 + 7;
const int N = 500005;
struct query {
  int l, r, i;
};
const int BLK = 800;
query Q[N];
int A[N], ans[N], freq[N];
int cnt = 0;

bool cmp(query a, query b) {
  if (a.l / BLK != b.l / BLK)
    return a.l / BLK < b.l / BLK;

  return a.r < b.r;
}

void add(int pos) {
  ++freq[A[pos]];
  if (freq[A[pos]] == 1)
    ++cnt;
}

void remove(int pos) {
  --freq[A[pos]];
  if (freq[A[pos]] == 0)
    --cnt;
}

void solve() {
  int n, q;
  cin >> n >> q;
  REP(i, 0, n)
  cin >> A[i];
  REP(i, 0, q) {
    cin >> Q[i].l >> Q[i].r;
    --Q[i].l, --Q[i].r;
    Q[i].i = i;
  }
  sort(Q, Q + q, cmp);
  int ml = 0, mr = -1;
  REP(i, 0, q) {
    int l = Q[i].l;
    int r = Q[i].r;

    while (ml > l) {
      --ml;
      add(ml);
    }

    while (mr < r) {
      ++mr;
      add(mr);
    }

    while (ml < l) {
      remove(ml);
      ++ml;
    }

    while (mr > r) {
      remove(mr);
      --mr;
    }

    ans[Q[i].i] = cnt;
  }
  REP(i, 0, q) { cout << ans[i] << "\n"; }
}

int main() { /*
            #ifndef ONLINE_JUDGE
                freopen("input.txt","r",stdin);
                freopen("output.txt","w",stdout);
            #*/
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  solve();

  return 0;
}