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

//
#include <bits/stdc++.h>

using namespace std;

#define PB push_back
#define f first
#define s second
#define what_is(x) cerr << #x << " is " << x << endl;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

const int sqrt_n = 700;
int arr[500005], c[500005];
int ans[500005];
int curr = 0;
void add(int ind) {
  if (arr[c[ind]] == 0) {
    curr++;
  }
  arr[c[ind]]++;
}

void rem(int ind) {
  arr[c[ind]]--;
  if (arr[c[ind]] == 0) {
    curr--;
  }
}

bool comp(pair<pii, int> a, pair<pii, int> b) {
  if (a.f.f / sqrt_n != b.f.f / sqrt_n) {
    return a.f < b.f;
  }
  return (a.f.f / sqrt_n & 1) ? (a.f.s < b.f.s) : (a.f.s > b.f.s);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> c[i];
    c[i]--;
  }
  vector<pair<pii, int>> queries;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    queries.PB(make_pair(make_pair(l, r), i));
  }
  sort(queries.begin(), queries.end(), comp);
  int curr_left = 0, curr_right = -1;
  for (pair<pii, int> p : queries) {
    // cout << p.f.f << " " << p.f.s  << "\\n";
    while (curr_left > p.f.f) {
      curr_left--;
      add(curr_left);
    }
    while (curr_right < p.f.s) {
      curr_right++;
      add(curr_right);
    }
    while (curr_left < p.f.f) {
      rem(curr_left);
      curr_left++;
    }
    while (curr_right > p.f.s) {
      rem(curr_right);
      curr_right--;
    }
    ans[p.s] = curr;
  }

  for (int i = 0; i < q; i++) {
    cout << ans[i] << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

//
#include <bits/stdc++.h>

using namespace std;

#define PB push_back
#define f first
#define s second
#define what_is(x) cerr << #x << " is " << x << endl;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

const int sqrt_n = 700;
set<int> s;
int arr[500005], c[500005];
int ans[500005];

void add(int ind) {
  if (arr[c[ind]] == 0) {
    s.insert(c[ind]);
  }
  arr[c[ind]]++;
}

void rem(int ind) {
  arr[c[ind]]--;
  if (arr[c[ind]] == 0) {
    s.erase(c[ind]);
  }
}

bool comp(pair<pii, int> a, pair<pii, int> b) {
  if (a.f.f / sqrt_n != b.f.f / sqrt_n) {
    return a.f < b.f;
  }
  return (a.f.f / sqrt_n & 1) ? (a.f.s < b.f.s) : (a.f.s > b.f.s);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> c[i];
    c[i]--;
  }
  vector<pair<pii, int>> queries;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    queries.PB(make_pair(make_pair(l, r), i));
  }
  sort(queries.begin(), queries.end(), comp);
  int curr_left = 0, curr_right = -1;
  for (pair<pii, int> p : queries) {
    // cout << p.f.f << " " << p.f.s  << "\\n";
    while (curr_left > p.f.f) {
      curr_left--;
      add(curr_left);
    }
    while (curr_right < p.f.s) {
      curr_right++;
      add(curr_right);
    }
    while (curr_left < p.f.f) {
      rem(curr_left);
      curr_left++;
    }
    while (curr_right > p.f.s) {
      rem(curr_right);
      curr_right--;
    }
    ans[p.s] = s.size();
  }

  for (int i = 0; i < q; i++) {
    cout << ans[i] << "\\n";
  }
  return 0;
}