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

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\\n"

using namespace std;
const long long MOD = (long long)1e9 + 7;

int n, q, c[500009], l, r, ans[500009], cnt[500009];
pair<pair<int, int>, int> dat[500009];
int seg, cur;

bool comp(const pair<pair<int, int>, int> &x,
          const pair<pair<int, int>, int> &y) {
  if (x.fi.fi / seg < y.fi.fi / seg)
    return true;
  else if (x.fi.fi / seg > y.fi.fi / seg)
    return false;
  return x.fi.se < y.fi.se;
}

void fun(int a, int b, int x, int y) {
  if (a < x) {
    for (int i = a; i < x; i++) {
      cnt[c[i]]--;
      if (cnt[c[i]] == 0)
        cur--;
    }
  } else {
    for (int i = a - 1; i >= x; i--) {
      if (cnt[c[i]] == 0)
        cur++;
      cnt[c[i]]++;
    }
  }
  if (b < y) {
    for (int i = b + 1; i <= y; i++) {
      if (cnt[c[i]] == 0)
        cur++;
      cnt[c[i]]++;
    }
  } else {
    for (int i = b; i > y; i--) {
      cnt[c[i]]--;
      if (cnt[c[i]] == 0)
        cur--;
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    pair<int, int> tmp = {l, r};
    dat[i] = {tmp, i};
  }
  seg = 1;
  while (seg * seg < n)
    seg++;
  sort(dat, dat + q, comp);
  for (int i = dat[0].fi.fi; i <= dat[0].fi.se; i++) {
    if (cnt[c[i]] == 0)
      cur++;
    cnt[c[i]]++;
  }
  ans[dat[0].se] = cur;
  for (int i = 1; i < q; i++) {
    fun(dat[i - 1].fi.fi, dat[i - 1].fi.se, dat[i].fi.fi, dat[i].fi.se);
    ans[dat[i].se] = cur;
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << E;
  }
  // system("pause");
  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\\n"

using namespace std;
const long long MOD = (long long)1e9 + 7;

int n, q, c[500009], l, r, ans[500009], cnt[500009];
pair<pair<int, int>, int> dat[500009];
int seg, cur;

bool comp(const pair<pair<int, int>, int> &x,
          const pair<pair<int, int>, int> &y) {
  if (x.fi.fi / seg < y.fi.fi / seg)
    return true;
  else if (x.fi.fi / seg > y.fi.fi / seg)
    return false;
  return x.fi.se < y.fi.se;
}

void fun(int a, int b, int x, int y) {
  if (a < x) {
    for (int i = a; i < x; i++) {
      cnt[c[i]]--;
      if (cnt[c[i]] == 0)
        cur--;
    }
  } else {
    for (int i = a - 1; i >= x; i--) {
      if (cnt[c[i]] == 0)
        cur++;
      cnt[c[i]]++;
    }
  }
  if (b < y) {
    for (int i = b + 1; i <= y; i++) {
      if (cnt[c[i]] == 0)
        cur++;
      cnt[c[i]]++;
    }
  } else {
    for (int i = b; i > y; i--) {
      cnt[c[i]]--;
      if (cnt[c[i]] == 0)
        cur--;
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    pair<int, int> tmp = {l, r};
    dat[i] = {tmp, i};
  }
  seg = 1;
  while (seg * seg < n)
    seg++;
  sort(dat, dat + 1, comp);
  for (int i = dat[0].fi.fi; i <= dat[0].fi.se; i++) {
    if (cnt[c[i]] == 0)
      cur++;
    cnt[c[i]]++;
  }
  ans[dat[0].se] = cur;
  for (int i = 1; i < q; i++) {
    fun(dat[i - 1].fi.fi, dat[i - 1].fi.se, dat[i].fi.fi, dat[i].fi.se);
    ans[dat[i].se] = cur;
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << E;
  }
  // system("pause");
  return 0;
}