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

/*
    Author : manu_sy
*/
#include "bits/stdc++.h"
using namespace std;

/** Template Begins **/

typedef long long ll;
typedef pair<int, int> PII;

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define endl '\\n'
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define LP(i, n) for (int i = 0; i < n; i++)

/* Debug */

template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.F << "," << P.S << ")";
}

template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[";
  for (auto vv : V)
    os << vv << ",";
  return os << "]";
}

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << '=' << h << endl;
}

template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << '=' << h << ',';
  _dbg(sdbg + 1, a...);
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr                                                                   \\
  if (0)                                                                       \\
  cout
#endif

/* Debug Ends */

const int N = 5e5 + 7;
const int rn = 750;

/** Template Ends **/
int n, q;
int c[N];
int cnt[N];
int dis;
int ans[N];
int l, r;

struct query {
  int l, r, idx;
  bool operator<(const query &oth) const {
    int s1 = l / rn, s2 = oth.l / rn;
    if (s1 == s2)
      return r < oth.r;
    return s1 < s2;
  }
};
int solve(int L, int R) {
  while (l < L) {
    cnt[c[l]]--;
    if (cnt[c[l]] == 0)
      dis--;
    l++;
  }
  while (l > L) {
    l--;
    cnt[c[l]]++;
    if (cnt[c[l]] == 1)
      dis++;
  }
  while (r < R) {
    r++;
    cnt[c[r]]++;
    if (cnt[c[r]] == 1)
      dis++;
  }
  while (r > R) {
    cnt[c[r]]--;
    if (cnt[c[r]] == 0)
      dis--;
    r--;
  }
  return dis;
}
signed main() {

  IOS;
  cin >> n >> q;
  FOR(i, 1, n) cin >> c[i];
  vector<query> queries(q);
  LP(i, q) {
    cin >> queries[i].l >> queries[i].r;
    queries[i].idx = i;
  }
  sort(all(queries));
  l = 0, r = -1;
  for (auto query : queries) {
    ans[query.idx] = solve(query.l, query.r);
  }
  LP(i, q) cout << ans[i] << endl;

  return 0;
}

Error: TLE

Fixed submission:

/*
    Author : manu_sy
*/
#include "bits/stdc++.h"
using namespace std;

/** Template Begins **/

typedef long long ll;
typedef pair<int, int> PII;

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define endl '\\n'
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define LP(i, n) for (int i = 0; i < n; i++)

/* Debug */

template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.F << "," << P.S << ")";
}

template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[";
  for (auto vv : V)
    os << vv << ",";
  return os << "]";
}

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << '=' << h << endl;
}

template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << '=' << h << ',';
  _dbg(sdbg + 1, a...);
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr                                                                   \\
  if (0)                                                                       \\
  cout
#endif

/* Debug Ends */

const int N = 5e5 + 7;

/** Template Ends **/
int n, rn, q;
int c[N];
int cnt[N];
int dis;
int ans[N];
int l, r;

struct query {
  int l, r, idx;
  bool operator<(const query &oth) const {
    int s1 = l / rn, s2 = oth.l / rn;
    if (s1 == s2)
      return r < oth.r;
    return s1 < s2;
  }
};
int solve(int L, int R) {
  while (l < L) {
    cnt[c[l]]--;
    if (cnt[c[l]] == 0)
      dis--;
    l++;
  }
  while (l > L) {
    l--;
    cnt[c[l]]++;
    if (cnt[c[l]] == 1)
      dis++;
  }
  while (r < R) {
    r++;
    cnt[c[r]]++;
    if (cnt[c[r]] == 1)
      dis++;
  }
  while (r > R) {
    cnt[c[r]]--;
    if (cnt[c[r]] == 0)
      dis--;
    r--;
  }
  return dis;
}
signed main() {

  IOS;
  cin >> n >> q;
  rn = sqrt(n);
  FOR(i, 1, n) cin >> c[i];
  vector<query> queries(q);
  LP(i, q) {
    cin >> queries[i].l >> queries[i].r;
    queries[i].idx = i;
  }
  sort(all(queries));
  l = 0, r = -1;
  for (auto query : queries) {
    ans[query.idx] = solve(query.l, query.r);
  }
  LP(i, q) cout << ans[i] << endl;

  return 0;
}