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

#include <bits/stdc++.h>

#define pb push_back
#define fill(x, y) memset(x, y, sizeof(y))
#define all(x) (x).begin(), (x).end()
#define debug(x)                                                               \\
  { cerr << #x << " = " << x << endl; }
#define IO                                                                     \\
  {                                                                            \\
    ios_base::sync_with_stdio(false);                                          \\
    cin.tie(0);                                                                \\
  }
#define read(x) freopen(x, "r", stdin)
#define write(x) freopen(x, "w", stdout)

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;

const int N = 500005;
int tree[4 * N];

void update(int node, int l, int r, int idx, int val) {
  if (idx < l or idx > r)
    return;
  if (l == r) {
    tree[node] = val;
    return;
  }
  int m = (l + r) / 2;
  update(2 * node, l, m, idx, val);
  update(2 * node + 1, m + 1, r, idx, val);
  tree[node] = tree[2 * node] + tree[2 * node + 1];
}

int queryd(int node, int l, int r, int s, int e) {
  if (e < l or s > r)
    return 0;
  if (s <= l and r <= e)
    return tree[node];

  int m = (l + r) / 2;
  return queryd(2 * node, l, m, s, e) + queryd(2 * node + 1, m + 1, r, s, e);
}

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> arr(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  pair<int, int> query[q];
  map<pair<int, int>, int> q_ans;

  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    query[i] = {r, l};
  }
  pair<int, int> q2[q];
  for (int i = 0; i < q; i++)
    q2[i] = query[i];
  sort(query, query + q);
  int last_occur[n + 1]; // index
  int last_pos[n + 1];
  for (int i = 0; i <= n; i++) {
    last_pos[i] = -1;
    last_occur[i] = 0;
  }
  int idx = 0;
  for (int i = 0; i < q; i++) {
    int l = query[i].second, r = query[i].first;
    while (idx < r) {
      update(1, 0, n, idx, 1);
      if (last_pos[arr[idx]] != -1)
        update(1, 0, n, last_pos[arr[idx]], 0);
      last_pos[arr[idx]] = idx;
      idx++;
    }
    int ans = 0;
    ans = queryd(1, 0, n, l - 1, r - 1);
    q_ans[query[i]] = ans;
  }

  for (int i = 0; i < q; i++) {
    cout << q_ans[q2[i]] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define IO                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define ll long long
#define pii pair<int, int>

using namespace std;

set<int> tree[4 * 500005];

set<int> merge(set<int> &a, set<int> &b) {
  set<int> res;
  for (auto i : a)
    res.insert(i);
  for (auto i : b)
    res.insert(i);
  return res;
}

void build(int node, int l, int r, vector<int> &a) {
  if (l == r) {
    tree[node].insert(a[l]);
    return;
  }
  int m = (l + r) / 2;
  build(2 * node, l, m, a);
  build(2 * node + 1, m + 1, r, a);
  tree[node] = merge(tree[2 * node], tree[2 * node + 1]);
}

set<int> query(int node, int l, int r, int s, int e) {
  if (s <= l and e >= r)
    return tree[node];
  if (r < s or l > e)
    return set<int>();

  int m = (l + r) / 2;
  set<int> q1, q2;
  q1 = query(2 * node, l, m, s, e);
  q2 = query(2 * node + 1, m + 1, r, s, e);

  return merge(q1, q2);
}

int main() {
  // freopen("output.txt", "w", stdout);
  IO;
  int n, q;
  cin >> n >> q;
  vector<int> arr(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  build(1, 0, n - 1, arr);

  while (q--) {
    int x, y;
    cin >> x >> y;
    set<int> q = query(1, 0, n - 1, x - 1, y - 1);
    cout << q.size() << endl;
  }
}