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

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

typedef int_fast32_t int32;
typedef int_fast64_t int64;

const int32 inf = 1e9 + 7;
const int32 MOD = 1000000007;
const int64 llinf = 1e18;

#define YES(n) cout << ((n) ? "YES\\n" : "NO\\n")
#define Yes(n) cout << ((n) ? "Yes\\n" : "No\\n")
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE\\n" : "IMPOSSIBLE\\n")
#define ANS(n) cout << (n) << "\\n"
#define REP(i, n) for (int64 i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define FORR(i, a, b) for (int64 i = (a); i >= (b); i--)
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
#define fi first
#define se second
#define pb(a) push_back(a)
typedef pair<int32, int32> pii;
typedef pair<int64, int64> pll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> struct FenwickTree {
  vector<T> fenwick;
  int n;

  FenwickTree(int n) : fenwick(vector<T>(n + 1, 0)), n(n) {}

  T sum(int i) {
    if (!i)
      return 0;
    return fenwick[i] + sum(i - (i & -i));
  }

  T sum(int i, int j) {
    // half-open Terval
    return sum(j - 1) - sum(i - 1);
  }

  void add(int i, T x) {
    if (i > n)
      return;
    fenwick[i] += x;
    add(i + (i & -i), x);
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 n, q;
  cin >> n >> q;
  vector<int32> c(n);
  REP(i, n) cin >> c[i];
  vector<pair<pii, int32>> lr(q);
  REP(i, q) {
    cin >> lr[i].fi.se >> lr[i].fi.fi;
    lr[i].se = i;
  }
  sort(all(lr));
  REP(i, q) swap(lr[i].fi.fi, lr[i].fi.se);

  vector<int32> pre(n + 1, -1);
  FenwickTree<int32> ft(n + 1);
  vector<int32> ans(q);

  // int32 cur = 1;
  // REP(i,q){
  //     auto [l,r] = lr[i].fi;
  //     while(cur <= r){
  //         if(pre[c[cur-1]] != -1)
  //             ft.add(pre[c[cur-1]],1);
  //         pre[c[cur-1]] = cur;
  //         ++cur;
  //     }
  //     ans[lr[i].se] = r - l + 1 - ft.sum(l,r);
  // }

  // int32 qi = 0;
  // FOR(x,1,n+1){
  //     if(pre[c[x-1]] != -1){
  //         ft.add(pre[c[x-1]],1);
  //     }
  //     pre[c[x-1]] = x;
  //     while(qi < q && lr[qi].fi.se == x){
  //         auto [l,r] = lr[qi].fi;
  //         ans[lr[qi].se] = r - l + 1 - ft.sum(l,r);
  //         ++qi;
  //     }
  // }

  int32 cur = 1;
  REP(i, q) {
    auto [l, r] = lr[i].fi;
    while (cur <= r) {
      if (pre[c[cur - 1]] != -1)
        ft.add(pre[c[cur - 1]], -1);
      ft.add(cur, 1);
      pre[c[cur - 1]] = cur;
      ++cur;
    }
    ans[lr[i].se] = ft.sum(l, r + 1);
  }

  REP(i, q) ANS(ans[i]);
  return 0;
}

Error: TLE

Fixed submission:

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

typedef int_fast32_t int32;
typedef int_fast64_t int64;

const int32 inf = 1e9 + 7;
const int32 MOD = 1000000007;
const int64 llinf = 1e18;

#define YES(n) cout << ((n) ? "YES\\n" : "NO\\n")
#define Yes(n) cout << ((n) ? "Yes\\n" : "No\\n")
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE\\n" : "IMPOSSIBLE\\n")
#define ANS(n) cout << (n) << "\\n"
#define REP(i, n) for (int64 i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define FORR(i, a, b) for (int64 i = (a); i >= (b); i--)
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
#define fi first
#define se second
#define pb(a) push_back(a)
typedef pair<int32, int32> pii;
typedef pair<int64, int64> pll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> struct FenwickTree {
  vector<T> fenwick;
  int n;

  FenwickTree(int n) : fenwick(vector<T>(n + 1, 0)), n(n) {}

  T sum(int i) {
    if (!i)
      return 0;
    return fenwick[i] + sum(i - (i & -i));
  }

  T sum(int i, int j) {
    // half-open Terval
    return sum(j - 1) - sum(i - 1);
  }

  void add(int i, T x) {
    if (i > n)
      return;
    fenwick[i] += x;
    add(i + (i & -i), x);
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 n, q;
  cin >> n >> q;
  vector<int32> c(n);
  REP(i, n) cin >> c[i];
  vector<pair<pii, int32>> lr(q);
  REP(i, q) {
    cin >> lr[i].fi.se >> lr[i].fi.fi;
    lr[i].se = i;
  }
  sort(all(lr));
  REP(i, q) swap(lr[i].fi.fi, lr[i].fi.se);

  vector<int32> pre(n, -1);
  FenwickTree<int32> ft(n);
  // int32 cur = 1;
  vector<int32> ans(q);
  // REP(i,q){
  //     auto [l,r] = lr[i].fi;
  //     while(cur <= r){
  //         if(pre[c[cur-1]] != -1)
  //             ft.add(pre[c[cur-1]],1);
  //         pre[c[cur-1]] = cur;
  //         ++cur;
  //     }
  //     ans[lr[i].se] = r - l + 1 - ft.sum(l,r);
  // }
  int32 qi = 0;
  FOR(x, 1, n + 1) {
    if (pre[c[x - 1]] != -1) {
      ft.add(pre[c[x - 1]], 1);
    }
    pre[c[x - 1]] = x;
    while (qi < q && lr[qi].fi.se == x) {
      auto [l, r] = lr[qi].fi;
      ans[lr[qi].se] = r - l + 1 - ft.sum(l, r);
      ++qi;
    }
  }
  REP(i, q) ANS(ans[i]);
  return 0;
}