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

// Optional FAST

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2,tune=native")

// Required Libraries

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>

// Required namespaces

using namespace std;
using namespace __gnu_pbds;

// Required defines

#define endl '\\n'

#define READ(X) cin >> X;
#define READV(X)                                                               \\
  long long X;                                                                 \\
  cin >> X;
#define READAR(A, N)                                                           \\
  long long A[N];                                                              \\
  for (long long i = 0; i < N; i++) {                                          \\
    cin >> A[i];                                                               \\
  }
#define rz(A, N) A.resize(N);
#define sz(X) (long long)(X.size())
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define FORI(a, b, c) for (long long a = b; a < c; a++)
#define FORD(a, b, c) for (long long a = b; a > c; a--)

// Required typedefs

typedef tree<long long, null_type, greater<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef tree<long long, null_type, greater<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set1;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

// Required Constants

const long long inf = (long long)1e18;
const long long MOD = (long long)(1e9 + 7);
const long long INIT = (long long)(1e6 + 1);
const long double PI = 3.14159265358979;

// Required random number generators

// mt19937 gen_rand_int(chrono::steady_clock::now().time_since_epoch().count());
// mt19937_64
// gen_rand_ll(chrono::steady_clock::now().time_since_epoch().count());

// Required Functions

ll power(ll x, ll y) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2) % MOD;
  p = (p * p) % MOD;

  return (y % 2 == 0) ? p : (x * p) % MOD;
}
ll modInverse(ll a) { return power(a, MOD - 2); }

// Work

int main() {
#ifndef ONLINE_JUDGE
  if (fopen("INPUT.txt", "r")) {
    freopen("INPUT.txt", "r", stdin);
    // freopen ("OUTPUT.txt" , "w" , stdout);
  }
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  clock_t clk;
  clk = clock();
  //-----------------------------------------------------------------------------------------------------------//

  READV(N);
  READV(Q);
  vector<ll> A(N + 1);
  FORI(i, 1, N + 1) { cin >> A[i]; }
  ll C[N + 1];
  memset(C, -1, sizeof(C));
  vector<vector<pll>> Z(N + 1);
  vector<ll> ans(Q);
  ordered_set calc;
  FORI(i, 0, Q) {
    READV(l);
    READV(r);
    Z[r].pb({l, i});
  }
  FORI(i, 1, N + 1) {
    if (C[A[i]] != -1) {
      calc.erase(calc.find(C[A[i]]));
    }
    C[A[i]] = i;
    calc.insert(C[A[i]]);
    for (auto v : Z[i]) {
      ans[v.se] = calc.order_of_key(v.fi - 1);
    }
  }
  for (auto x : ans) {
    cout << x << endl;
  }

  //-----------------------------------------------------------------------------------------------------------//

  clk = clock() - clk;
  cerr << fixed << setprecision(6) << "Time: " << ((double)clk) / CLOCKS_PER_SEC
       << endl;

  return 0;
}

Error: TLE

Fixed submission:

// Optional FAST

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2,tune=native")

// Required Libraries

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>

// Required namespaces

using namespace std;
using namespace __gnu_pbds;

// Required defines

#define endl '\\n'

#define READ(X) cin >> X;
#define READV(X)                                                               \\
  long long X;                                                                 \\
  cin >> X;
#define READAR(A, N)                                                           \\
  long long A[N];                                                              \\
  for (long long i = 0; i < N; i++) {                                          \\
    cin >> A[i];                                                               \\
  }
#define rz(A, N) A.resize(N);
#define sz(X) (long long)(X.size())
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define FORI(a, b, c) for (long long a = b; a < c; a++)
#define FORD(a, b, c) for (long long a = b; a > c; a--)

// Required typedefs

typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef tree<long long, null_type, greater<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set1;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

// Required Constants

const long long inf = (long long)1e18;
const long long MOD = (long long)(1e9 + 7);
const long long INIT = (long long)(1e6 + 1);
const long double PI = 3.14159265358979;

// Required random number generators

// mt19937 gen_rand_int(chrono::steady_clock::now().time_since_epoch().count());
// mt19937_64
// gen_rand_ll(chrono::steady_clock::now().time_since_epoch().count());

// Required Functions

ll power(ll x, ll y) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2) % MOD;
  p = (p * p) % MOD;

  return (y % 2 == 0) ? p : (x * p) % MOD;
}
ll modInverse(ll a) { return power(a, MOD - 2); }

// Work
int N;
vector<set<int>> SegT;
vector<int> A;
void build(int v, int l, int r) {
  if (l == r) {
    SegT[v].insert(A[l]);
  } else {
    int mid = (l + r) / 2;
    build(2 * v, l, mid);
    build(2 * v + 1, mid + 1, r);
    SegT[v].insert(SegT[2 * v].begin(), SegT[2 * v].end());
    SegT[v].insert(SegT[2 * v + 1].begin(), SegT[2 * v + 1].end());
  }
}
set<int> query_set(int v, int l, int r, int tl, int tr) {
  if (l <= tl && tr <= r) {
    return SegT[v];
  } else if (tr < l || r < tl) {
    set<int> result;
    return result;
  } else {
    int mid = (tl + tr) / 2;
    set<int> result = query_set(2 * v, l, r, tl, mid);
    set<int> result1 = query_set(2 * v + 1, l, r, mid + 1, tr);
    if (sz(result1) > sz(result)) {
      result1.insert(result.begin(), result.end());
      return result1;
    } else {
      result.insert(result1.begin(), result1.end());
      return result;
    }
  }
}
int query(int l, int r) { return (query_set(1, l, r, 1, N)).size(); }
int main() {
#ifndef ONLINE_JUDGE
  if (fopen("INPUT.txt", "r")) {
    freopen("INPUT.txt", "r", stdin);
    // freopen ("OUTPUT.txt" , "w" , stdout);
  }
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  clock_t clk;
  clk = clock();
  //-----------------------------------------------------------------------------------------------------------//

  READ(N);
  READV(Q);
  SegT.resize(4 * N + 1);
  A.resize(N + 1);
  FORI(i, 1, N + 1) { cin >> A[i]; }
  build(1, 1, N);
  while (Q--) {
    READV(l);
    READV(r);
    cout << query(l, r) << endl;
  }

  //-----------------------------------------------------------------------------------------------------------//

  clk = clock() - clk;
  cerr << fixed << setprecision(6) << "Time: " << ((double)clk) / CLOCKS_PER_SEC
       << endl;

  return 0;
}