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

#include <bits/stdc++.h>
using namespace std;
int tree[2000020];
int ar[500003], idx[6000009], ans[500009];
struct str {
  int a, b, c;
} qry[500002];
bool comp(str s1, str s2) {
  if (s1.b != s2.b)
    return s1.b < s2.b;
  return s1.a < s2.a;
}
void update(int p, int lo, int hi, int pos, int val) {
  if (lo == hi) {
    tree[p] = val;
    return;
  }
  int md = (lo + hi) / 2;
  if (pos <= md)
    update(p * 2, lo, md, pos, val);
  else
    update(p * 2 + 1, md + 1, hi, pos, val);
  tree[p] = tree[p * 2] + tree[p * 2 + 1];
}
int query(int p, int lo, int hi, int l, int r) {
  if (l <= lo && hi <= r)
    return tree[p];
  if (hi < l || r < lo)
    return 0;
  // cout<<lo<<" "<<hi<<" "<<l<<" k "<<r<<endl ;
  int md = (lo + hi) / 2;
  int x = query(p * 2, lo, md, l, r), y = query(p * 2 + 1, md + 1, hi, l, r);
  return x + y;
}
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%d", &ar[i]);
  for (int i = 0; i < q; i++) {
    int a, b, c;
    cin >> a >> b;
    c = i;
    qry[i].a = a;
    qry[i].b = b;
    qry[i].c = c;
  }
  sort(qry, qry + q, comp);
  // for(int i = 0 ; i < q ; i++) cout<<qry[i].a<<" "<<qry[i].b<<"
  // "<<qry[i].c<<endl ; cout<<endl ;
  int ps = 1;
  for (int i = 0; i < q; i++) {
    while (ps <= qry[i].b) {
      int x = ar[ps];
      if (idx[x])
        update(1, 1, n, idx[x], 0);
      update(1, 1, n, ps, 1);
      idx[x] = ps;
      ps++;
    }
    // cout<<"i : "<<i<<endl ;
    // for(int j = 1 ; j <= 16 ; j++) cout<<tree[j]<<" " ; cout<<endl ;
    ans[qry[i].c] = query(1, 1, n, qry[i].a, qry[i].b); // cout<<endl ;
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
set<int> tree[2000020];
set<int> an;
int ar[500003];
void build(int p, int lo, int hi) {
  if (lo == hi) {
    tree[p].insert(ar[lo]);
    return;
  }
  int md = (lo + hi) / 2;
  build(p * 2, lo, md);
  build(p * 2 + 1, md + 1, hi);
  vector<int> v;
  merge(tree[p * 2].begin(), tree[p * 2].end(), tree[p * 2 + 1].begin(),
        tree[p * 2 + 1].end(), back_inserter(v));

  set<int> s(v.begin(), v.end());
  tree[p] = s;
  // cout<<p<<endl ;
  // for(int i = 0 ; i < tree[p].size() ; i++) cout<<tree[p][i]<<" " ;
  // cout<<endl ;
}
set<int> query(int p, int lo, int hi, int l, int r) {
  set<int> va, vb;
  vector<int> vc;
  if (lo >= l && hi <= r) {
    return tree[p];
  }
  if (hi < l || r < lo) {
    return va;
  }
  int md = (lo + hi) / 2;
  va = query(p * 2, lo, md, l, r);
  vb = query(p * 2 + 1, md + 1, hi, l, r);
  merge(va.begin(), va.end(), vb.begin(), vb.end(), back_inserter(vc));
  set<int> s(vc.begin(), vc.end());
  return s;
}
int main() {
  /*set<int>s1,s2,s3 ;
  s1.insert(1) ; s1.insert(1) ; s1.insert(3) ;
  s2.insert(4) ; s2.insert(5) ; s2.insert(6) ;
  vector<int>v ;
  merge(s1.begin(),s1.end(),s2.begin(),s2.end(),back_inserter(v)) ;
  set<int>s(v.begin(),v.end()) ;
  for(int x : s) cout<<x<<" " ; cout<<endl ;
  for(int x : s2) cout<<x<<" " ; cout<<endl ;*/
  int n, q;
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%d", &ar[i]);
  build(1, 1, n); // cout<<endl ;
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    an.clear();
    an = query(1, 1, n, l, r); // cout<<endl ;
    int sz = an.size();
    printf("%d\\n", sz);
  }
}