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

#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
const int inf = numeric_limits<int>::max();
struct __io {
  __io() {
    ios_base::Init i;
    ios_base::sync_with_stdio(0);
    cin.tie(0);
  }
} __io;      // fast I/O
#ifdef LOCAL // setting up print debugging (yes lol)
template <class K, class V>
ostream &operator<<(ostream &s, const pair<K, V> &p) {
  s << '<' << p.x << ", " << p.y << '>';
  return s;
}
template <class T, class = typename T::value_type,
          class = typename enable_if<!is_same<T, string>::value>::type>
ostream &operator<<(ostream &s, const T &v) {
  s << '[';
  for (auto &x : v) {
    s << x << ", ";
  }
  if (!v.empty()) {
    s << "\\b\\b";
  }
  s << ']';
  return s;
}
void __prnt() { cerr << endl; }
template <class T, class... Ts> void __prnt(T &&a, Ts &&...etc) {
  cerr << a << ' ';
  __prnt(etc...);
}
#define print(...) __prnt(__VA_ARGS__)
#else
#define print(...)
#endif

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

signed main() {
  int n, t;
  cin >> n >> t;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<query> q(t);
  for (int i = 0; i < t; ++i) {
    cin >> q[i].l >> q[i].r;
    q[i].i = i;
    q[i].l--;
    q[i].r--;
  }

  int sqrtn = sqrt(n) + 1;
  sort(q.begin(), q.end(), [&sqrtn](query &a, query &b) {
    if (a.l / sqrtn != b.l / sqrtn) {
      return a.l / sqrtn < b.l / sqrtn;
    }
    return a.r < b.r;
  });

  vector<int> m(n + 1);
  int l = 0;
  int r = -1;
  int cnt = 0;

  for (auto &q : q) {
    if (r < q.l || q.r < l) {
      print("!");
      m.assign(n + 1, 0);
      cnt = 0;
      l = q.l;
      r = q.l - 1;
    }
    while (r < q.r) {
      r++;
      if (m[a[r]] == 0)
        cnt++;
      m[a[r]]++;
    }
    while (r > q.r) {
      m[a[r]]--;
      if (m[a[r]] == 0)
        cnt--;
      r--;
    }
    while (l < q.l) {
      m[a[l]]--;
      if (m[a[l]] == 0)
        cnt--;
      l++;
    }
    while (l > q.l) {
      l--;
      if (m[a[l]] == 0)
        cnt++;
      m[a[l]]++;
    }
    q.ans = cnt;
  }
  sort(q.begin(), q.end(), [](query &a, query &b) { return a.i < b.i; });

  for (auto &q : q) {
    cout << q.ans << '\\n';
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fi first
#define se second
#define fast ios_base::sync_with_stdio(false);
using namespace std;

// for debugging
/*
g++ -D_GLIBCXX_ASSERTIONS -DDEBUG -ggdb3 -std=c++14
*/
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x)                                                                 \\
  {                                                                            \\
    ++recur_depth;                                                             \\
    auto x_ = x;                                                               \\
    --recur_depth;                                                             \\
    cerr << string(recur_depth, '\\t') << "\\e[91m" << __func__ << ":"           \\
         << __LINE__ << "\\t" << #x << " = " << x_ << "\\e[39m" << endl;         \\
  }
#else
#define dbg(x)
#endif
template <typename Ostream, typename Cont>
typename enable_if<is_same<Ostream, ostream>::value, Ostream &>::type
operator<<(Ostream &os, const Cont &v) {
  os << "[";
  for (auto &x : v) {
    os << x << ", ";
  }
  return os << "]";
}
template <typename Ostream, typename... Ts>
Ostream &operator<<(Ostream &os, const pair<Ts...> &p) {
  return os << "{" << p.first << ", " << p.second << "}";
}
// debugging ends here

typedef long long int ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

vector<int> a;
int blen;
int cnt;
const int mxn = 5e5 + 10;
int freq[mxn];

struct query {
  int l, r, idx;
};

bool cmp(query a, query b) {
  return (a.l / blen < b.l / blen) ||
         ((a.l / blen == b.l / blen) && (a.r < b.r));
}

void add(int i) {
  freq[a[i]]++;
  if (freq[a[i]] == 1)
    cnt++;
}

void remove(int i) {
  freq[a[i]]--;
  if (freq[a[i]] == 0)
    cnt--;
}

void solve() {
  int n;
  cin >> n;
  a = vector<int>(n);
  blen = sqrt(n) + 1;
  int q;
  cin >> q;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  vector<query> qs(q);

  for (int i = 0; i < q; i++) {
    cin >> qs[i].l >> qs[i].r;
    qs[i].l--, qs[i].r--;
    qs[i].idx = i;
  }
  int ans[q];
  sort(qs.begin(), qs.end(), cmp);
  int ml = 0, mr = -1;
  for (int i = 0; i < q; i++) {
    int l = qs[i].l;
    int r = qs[i].r;
    // adding the elements for answering the queries
    while (ml > l)
      add(--ml);
    while (mr < r)
      add(++mr);

    // removing the elements
    while (ml < l)
      remove(ml++);
    while (mr > r)
      remove(mr--);

    ans[qs[i].idx] = cnt;
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}

int main() {
  fast;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  // int t;cin >> t;while(t--)
  solve();
}