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

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

ll neutral = 0;
struct fenewicktree {
  vector<ll> bit;
  ll n;
  fenewicktree(ll n) {
    // zero based-indexing
    this->n = n;
    bit.assign(n, neutral);

    // one based idexing
    // this->n=n+1;
    // bit.assign(n+1,neutral);
  }
  void build(vector<ll> &v) {
    for (int i = 0; i < v.size(); i++)
      add(i, v[i]);
  }

  ll sum(ll r) {
    ll ret = 0;
    // zero based indexing
    for (; r > -1; r = (r & (r + 1)) - 1) {
      // cout<<" sum "<<r<<endl;
      ret += bit[r];
    }

    // one based
    // for(++r;r>0;r-= (r& -r))
    //     ret+=bit[r];
    return ret;
  }

  ll sum(ll l, ll r) { return sum(r) - sum(l - 1); }

  void add(ll idx, ll delta) {
    // zero based
    for (; idx < n; idx = (idx | (idx + 1))) {
      // cout<<" add2 "<<idx<<endl;
      bit[idx] += delta;
    }

    // one based
    //  for(++idx;idx<n;idx+= (idx& -idx))
    //      bit[idx]+=delta;
  }
  void show() {
    for (auto a : bit)
      cout << a << " ";
    cout << endl;
  }
};

bool comp(pair<ll, pair<ll, ll>> &p1, pair<ll, pair<ll, ll>> &p2) {
  return p1.second.second < p2.second.second;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, q;
  cin >> n >> q;
  ll arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  vector<pair<ll, pair<ll, ll>>> v(q);
  for (int tt = 0; tt < q; tt++) {
    cin >> v[tt].second.first >> v[tt].second.second;
    v[tt].second.first--;
    v[tt].second.second--;
    v[tt].first = tt;
  }
  sort(v.begin(), v.end(), comp);
  // for(auto a:v) cout<<a.second.first<<" "<<a.second.second<<endl;
  ll k = 0;
  fenewicktree ft(500001);
  ll last[600001];
  memset(last, -1, sizeof last);
  map<ll, ll> mp;
  for (int i = 0; i < n && k < v.size(); i++) {
    if (last[arr[i]] != -1) {
      ft.add(last[arr[i]], -1);
    }
    last[arr[i]] = i;
    ft.add(i, 1);
    while (k < v.size() && i == v[k].second.second) {
      mp[v[k].first] = ft.sum(v[k].second.first, v[k].second.second);
      k++;
    }
    // ft.show();
  }
  for (int i = 0; i < v.size(); i++)
    cout << mp[i] << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

struct node {
  set<int> st;
};

node merge(node n1, node n2) {
  node n3;
  for (auto a : n1.st)
    n3.st.insert(a);
  for (auto a : n2.st)
    n3.st.insert(a);
  return n3;
}

// void show(node x)
// {
//     cout<<x.a<<" "<<x.b<<endl<<x.c<<" "<<x.d<<endl<<endl;
// }

node neutral = {set<int>()};

node single(ll a) {
  set<int> st;
  st.insert(a);
  return {st};
}

struct segtree {
  ll size;
  vector<node> tree;
  void init(ll n) {
    size = 1;
    while (size < n)
      size *= 2;
    tree.assign(2 * size, neutral);
  }

  // using [lx,rx)
  void build(vector<int> &v, ll x, ll lx, ll rx) {
    if (lx + 1 == rx) {
      if (lx < v.size()) {
        tree[x] = single(v[lx]);
      }
      return;
    }
    ll m = (lx + rx) / 2;
    build(v, 2 * x + 1, lx, m);
    build(v, 2 * x + 2, m, rx);
    tree[x] = merge(tree[2 * x + 1], tree[2 * x + 2]);
  }

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

  void set(ll i, ll v, ll x, ll lx, ll rx) {
    if (lx + 1 == rx) {
      tree[x] = single(v);
      return;
    }
    ll m = (lx + rx) / 2;
    if (i < m)
      set(i, v, 2 * x + 1, lx, m);
    else
      set(i, v, 2 * x + 2, m, rx);
    tree[x] = merge(tree[2 * x + 1], tree[2 * x + 2]);
  }

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

  node query(ll l, ll r, ll x, ll lx, ll rx) {
    if (lx >= l && rx <= r) {
      return tree[x];
    }
    if (rx <= l || lx >= r)
      return neutral;
    ll m = (lx + rx) / 2;
    return merge(query(l, r, 2 * x + 1, lx, m), query(l, r, 2 * x + 2, m, rx));
  }

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, q;
  cin >> n >> q;
  vector<int> arr(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  segtree st;
  st.init(n);
  st.build(arr);
  int l, r;
  while (q--) {
    cin >> l >> r;
    cout << st.query(l - 1, r).st.size() << endl;
  }
}