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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR1(i, n) for (int(i) = 1; (i) <= (n); (i)++)
#define FORI(i, n) for (int(i) = n - 1; (i) >= 0; (i)--)
template <class T, class U> void umin(T &x, const U &y) { x = min(x, (T)y); }
template <class T, class U> void umax(T &x, const U &y) { x = max(x, (T)y); }
template <class T, class U> void init(vector<T> &v, U x, size_t n) {
  v = vector<T>(n, x);
}
template <class T, class U>
void init(vector<T> &v, U x, size_t n, size_t m...) {
  v = vector<T>(n);
  for (auto &a : v)
    init(a, x, m);
}

int m;
struct ttt {
  int l, r, i;
  bool operator<(const ttt &o) {
    if (l / m != o.l / m)
      return l / m < o.l / m;
    return r > o.r;
  }
};

// #define TEST 1
int main(int argc, char **argv) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(15);
  if (argc == 2 && atoi(argv[1]) == 123456789)
    freopen("d:\\\\code\\\\cpp\\\\contests\\\\stdin", "r", stdin);

  int n, q;
  cin >> n >> q;

  vector<int> c(n);
#if TEST
  FOR(i, n) c[i] = rand() % 100;
#else
  FOR(i, n) cin >> c[i];
#endif

  vector<ttt> v;
  FOR(i, q) {
    int l, r;
#if TEST
    l = 1 + rand() % n;
    r = l + rand() % n;
    if (r < l)
      swap(l, r);
#else
    cin >> l >> r;
#endif
    l--;
    r--;
    v.push_back({l, r, i});
  }
  m = sqrt(n);
  sort(v.begin(), v.end());

  int cnt = 0;
  vector<int> H(n + 1);
  for (int i = v[0].l; i <= v[0].r; i++) {
    H[c[i]]++;
    cnt += H[c[i]] == 1;
  }

  vector<int> sol(q);
  sol[v[0].i] = cnt;
  int l = v[0].l, r = v[0].r;
  FOR1(i, q - 1) {
    while (r > v[i].r) {
      H[c[r]]--;
      if (H[c[r]] == 0)
        cnt--;
      r--;
    }
    while (r < v[i].r) {
      r++;
      H[c[r]]++;
      if (H[c[r]] == 1)
        cnt++;
    }
    while (l < v[i].l) {
      H[c[l]]--;
      if (H[c[l]] == 0)
        cnt--;
      l++;
    }
    while (l > v[i].l) {
      l--;
      H[c[l]]++;
      if (H[c[l]] == 1)
        cnt++;
    }
    sol[v[i].i] = cnt;
  }
  FOR(i, q)
  cout << sol[i] << '\\n';
  cout << endl;

  if (argc == 2 && atoi(argv[1]) == 123456789)
    cout << clock() * 1.0 / CLOCKS_PER_SEC << " sec\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR1(i, n) for (int(i) = 1; (i) <= (n); (i)++)
#define FORI(i, n) for (int(i) = n - 1; (i) >= 0; (i)--)
template <class T, class U> void umin(T &x, const U &y) { x = min(x, (T)y); }
template <class T, class U> void umax(T &x, const U &y) { x = max(x, (T)y); }
template <class T, class U> void init(vector<T> &v, U x, size_t n) {
  v = vector<T>(n, x);
}
template <class T, class U>
void init(vector<T> &v, U x, size_t n, size_t m...) {
  v = vector<T>(n);
  for (auto &a : v)
    init(a, x, m);
}

int m;
struct ttt {
  int l, r, i;
  bool operator<(const ttt &o) {
    if (l / m != o.l / m)
      return l / m < o.l / m;
    return r > o.r;
  }
};

// #define TEST 1
int main(int argc, char **argv) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(15);
  if (argc == 2 && atoi(argv[1]) == 123456789)
    freopen("d:\\\\code\\\\cpp\\\\contests\\\\stdin", "r", stdin);

  int n, q;
  cin >> n >> q;

  vector<int> c(n);
#if TEST
  FOR(i, n) c[i] = rand() % 100;
#else
  FOR(i, n) cin >> c[i];
#endif

  vector<ttt> v;
  FOR(i, q) {
    int l, r;
#if TEST
    l = 1 + rand() % n;
    r = l + rand() % n;
    if (r < l)
      swap(l, r);
#else
    cin >> l >> r;
#endif
    l--;
    r--;
    v.push_back({l, r, i});
  }
  m = sqrt(n);
  sort(v.begin(), v.end());
  unordered_map<int, int> H;
  for (int i = v[0].l; i <= v[0].r; i++)
    H[c[i]]++;

  vector<int> sol(q);
  sol[v[0].i] = H.size();
  int l = v[0].l, r = v[0].r;
  FOR1(i, q - 1) {
    while (r > v[i].r) {
      H[c[r]]--;
      if (H[c[r]] == 0)
        H.erase(c[r]);
      r--;
    }
    while (r < v[i].r) {
      r++;
      H[c[r]]++;
    }
    while (l < v[i].l) {
      H[c[l]]--;
      if (H[c[l]] == 0)
        H.erase(c[l]);
      l++;
    }
    while (l > v[i].l) {
      l--;
      H[c[l]]++;
    }
    sol[v[i].i] = H.size();
  }
  FOR(i, q)
  cout << sol[i] << '\\n';
  cout << endl;

  if (argc == 2 && atoi(argv[1]) == 123456789)
    cout << clock() * 1.0 / CLOCKS_PER_SEC << " sec\\n";
  return 0;
}