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

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const int MX = 500005;

struct segtree {
  int size;
  vector<int> sums;

  void init(int n) {
    size = 1;
    while (size < n)
      size *= 2;
    sums.assign(2 * size, 0);
  }

  void set(int i, int v, int x, int lx, int rx) {
    if (rx - lx == 1) {
      sums[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);
    }

    sums[x] = sums[2 * x + 1] + sums[2 * x + 2];
  }

  void set(int i, int v) { set(i, v, 0, 0, size); }

  int query(int l, int r, int x, int lx, int rx) {
    if (lx >= r || l >= rx)
      return 0;
    if (lx >= l && rx <= r)
      return sums[x];

    int m = (lx + rx) / 2;
    int q1 = query(l, r, 2 * x + 1, lx, m);
    int q2 = query(l, r, 2 * x + 2, m, rx);
    return q1 + q2;
  }

  int query(int l, int r) { return query(l, r, 0, 0, size); }
};

int a[MX], loc[MX], ans[MX];
pair<pair<int, int>, int> qv[MX];

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n, q;
  segtree st;
  cin >> n >> q;
  st.init(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    --a[i];
    loc[a[i]] = -1;
  }

  // Endpoints are inclusive so +1 to right side query
  for (int i = 0; i < q; ++i) {
    cin >> qv[i].first.second >> qv[i].first.first;
    --qv[i].first.first, --qv[i].first.second;
    qv[i].second = i;
  }
  sort(qv, qv + q);

  for (int i = 0, pt = 0; i < q; ++i) {
    while (pt <= qv[i].first.first) {
      if (loc[a[pt]] != -1) {
        st.set(loc[a[pt]], 0);
        // cout << "RESETTING " << loc[a[pt]] << '\\n';
      }
      loc[a[pt]] = pt;
      st.set(pt, 1);
      // cout << "SETTING " << pt << '\\n';
      ++pt;
    }

    // cout << "QUERYING: " << qv[i].first.second << ' ' << qv[i].first.first +
    // 1 << '\\n';
    ans[qv[i].second] = st.query(qv[i].first.second, qv[i].first.first + 1);
  }

  for (int i = 0; i < q; ++i) {
    cout << ans[i] << '\\n';
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

struct segtree {
  int size;
  vector<set<int>> uniqs;

  void init(int n) {
    size = 1;
    set<int> empty;
    while (size < n)
      size *= 2;
    uniqs.assign(2 * size, empty);
  }

  void build(vector<int> &a, int x, int lx, int rx) {
    if (rx - lx == 1) {
      if (lx < sz(a)) {
        uniqs[x].insert(a[lx]);
      }
      return;
    }

    int m = (lx + rx) / 2;
    build(a, 2 * x + 1, lx, m);
    build(a, 2 * x + 2, m, rx);
    uniqs[x].insert(all(uniqs[2 * x + 1]));
    uniqs[x].insert(all(uniqs[2 * x + 2]));
  }

  void build(vector<int> &a) { build(a, 0, 0, size); }

  set<int> query(int l, int r, int x, int lx, int rx) {
    set<int> s;
    if (lx >= r || l >= rx)
      return s;
    if (lx >= l && rx <= r)
      return uniqs[x];

    int m = (lx + rx) / 2;
    set<int> q1 = query(l, r, 2 * x + 1, lx, m);
    set<int> q2 = query(l, r, 2 * x + 2, m, rx);
    s.insert(all(q1));
    s.insert(all(q2));
    return s;
  }

  set<int> query(int l, int r) { return query(l, r, 0, 0, size); }
};

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n, q;
  segtree st;
  cin >> n >> q;
  st.init(n);

  vector<int> a(n);
  for (auto &i : a)
    cin >> i;
  st.build(a);

  while (q--) {
    int l, r;
    cin >> l >> r;
    --l;
    cout << sz(st.query(l, r)) << '\\n';
  }
}