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

#include <bits/stdc++.h>

using namespace std;

struct point {
  int left, right, block, id;
};

bool cmp(const point &lhs, const point &rhs) {
  if (lhs.block < rhs.block)
    return true;
  else if (lhs.block > rhs.block)
    return false;
  else
    return lhs.right < rhs.right;
}

int main() {
  int n, q;
  scanf("%d", &n);
  vector<int> A(n);
  scanf("%d", &q);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);

  int block_size = sqrt(n);

  vector<point> query(q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &query[i].left, &query[i].right);
    query[i].left--, query[i].right--;
    query[i].block = query[i].left / block_size;
    query[i].id = i;
  }

  sort(query.begin(), query.end(), cmp);

  vector<int> solution(q);
  vector<int> hash(1000100);

  int left = 0, right = 0, answer = 1;
  hash[A[0]] = 1;

  for (const point &task : query) {
    while (left < task.left) {
      hash[A[left]]--;
      answer -= (hash[A[left]] == 0);
      left++;
    }
    while (left > task.left) {
      left--;
      hash[A[left]]++;
      answer += (hash[A[left]] == 1);
    }
    while (right < task.right) {
      right++;
      hash[A[right]]++;
      answer += (hash[A[right]] == 1);
    }
    while (right > task.right) {
      hash[A[right]]--;
      answer -= (hash[A[right]] == 0);
      right--;
    }
    solution[task.id] = answer;
  }

  for (int answer : solution)
    printf("%d\\n", answer);

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define FRE                                                                    \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define all(v) (v).begin(), (v).end()
#define endl "\\n"
#define int long long
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = a; i > b; i--)
#define vi vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define SET(A, VAL) memset(A, VAL, sizeof(A))
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define trace4(a, b, c, d)                                                     \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f)                                               \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \\
       << #f << ": " << f << endl
const int MAX = 1e6 + 8;
const int MOD = 1e9 + 7;
const int sz = 1e5 + 5;
void f_io() {
  IOS;
  // #ifndef ONLINE_JUDGE
  // FRE
  // #endif
}
struct segtree {
  int size;
  vector<unordered_set<int>> a;
  void init(int n) {
    size = 1;
    while (size < n)
      size *= 2;
    a.resize(2 * size);
  }
  unordered_set<int> merge(unordered_set<int> s1, unordered_set<int> s2) {
    for (auto el : s2)
      s1.insert(el);
    return s1;
  }
  void build(vector<int> &nums, int x, int lx, int rx) {
    if (rx - lx == 1) {
      if (lx < (int)nums.size()) {
        unordered_set<int> temp;
        temp.insert(nums[lx]);
        a[x] = temp;
      }
      return;
    }
    int m = (lx + rx) / 2;
    build(nums, 2 * x + 1, lx, m);
    build(nums, 2 * x + 2, m, rx);
    a[x] = merge(a[2 * x + 1], a[2 * x + 2]);
  }
  void build(vector<int> &nums) { build(nums, 0, 0, size); }
  // void upd(int i, int v, int x, int lx, int rx) {
  //     if(rx - lx == 1){
  //         a[x] = v;
  //         return;
  //     }
  //     int m = (lx + rx) / 2;
  //     if(i < m){
  //         upd(i, v, 2 * x + 1, lx, m);
  //     } else {
  //         upd(i, v, 2 * x + 2, m, rx);
  //     }
  //     a[x] = min(a[2 * x + 1], a[2 * x + 2]);
  // }
  // void upd(int i, int v) {
  //     upd(i, v, 0, 0, size);
  // }

  unordered_set<int> sum(int l, int r, int x, int lx, int rx) {
    if (lx >= r || rx <= l) {
      unordered_set<int> ret;
      return ret;
    }
    if (lx >= l && rx <= r)
      return a[x];
    int m = (lx + rx) / 2;
    auto s1 = sum(l, r, 2 * x + 1, lx, m);
    auto s2 = sum(l, r, 2 * x + 2, m, rx);
    return merge(s1, s2);
  }

  unordered_set<int> sum(int l, int r) { return sum(l, r, 0, 0, size); }
};

int32_t main() {
  f_io();
  int n, m;
  cin >> n >> m;
  segtree st;
  st.init(n);
  vector<int> a(n);
  for (auto &el : a)
    cin >> el;
  // trace2(n, n);
  st.build(a);
  while (m--) {
    int l, r;
    cin >> l >> r;
    l--;
    auto el = st.sum(l, r);
    cout << el.size() << endl;
  }
}