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

#include <bits/stdc++.h>
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
typedef long long ll;
class FenWick {
  int idx, N;
  vector<ll> bit;

public:
  FenWick(int n) {
    N = n;
    bit.resize(N + 1, 0);
  }
  ll sum(int id) {
    ll ret = 0;
    for (; id > 0; id -= (id & (-id))) {
      ret += bit[id];
    }
    return ret;
  }
  ll sum(int l, int r) { return sum(r) - sum(l - 1); }
  void update(int id, ll val) {
    for (; id <= N; id += (id & (-id))) {
      bit[id] += val;
    }
  }
};
struct Query {
  int l, r, idx;
  bool operator<(const Query &rhs) const { return r < rhs.r; }
};
int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> vals(N);
  for (auto &i : vals) {
    cin >> i;
  }
  vector<Query> queries(Q);
  for (int i = 0; i < Q; i++) {
    cin >> queries[i].l >> queries[i].r;
    queries[i].idx = i;
  }
  sort(queries.begin(), queries.end());
  FenWick bit(N);
  int qno = 0;
  vector<int> last(N + 10, -1), ans(Q);
  for (int i = 1; i <= N; i++) {
    if (last[vals[i - 1]] != -1) {
      bit.update(last[vals[i - 1]], -1);
    }
    bit.update(i, 1);
    last[vals[i - 1]] = i;
    while (queries[qno].r == i && qno < Q) {
      ans[queries[qno].idx] = bit.sum(queries[qno].l, queries[qno].r);
      qno++;
    }
    if (qno > Q)
      break;
  }
  for (auto it : ans) {
    cout << it << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define all(x) (x).begin(), (x).end()
const int N = 5000010;
vector<set<int>> t(2 * N);
set<int> combine(set<int> &a, set<int> &b) {
  set<int> c;
  merge(all(a), all(b), inserter(c, c.end()));
  // debug()<<imie(a) imie(b) imie(c);
  return c;
}
void build(vector<int> &a, int v, int tl, int tr) {
  if (tl == tr) {
    t[v].insert(a[tl]);
  } else {
    int mid = (tl + tr) / 2;
    build(a, v * 2, tl, mid);
    build(a, v * 2 + 1, mid + 1, tr);
    t[v].insert(all(t[v * 2]));
    t[v].insert(all(t[v * 2 + 1]));
  }
}
set<int> query(int v, int tl, int tr, int l, int r) {
  set<int> emp, x, y;
  if (l > r) {
    return emp;
  }
  if (l == tl && r == tr) {
    return t[v];
  }
  int mid = (tl + tr) / 2;
  x = query(v * 2, tl, mid, l, min(r, mid));
  y = query(v * 2 + 1, mid + 1, tr, max(l, mid + 1), r);
  emp.insert(all(x));
  emp.insert(all(y));
  return emp;
}
int main() {
  // cout<<t.size()<<endl;
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, q;
  cin >> n >> q;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int root = 1, tl = 0, tr = n - 1;
  build(v, root, tl, tr);
  while (q--) {
    int l, r;
    cin >> l >> r;
    --l, --r;
    cout << (query(root, tl, tr, l, r).size()) << "\\n";
  }
}