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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pb push_back
#define pii pair<ll, ll>
#define tii tuple<ll, ll, ll>
#define all(v) (v).begin(), (v).end()
#define ll long long
#define ull unsigned long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);
#define ld long double
#define mod 1000000007
#define PI 3.141592653589793238
#define phi 1.638
#define eps 1e-9
#define endl "\\n"
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const int MAX = 1000001;

int n, q;

// structure to store queries
struct Query {
  int l, r, idx;
};

// cmp function to sort queries according to r
bool cmp(Query x, Query y) { return x.r < y.r; }

// updating the bit array
void update(int idx, int val, int bit[], int n) {
  for (; idx <= n; idx += idx & -idx)
    bit[idx] += val;
}

// querying the bit array
int query(int idx, int bit[], int n) {
  int sum = 0;
  for (; idx > 0; idx -= idx & -idx)
    sum += bit[idx];
  return sum;
}

void answeringQueries(vector<int> &arr, vector<Query> &queries) {
  int bit[n + 1];
  memset(bit, 0, sizeof(bit));

  // holds the rightmost index of any number
  // as numbers of a[i] are less than or equal to 10^6
  int last_visit[MAX];
  memset(last_visit, -1, sizeof(last_visit));

  // answer for each query
  int ans[q];
  int query_counter = 0;
  for (int i = 0; i < n; i++) {
    // If last visit is not -1 update -1 at the
    // idx equal to last_visit[arr[i]]
    if (last_visit[arr[i]] != -1)
      update(last_visit[arr[i]] + 1, -1, bit, n);

    // Setting last_visit[arr[i]] as i and updating
    // the bit array accordingly
    last_visit[arr[i]] = i;
    update(i + 1, 1, bit, n);

    // If i is equal to r of any query  store answer
    // for that query in ans[]
    while (query_counter < q && queries[query_counter].r == i) {
      ans[queries[query_counter].idx] =
          query(queries[query_counter].r + 1, bit, n) -
          query(queries[query_counter].l, bit, n);
      query_counter++;
    }
  }

  // print answer for each query
  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
}

int main() {
  cin >> n >> q;
  vector<int> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];

  vector<Query> queries(q);

  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    l -= 1;
    r -= 1;
    queries[i].l = l;
    queries[i].r = r;
    queries[i].idx = i;
  }

  sort(all(queries), cmp);
  answeringQueries(a, queries);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pb push_back
#define pii pair<ll, ll>
#define tii tuple<ll, ll, ll>
#define all(v) (v).begin(), (v).end()
#define ll long long
#define ull unsigned long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);
#define ld long double
#define mod 1000000007
#define PI 3.141592653589793238
#define phi 1.638
#define eps 1e-9
#define endl "\\n"
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const int N = 5e5 + 5;
int n, q;

vector<int> seg[2 * N];

void merge(vector<int> &a, vector<int> &b, vector<int> &c) {
  int i = 0, j = 0;
  while (i < a.size() && j < b.size()) {
    if (a[i] < b[j]) {
      if (c.size() > 0 && a[i] == c.back()) {
        i++;
      } else
        c.push_back(a[i++]);
    } else {
      if (c.size() > 0 && b[j] == c.back()) {
        j++;
      } else
        c.push_back(b[j++]);
    }
  }

  while (i < a.size()) {
    if (c.size() > 0 && a[i] == c.back())
      i++;
    else
      c.push_back(a[i++]);
  }
  while (j < b.size()) {
    if (c.size() > 0 && b[j] == c.back()) {
      j++;
    } else
      c.push_back(b[j++]);
  }
}

void build() {
  for (int i = n - 1; i > 0; --i) {
    merge(seg[2 * i], seg[2 * i + 1], seg[i]);
  }
}

int query(int l, int r) {
  l += n;
  r += n + 1;
  vector<int> ans;
  for (; l < r; l /= 2, r /= 2) {
    if (l & 1) {
      vector<int> temp;
      merge(seg[l++], ans, temp);
      ans = temp;
    }
    if (r & 1) {
      vector<int> temp;
      merge(seg[--r], ans, temp);
      ans = temp;
    }
  }

  return ans.size();
}

int main() {
  fastio cin >> n >> q;

  for (int i = 0; i < n; ++i) {
    int val;
    cin >> val;
    seg[i + n].push_back(val);
  }
  build();

  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << query(l - 1, r - 1) << endl;
  }
}