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

#include <bits/stdc++.h>
using namespace std;
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, set<T> S) {
  os << "{ ";
  for (auto s : S)
    os << s << " ";
  return os << "}";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) {
  os << "{ ";
  for (auto m : M)
    os << "(" << m.first << ":" << m.second << ")";
  return os << "}";
}
#define cerr cout
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...) 1
#endif
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update> find_by_order(k)  returns
// iterator to kth element starting from 0; order_of_key(k) returns count of
// elements strictly smaller than k;
// For multiset use less_equal operator but it does support erase operations for
// multiset
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define I insert
#define pb push_back
#define F first
#define S second
// #define endl "\\n"
#define all(v) (v).begin(), (v).end()
#define For(i, n) for (int i = 0; i < (int)n; ++i)
#define Rev(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define Rep(i, n) for (int i = 1; i <= (int)n; ++i)
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef vector<pii> vpi;
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define mp make_pair
// inline void modadd(int &a,int &b){a+=b;if(a>=mod)a-=mod;}
const int N = 5e5 + 100;

int tab[N];
void upt(int x, int val) {
  x = N - 1 - x;
  while (x < N) {
    tab[x] += val;
    x += x & -x;
  }
}
int query(int x) {
  x = N - 1 - x;
  int res = 0;
  while (x) {
    res += tab[x];
    x -= x & -x;
  }
  return res;
}
int ans[N];

int rht[N];
int A[N];
int L[N], R[N];
vi a[N], b[N];

int main() {
  fio;
  cout << fixed << setprecision(25);
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; ++i) {
    cin >> A[i];
  }
  for (int i = n; i >= 1; --i) {
    if (rht[A[i]] == 0)
      rht[A[i]] = N - 2;
    int nxt = A[i];
    A[i] = rht[nxt];
    rht[nxt] = i;
  }
  for (int i = 1; i <= q; ++i) {
    cin >> L[i] >> R[i];
    a[L[i] - 1].pb(i);
    b[R[i]].pb(i);
  }

  for (int i = 1; i <= n; ++i) {
    // cout << A[i] << ' ' << a[i] << ' ' << b[i] << endl;
    upt(A[i], 1);
    for (auto it : a[i]) {
      ans[it] -= query(R[it] + 1);
    }
    for (auto it : b[i]) {
      ans[it] += query(i + 1);
    }
  }
  for (int i = 1; i <= q; ++i)
    cout << ans[i] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, set<T> S) {
  os << "{ ";
  for (auto s : S)
    os << s << " ";
  return os << "}";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) {
  os << "{ ";
  for (auto m : M)
    os << "(" << m.first << ":" << m.second << ")";
  return os << "}";
}
#define cerr cout
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...) 1
#endif
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update> find_by_order(k)  returns
// iterator to kth element starting from 0; order_of_key(k) returns count of
// elements strictly smaller than k;
// For multiset use less_equal operator but it does support erase operations for
// multiset
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define I insert
#define pb push_back
#define F first
#define S second
// #define endl "\\n"
#define all(v) (v).begin(), (v).end()
#define For(i, n) for (int i = 0; i < (int)n; ++i)
#define Rev(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define Rep(i, n) for (int i = 1; i <= (int)n; ++i)
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef vector<pii> vpi;
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define mp make_pair
// inline void modadd(int &a,int &b){a+=b;if(a>=mod)a-=mod;}
const int N = 5e5 + 100;
const int B = 500;
int A[N];

int freq[N];

bool cmp(ppi &x, ppi &y) {
  if (x.F.F / B != y.F.F / B)
    return x.F.F < y.F.F;
  else
    return x.F.S < y.F.S;
}
int ans[N];
int curr;
pair<pii, int> query[N];

void sub(int c) {
  freq[c]--;
  if (freq[c] == 0)
    --curr;
}
void add(int c) {
  freq[c]++;
  if (freq[c] == 1)
    ++curr;
}

int main() {
  fio;
  cout << fixed << setprecision(25);
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; ++i)
    cin >> A[i];
  for (int i = 1; i <= q; ++i) {
    cin >> query[i].F.F >> query[i].F.S;
    query[i].S = i;
  }
  sort(query + 1, query + q + 1, cmp);
  int l = 1, r = 0;
  for (int i = 1; i <= q; ++i) {
    // cout << l << ' ' << r << endl;
    // cout << query[i] << endl;
    while (query[i].F.F < l) {
      --l;
      add(A[l]);
    }
    while (query[i].F.S > r) {
      ++r;
      add(A[r]);
    }

    while (query[i].F.F > l) {
      sub(A[l]);
      ++l;
    }
    while (query[i].F.S < r) {
      sub(A[r]);
      --r;
    }
    ans[query[i].S] = curr;
  }
  for (int i = 1; i <= q; ++i)
    cout << ans[i] << '\\n';
}