Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
/*** 𝕒𝕜𝕤𝕙𝕒𝕪𝕜𝕦𝕞𝕒𝕣𝟡𝟡 ***/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using id_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define f(i, x, n) for (i = x; i < n; i++)
#define fr(i, x, n) for (i = x; i >= n; i--)
#define pu push_back
#define pp pop_back
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define SP(x) fixed << setprecision(x)
#define IOS()                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

const long long MOD = 1e9 + 7;
const long double PI = acos(-1);
const long long INF = INT_MAX;
const long long LINF = LLONG_MAX;
const long long N = 5e5 + 1;

typedef long long ll;
typedef long double ld;

ll blocksize = 10000, n, m, ans = 0;
ll a[N];
ll cont[N];

void remove(ll ind) {
  if (cont[a[ind]] == 1)
    ans--;
  cont[a[ind]]--;
}
void add(ll ind) {
  cont[a[ind]]++;
  if (cont[a[ind]] == 1)
    ans++;
}
ll getanswer() { return ans; }
struct Query {
  ll l, r, ind;
  bool operator<(Query other) const {
    return make_pair(l / blocksize, r) <
           make_pair(other.l / blocksize, other.r);
  }
};

vector<ll> mosalgo(vector<Query> queries) {
  vector<ll> answer(queries.size());
  sort(all(queries));
  ll curl = 0, curr = -1;
  for (Query q : queries) {
    while (curl > q.l) {
      curl--;
      add(curl);
    }
    while (curr < q.r) {
      curr++;
      add(curr);
    }
    while (curl < q.l) {
      remove(curl);
      curl++;
    }
    while (curr > q.r) {
      remove(curr);
      curr--;
    }
    answer[q.ind] = getanswer();
  }
  return answer;
}

void test_case() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<Query> v;
  for (int i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    v.pu({x, y, i});
  }
  vector<ll> res = mosalgo(v);
  for (int i = 0; i < m; i++)
    cout << res[i] << '\n';
}

int main() {
  IOS();

  int tc = 1;
  // cin>>tc;
  while (tc--)
    test_case();
  return 0;
}

Fixed submission:
/*** 𝕒𝕜𝕤𝕙𝕒𝕪𝕜𝕦𝕞𝕒𝕣𝟡𝟡 ***/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using id_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define f(i, x, n) for (i = x; i < n; i++)
#define fr(i, x, n) for (i = x; i >= n; i--)
#define pu push_back
#define pp pop_back
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define SP(x) fixed << setprecision(x)
#define IOS()                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

const long long MOD = 1e9 + 7;
const long double PI = acos(-1);
const long long INF = INT_MAX;
const long long LINF = LLONG_MAX;
const long long N = 5e5 + 1;

typedef long long ll;
typedef long double ld;

ll blocksize = 2000, n, m, ans = 0;
ll a[N];
ll cont[N];

void remove(ll ind) {
  if (cont[a[ind]] == 1)
    ans--;
  cont[a[ind]]--;
}
void add(ll ind) {
  cont[a[ind]]++;
  if (cont[a[ind]] == 1)
    ans++;
}
ll getanswer() { return ans; }
struct Query {
  ll l, r, ind;
  bool operator<(Query other) const {
    return make_pair(l / blocksize, r) <
           make_pair(other.l / blocksize, other.r);
  }
};

vector<ll> mosalgo(vector<Query> queries) {
  vector<ll> answer(queries.size());
  sort(all(queries));
  ll curl = 0, curr = -1;
  for (Query q : queries) {
    while (curl > q.l) {
      curl--;
      add(curl);
    }
    while (curr < q.r) {
      curr++;
      add(curr);
    }
    while (curl < q.l) {
      remove(curl);
      curl++;
    }
    while (curr > q.r) {
      remove(curr);
      curr--;
    }
    answer[q.ind] = getanswer();
  }
  return answer;
}

void test_case() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<Query> v;
  for (int i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    v.pu({x, y, i});
  }
  vector<ll> res = mosalgo(v);
  for (int i = 0; i < m; i++)
    cout << res[i] << '\n';
}

int main() {
  IOS();

  int tc = 1;
  // cin>>tc;
  while (tc--)
    test_case();
  return 0;
}