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

#include <bits/stdc++.h>

using namespace std;
struct seg {
  int size = 1;
  vector<int> tizzi;
  int neu = 0;
  seg(int n) {
    while (size < n)
      size *= 2;
    tizzi.assign(size * 2, neu);
  }
  int comb(int &a, int &b) { return a + b; }
  void set(int i, int v) { set(i, v, 0, 0, size); }
  void set(int i, int v, int x, int lx, int rx) {
    if (rx - lx == 1) {
      tizzi[x] = v;
      return;
    }
    int m = (lx + rx) / 2;
    if (i < m)
      set(i, v, 2 * x + 1, lx, m);
    else
      set(i, v, 2 * x + 2, m, rx);
    tizzi[x] = comb(tizzi[2 * x + 1], tizzi[2 * x + 2]);
  }
  int query(int l, int r, int x, int lx, int rx) {
    if (lx >= r || l >= rx)
      return neu;
    if (l <= lx && r >= rx)
      return tizzi[x];
    int m = (lx + rx) / 2;
    int li = query(l, r, 2 * x + 1, lx, m);
    int ri = query(l, r, 2 * x + 2, m, rx);
    return comb(li, ri);
  }
  int query(int l, int r) { return query(l, r, 0, 0, size); }
};
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int N, M;
  cin >> N >> M;
  vector<int> in(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> in[i];
  vector<int> lastind(*max_element(in.begin(), in.end()) + 1, 0);
  deque<pair<int, int>> righth[N + 1];
  int ans[M];
  seg f(N + 1);
  for (int i = 0; i < M; i++) {
    int l, r;
    cin >> l >> r;
    righth[r].push_back({l, i});
  }
  for (int i = 1; i <= N; i++) {
    if (lastind[in[i]] > 0)
      f.set(lastind[in[i]], 0);
    lastind[in[i]] = i;
    f.set(i, 1);
    for (int q = 0; q < righth[i].size(); q++) {
      ans[righth[i][q].second] = f.query(righth[i][q].first, i + 1);
    }
  }
  for (int i = 0; i < M; i++)
    cout << ans[i] << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
struct seg {
  int size = 1;
  vector<unordered_map<int, int>> tizzi;
  unordered_map<int, int> neu;
  seg(int n) {
    while (size < n)
      size *= 2;
    tizzi.assign(size * 2, neu);
  }
  unordered_map<int, int> comb(unordered_map<int, int> &a,
                               unordered_map<int, int> &b) {
    if (a.empty() || b.empty()) {
      return (a.empty()) ? b : a;
    }
    unordered_map<int, int> c;
    //   c.reserve(a.size()+b.size());
    for (pair<int, int> aa : a) {
      c[aa.first] = 1;
    }
    for (pair<int, int> aa : b) {
      c[aa.first] = 1;
    }
    return c;
  }
  void build(vector<int> &sac) { build(sac, 0, 0, size); }
  void build(vector<int> &sac, int x, int lx, int rx) {
    if (rx - lx == 1) {
      if (lx < (int)sac.size()) {
        tizzi[x][sac[lx]] = 1;
      }
      return;
    }
    int m = (lx + rx) / 2;
    build(sac, 2 * x + 1, lx, m);
    build(sac, 2 * x + 2, m, rx);
    tizzi[x] = comb(tizzi[2 * x + 1], tizzi[2 * x + 2]);
  }
  unordered_map<int, int> query(int l, int r, int x, int lx, int rx) {
    if (lx >= r || l >= rx)
      return neu;
    if (l <= lx && r >= rx)
      return tizzi[x];
    int m = (lx + rx) / 2;
    unordered_map<int, int> li = query(l, r, 2 * x + 1, lx, m);
    unordered_map<int, int> ri = query(l, r, 2 * x + 2, m, rx);
    return comb(li, ri);
  }
  int query(int l, int r) { return query(l, r, 0, 0, size).size(); }
};
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int N, M;
  cin >> N >> M;
  vector<int> in(N);
  for (int &i : in)
    cin >> i;
  seg f(N);
  f.build(in);
  while (M--) {
    //  cout<<"AS";
    int l, r;
    cin >> l >> r;
    l--;
    cout << f.query(l, r) << '\\n';
  }
}