Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define endl '\n'
#define snd second
#define fst first
#define fastio cin.tie(NULL), cout.sync_with_stdio(true)

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef pair<ll, int> ii;
typedef pair<ii, int> iii;

const int mod = 1e9 + 7;
const int N = 100005;

struct query {
  int i, l, r, ans;
};

void solve() {
  int n, qq;
  cin >> n >> qq;
  vector<int> a(n), see(n + 1);
  vector<query> q(qq);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < qq; i++) {
    cin >> q[i].l >> q[i].r;
    q[i].i = i;
    q[i].l--, q[i].r--;
  }
  int len = sqrt(n) + 1;
  sort(q.begin(), q.end(), [&len](query &a, query &b) {
    if (a.l != b.l)
      return a.l < b.l;
    return a.r < b.r;
  });
  int l = 0, r = -1, cnt = 0;
  for (auto &q : q) {
    while (l < q.l) {
      see[a[l]]--;
      if (see[a[l]] == 0)
        cnt--;
      l++;
    }
    while (l > q.l) {
      l--;
      if (see[a[l]] == 0)
        cnt++;
      see[a[l]]++;
    }
    while (r < q.r) {
      r++;
      if (see[a[r]] == 0)
        cnt++;
      see[a[r]]++;
    }
    while (r > q.r) {
      see[a[r]]--;
      if (see[a[r]] == 0)
        cnt--;
      r--;
    }
    q.ans = cnt;
  }
  sort(q.begin(), q.end(), [](query &a, query &b) { return a.i < b.i; });
  for (int i = 0; i < qq; i++)
    cout << q[i].ans << '\n';
}

int main() {
  fastio;
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define endl '\n'
#define snd second
#define fst first
#define fastio cin.tie(NULL), cout.sync_with_stdio(true)

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef pair<ll, int> ii;
typedef pair<ii, int> iii;

const int mod = 1e9 + 7;
const int N = 100005;

struct query {
  int i, l, r, ans;
};

void solve() {
  int n, qq;
  cin >> n >> qq;
  vector<int> a(n), see(n + 1);
  vector<query> q(qq);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < qq; i++) {
    cin >> q[i].l >> q[i].r;
    q[i].i = i;
    q[i].l--, q[i].r--;
  }
  int len = sqrt(n) + 1;
  sort(q.begin(), q.end(),
       [&len](query &a, query &b) { // sqrt decomp. trick (mo's)
         if (a.l / len != b.l / len)
           return a.l / len < b.l / len;
         return a.r < b.r;
       });
  int l = 0, r = -1, cnt = 0;
  for (auto &q : q) {
    while (l < q.l) {
      see[a[l]]--;
      if (see[a[l]] == 0)
        cnt--;
      l++;
    }
    while (l > q.l) {
      l--;
      if (see[a[l]] == 0)
        cnt++;
      see[a[l]]++;
    }
    while (r < q.r) {
      r++;
      if (see[a[r]] == 0)
        cnt++;
      see[a[r]]++;
    }
    while (r > q.r) {
      see[a[r]]--;
      if (see[a[r]] == 0)
        cnt--;
      r--;
    }
    q.ans = cnt;
  }
  sort(q.begin(), q.end(), [](query &a, query &b) { return a.i < b.i; });
  for (int i = 0; i < qq; i++)
    cout << q[i].ans << '\n';
}

int main() {
  fastio;
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}