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

#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back

#define limit (int)(5e5 + 5)
#define maxn (long long int)(2147483647)

using namespace std;
using lli = long long int;
using ulli = unsigned long long int;
using ld = long double;

int n, q;
int c[limit], ft[limit] = {}, ans[limit];
vector<int> a, ql[limit], qid[limit];

void update(int in, int d) {
  // cout << n;
  // cout << in << " " << d << "\\n";

  for (int i = in + 1; i <= n; i += i & (-i)) {
    // cout << in << " " << i << " " << d << "\\n";
    ft[i] += d;
  }

  return;
}

int sum(int in) {
  in++;

  int ans = 0;

  for (int i = in; i > 0; i -= i & (-i)) {
    ans += ft[i];
  }

  // cout << ans << " ";
  return ans;
}

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

  cin >> n >> q;

  a.assign(n, -1);

  for (int i = 0; i < n; ++i) {
    cin >> c[i];
    c[i]--;
  }

  int l, r;

  for (int i = 0; i < q; ++i) {
    cin >> l >> r;
    l--;
    r--;

    ql[r].push_back(l);
    qid[r].push_back(i);
  }

  for (int i = 0; i < n; ++i) {
    // for (int j = 0; j < n; ++j)
    // {
    //     cout << ft[j] << " ";
    // }
    // cout << "\\n";

    if (a[c[i]] != -1) {
      update(a[c[i]], -1);
    }

    a[c[i]] = i;
    update(a[c[i]], 1);

    for (int j = 0; j < ql[i].size(); ++j) {
      ans[qid[i][j]] = sum(i) - sum(ql[i][j] - 1);
    }
  }

  for (int i = 0; i < q; ++i) {
    cout << ans[i] << "\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>

#define fi first
#define se second
#define pb push_back

#define limit (int)(5e5 + 5)

#define maxn (long long int)(2147483647);

using namespace std;
using lli = long long int;
using ulli = unsigned long long int;
using ld = long double;

int n, q, sn, ca = 0;
int a[limit], cnt[limit] = {}, ans[limit];

struct query {
  int in;
  int l, r;

  bool operator>(query rhs) const {
    if ((l) / sn != (rhs.l) / sn) {
      return l > rhs.l;
    }

    if (r != rhs.r) {
      return r > rhs.r;
    }

    return l > rhs.l;
  }

  bool operator<(query rhs) const {
    if ((l) / sn != (rhs.l) / sn) {
      return l < rhs.l;
    }

    if (r != rhs.r) {
      return r < rhs.r;
    }

    return l < rhs.l;
  }

} ql[limit];

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

  cin >> n >> q;
  sn = sqrt(n);

  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    // a[i]--;
  }

  for (int i = 0; i < n; ++i) {
    cin >> ql[i].l >> ql[i].r;
    ql[i].in = i;
  }

  sort(ql, ql + q);

  int lp = ql[0].l, rp = ql[0].r;

  for (int i = ql[0].l; i <= ql[0].r; ++i) {
    cnt[a[i]]++;

    if (cnt[a[i]] == 1)
      ca++;
  }

  ans[ql[0].in] = ca;

  int d;

  for (int i = 1; i < q; ++i) {
    if (ql[i].l >= lp)
      d = 1;
    else
      d = -1;

    while (lp != ql[i].l) {
      if (d > 0)
        cnt[a[lp]]--;
      else
        cnt[a[lp - 1]]++;

      if (cnt[a[lp]] == 0 && d == 1)
        ca--;

      if (d == -1)
        if (cnt[a[lp - 1]] == 1)
          ca++;

      lp += d;
    }

    if (ql[i].r >= rp)
      d = 1;
    else
      d = -1;

    while (rp != ql[i].r) {
      if (d < 0)
        cnt[a[rp]]--;
      else
        cnt[a[rp + 1]]++;

      if (cnt[a[rp]] == 0 && d == -1)
        ca--;

      if (d == 1)
        if (cnt[a[rp + 1]] == 1)
          ca++;

      rp += d;
    }

    ans[ql[i].in] = ca;
  }

  for (int i = 0; i < q; ++i) {
    cout << ans[i] << "\\n";
  }

  return 0;
}