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

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#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 srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (const auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define sar(a, n)                                                              \\
  {                                                                            \\
    cout << #a << ":";                                                         \\
    rep(pachico, n) cout << " " << a[pachico];                                 \\
    cout << endl;                                                              \\
  }

using namespace std;

template <typename S, typename T> auto &operator<<(ostream &o, pair<S, T> p) {
  return o << "{" << p.fi << "," << p.se << "}";
}
template <typename T> auto &operator<<(ostream &o, set<T> s) {
  for (auto &e : s)
    o << e << " ";
  return o;
}
template <typename S, typename T, typename U>
auto &operator<<(ostream &o, priority_queue<S, T, U> q) {
  while (!q.empty())
    o << q.top() << " ", q.pop();
  return o;
}
template <typename K, typename T> auto &operator<<(ostream &o, map<K, T> &m) {
  for (auto &e : m)
    o << e << " ";
  return o;
}
template <typename T> auto &operator<<(ostream &o, vector<T> v) {
  for (auto &e : v)
    o << e << " ";
  return o;
}
void ashow() { cout << endl; }
template <typename T, typename... A> void ashow(T t, A... a) {
  cout << t << " ";
  ashow(a...);
}
template <typename S, typename T, typename U> struct TRI {
  S fi;
  T se;
  U th;
  TRI() {}
  TRI(S f, T s, U t) : fi(f), se(s), th(t) {}
  bool operator<(const TRI &_) const {
    return (fi == _.fi) ? ((se == _.se) ? (th < _.th) : (se < _.se))
                        : (fi < _.fi);
  }
};
template <typename S, typename T, typename U>
auto &operator<<(ostream &o, TRI<S, T, U> &t) {
  return o << "{" << t.fi << "," << t.se << "," << t.th << "}";
}

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef TRI<int, int, int> tri;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_VAL = 500005;
int a[MAX_VAL];   // 要素
int cnt[MAX_VAL]; // 区間内のiの個数
int res;          // 区間内の種類の数

class Mo {
private:
  vector<int> left, right;
  const int width;
  void add(const int id);
  void del(const int id);

public:
  vector<int> ans;

  Mo(const int n) : width((int)sqrt(n)) {}
  // クエリ[l,r)
  void insert(const int l, const int r) {
    left.push_back(l), right.push_back(r);
  }
  void solve() {
    const int sz = (int)left.size();
    int nl = 0, nr = 0;
    vector<int> ord(sz);
    iota(ord.begin(), ord.end(), 0);
    sort(ord.begin(), ord.end(), [&](const int a, const int b) {
      const int c = left[a] / width, d = left[b] / width;
      return (c == d)
                 ? ((c & 1) ? (right[b] < right[a]) : (right[a] < right[b]))
                 : (c < d);
    });
    ans.resize(sz);
    for (const int id : ord) {
      while (nl > left[id])
        add(--nl);
      while (nr < right[id])
        add(nr++); // add
      while (nl < left[id])
        del(nl++);
      while (nr > right[id])
        del(--nr); // del
      ans[id] = res;
    }
  }
};

// idは元の配列のインデックス
void Mo::add(const int id) {
  ++cnt[a[id]];
  if (cnt[a[id]] == 1)
    ++res;
}

void Mo::del(const int id) {
  --cnt[a[id]];
  if (cnt[a[id]] == 0)
    --res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, q, l, r;
  cin >> n >> q;
  Mo mo(n);
  rep(i, n) cin >> a[i];
  rep(i, q) {
    cin >> l >> r;
    --l, mo.insert(l, r);
  }
  mo.solve();
  const auto &ans = mo.ans;
  rep(i, q) { cout << ans[i] << "\\n"; }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pi 3.1415926536
#define pb push_back
#define ff first
#define ss second
#define N 1000000007
#define INF (1ll << 60) - 1
#define M 998244353
#define rep(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define nrep(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define pl pair<ll, ll>
#define pll pair<ll, pair<ll, ll>>
vector<pair<int, pair<int, int>>> v;
int a[500005], ans[500005];
int m[500005];
int block;
bool comp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
  if (a.ss.ff / block != b.ss.ff / block)
    return a.ss.ff / block < b.ss.ff / block;
  else
    a.ss.ss < b.ss.ss;
}
int main() {
  int i, t, y, n, z, l = 0, j, r, q, x;
  scanf("%d %d", &n, &q);
  rep(i, 0, n - 1) { scanf("%d", &a[i]); }
  rep(i, 0, q - 1) {
    scanf("%d %d", &x, &y);
    x--;
    y--;
    v.pb({i, {x, y}});
  }
  block = (int)sqrt(n);
  sort(v.begin(), v.end(), comp);
  l = 0;
  r = -1;
  int cnt = 0;
  rep(i, 0, q - 1) {
    x = v[i].ss.ff;
    y = v[i].ss.ss;
    while (l > x) {
      l--;
      if (m[a[l]] == 0)
        cnt++;
      m[a[l]]++;
    }
    while (r < y) {
      r++;
      if (m[a[r]] == 0)
        cnt++;
      m[a[r]]++;
    }
    while (l < x) {
      m[a[l]]--;
      if (m[a[l]] == 0)
        cnt--;
      l++;
    }
    while (r > y) {
      m[a[r]]--;
      if (m[a[r]] == 0)
        cnt--;
      r--;
    }
    ans[v[i].ff] = cnt;
  }
  rep(i, 0, q - 1) printf("%d\\n", ans[i]);
}