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

// By Debajit Chakraborty
#include <bits/stdc++.h>
using namespace std;

#define SPEED                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define MP make_pair
#define pb push_back
#define ff first
#define ss second
#define sz(x) x.size()
#define all(c) c.begin(), c.end()
#define int long long
#define fr(i, j, x) for (int i = j; i < x; i++)
#define rep(i, j, x) for (int i = j; i <= x; i++)
#define rev(i, j, x) for (int i = j; i >= x; i--)
#define mem(x, val) memset(x, val, sizeof(x))
#define endl '\\n'
#define vi vector<int>
#define pii pair<int, int>

const int mod = 1e9 + 7;
const int MAX = 5e5 + 10;
const int INF = 1e8;

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;
}

#ifndef ONLINE_JUDGE
freopen("inputf.in", "r", stdin);
// freopen("outputf.in", "w", stdout);
#endif
void answeringQueries(int arr[], int n, Query queries[], int q) {
  // initializing bit array
  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;
}

void solve() {
  int n, q, l, r;
  cin >> n >> q;
  int a[n];
  rep(i, 0, n - 1) { cin >> a[i]; }
  Query queries[q + 1];
  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    queries[i].idx = i;
    queries[i].l = l - 1;
    queries[i].r = r - 1;
  }
  sort(queries, queries + q, cmp);
  answeringQueries(a, n, queries, q);
}

int32_t main() {
  SPEED;
  int T = 1;
  // cin>>T;
  int t1 = 1;
  while (true) {
    // cout<<"Case #"<<t1<<": ";
    // cout<<endl;
    solve();
    t1++;
    if (t1 > T)
      break;
  }
}


Error: TLE

Fixed submission:

// By Debajit Chakraborty
#include <bits/stdc++.h>
using namespace std;

#define SPEED                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define MP make_pair
#define pb push_back
#define ff first
#define ss second
#define sz(x) x.size()
#define all(c) c.begin(), c.end()
// #define int long long
#define fr(i, j, x) for (int i = j; i < x; i++)
#define rep(i, j, x) for (int i = j; i <= x; i++)
#define rev(i, j, x) for (int i = j; i >= x; i--)
#define mem(x, val) memset(x, val, sizeof(x))
#define endl '\\n'
#define vi vector<int>
#define pii pair<int, int>

const int mod = 1e9 + 7;
const int N = 5e5 + 10;
const int INF = 1e8;

int n, c[N], q, l, r;
set<int> t[4 * N];

void build(int i, int l, int r) {
  if (l == r) {
    t[i].insert(c[l]);
    return;
  }
  int mid = (l + r) / 2;
  build(i * 2, l, mid);
  build(i * 2 + 1, mid + 1, r);
  t[i].insert(t[2 * i].begin(), t[2 * i].end());
  t[i].insert(t[2 * i + 1].begin(), t[2 * i + 1].end());
}

set<int> query(int index, int l, int r, int lq, int rq) {
  set<int> left, right, result;
  if (l > rq || lq > r) {
    return result;
  }
  if (lq <= l && r <= rq) {
    return t[index];
  }
  int mid = (l + r) / 2;
  left = query(2 * index, l, mid, lq, rq);
  result.insert(left.begin(), left.end());

  right = query(2 * index + 1, mid + 1, r, lq, rq);
  result.insert(right.begin(), right.end());
  return result;
}

void solve() {
  cin >> n >> q;
  rep(i, 0, n - 1) { cin >> c[i]; }
  build(1, 0, n - 1);
  set<int> now;
  while (q--) {
    cin >> l >> r;
    now = query(1, 0, n - 1, l - 1, r - 1);
    cout << now.size() << endl;
  }
}

int32_t main() {
  SPEED;
  int T = 1;
  // cin>>T;
  int t1 = 1;
  while (true) {
    // cout<<"Case #"<<t1<<": ";
    // cout<<endl;
    solve();
    t1++;
    if (t1 > T)
      break;
  }
}