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

//@formatter:off
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define rep2(i, s, n) for (int i = int(s); i < int(n); ++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vvd vector<vector<double>>
#define vs vector<string>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vp vector<P>
#define vvp vector<vector<P>>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.first >> p.second;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << '{' << p.first << "," << p.second << '}';
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v) {
    is >> t;
  }
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '[';
  rep(i, v.size()) os << v[i] << (i == int(v.size() - 1) ? ']' : ',');
  return os;
}
void Yes(bool b) { cout << (b ? "Yes" : "No") << '\\n'; }
void YES(bool b) { cout << (b ? "YES" : "NO") << '\\n'; }
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int inf = 1001001001;
const ll linf = 1001001001001001001;
//@formatter:on

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, q;
  cin >> n >> q;
  vi c(n);
  cin >> c;
  rep(i, n) c[i]--;
  vi l(q), r(q);
  rep(i, q) cin >> l[i] >> r[i];
  rep(i, q) {
    l[i]--;
    r[i]--;
  }
  int d = sqrt(n) + 1;
  auto cmp = [&](int a, int b) {
    if (l[a] / d < l[b] / d)
      return true;
    return l[a] / d == l[b] / d and r[a] < r[b];
  };
  vi ord(q);
  iota(all(ord), 0);
  sort(all(ord), cmp);
  int nl = l[ord[0]], nr = r[ord[0]];
  vi ans(q);
  int sum = 0;
  vi v(n);
  auto erase = [&](int i) {
    if (--v[i] == 0) {
      sum--;
    }
  };
  auto add = [&](int i) {
    if (v[i]++ == 0)
      sum++;
  };
  for (int i = nl; i <= nr; i++)
    add(c[i]);
  ans[ord[0]] = sum;
  rep2(i, 1, q) {
    if (nl <= l[ord[i]]) {
      while (nl < l[ord[i]]) {
        erase(c[nl++]);
      }
    } else {
      while (nl > l[ord[i]]) {
        add(c[--nl]);
      }
    }
    if (nr >= r[ord[i]]) {
      while (nr > r[ord[i]]) {
        erase(c[nr--]);
      }
    } else {
      while (nr < r[ord[i]]) {
        add(c[++nr]);
      }
    }
    ans[ord[i]] = sum;
  }
  rep(i, q) cout << ans[i] << '\\n';
}


Error: TLE

Fixed submission:

//@formatter:off
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define rep2(i, s, n) for (int i = int(s); i < int(n); ++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vvd vector<vector<double>>
#define vs vector<string>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vp vector<P>
#define vvp vector<vector<P>>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.first >> p.second;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << '{' << p.first << "," << p.second << '}';
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v) {
    is >> t;
  }
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '[';
  rep(i, v.size()) os << v[i] << (i == int(v.size() - 1) ? ']' : ',');
  return os;
}
void Yes(bool b) { cout << (b ? "Yes" : "No") << '\\n'; }
void YES(bool b) { cout << (b ? "YES" : "NO") << '\\n'; }
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int inf = 1001001001;
const ll linf = 1001001001001001001;
//@formatter:on

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, q;
  cin >> n >> q;
  vi c(n);
  cin >> c;
  vi l(q), r(q);
  rep(i, q) cin >> l[i] >> r[i];
  rep(i, q) {
    l[i]--;
    r[i]--;
  }
  int d = sqrt(n);
  auto cmp = [&](int a, int b) {
    if (l[a] / d < l[b] / d)
      return true;
    if (l[a] / d == l[b] / d and r[a] < r[b])
      return true;
    return false;
  };
  vi ord(q);
  iota(all(ord), 0);
  sort(all(ord), cmp);
  int nl = l[ord[0]], nr = r[ord[0]];
  map<int, int> mp;
  for (int i = nl; i <= nr; i++)
    mp[c[i]]++;
  vi ans(q);
  ans[ord[0]] = mp.size();
  auto erase = [&](int i) {
    if (--mp[i] == 0) {
      mp.erase(i);
    }
  };
  auto add = [&](int i) { mp[i]++; };
  rep2(i, 1, n) {
    if (nl <= l[ord[i]]) {
      while (nl < l[ord[i]]) {
        erase(c[nl++]);
      }
    } else {
      while (nl > l[ord[i]]) {
        add(c[--nl]);
      }
    }
    if (nr >= r[ord[i]]) {
      while (nr > r[ord[i]]) {
        erase(c[nr--]);
      }
    } else {
      while (nr < r[ord[i]]) {
        add(c[++nr]);
      }
    }
    ans[ord[i]] = mp.size();
  }
  rep(i, q) cout << ans[i] << '\\n';
}