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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/rope>

#define ll long long
#define ld long double
#define fi first
#define se second
#define sz(x) x.size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define rep(i, x, n) for (ll i = x; i < n; i++)
#define fastio()                                                               \\
  ios_base::sync_with_stdio(NULL);                                             \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<pll> vll;

inline void EXECUTE_TIME() {
  cout << endl
       << setprecision(10) << fixed
       << "CPU Time: " << clock() / double(CLOCKS_PER_SEC) << " sec(s)\\n";
}

template <class T> inline T RD(T &a) {
  T x;
  cin >> x;
  return a = x;
}
template <class T> inline T gcd(T a, T b) {
  if (b)
    return gcd(b, a % b);
  else
    return a;
}
template <class T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef trie<string, null_type, trie_string_access_traits<>, pat_trie_tag,
             trie_prefix_search_node_update>
    pref_trie;

const int N = 5e5 + 10;
const int bl = 700;
int n, q;

int a[N];

int cnt[N];
int res = 0;

int ans[N];

struct queries {
  int l, r, id;
};

bool cmp(queries p, queries q) {
  if (p.l / bl != q.l / bl)
    return p.l < q.l;
  return (p.l / bl & 1) ? (p.r < q.r) : (p.r > q.r);
}

queries query[N];

void add(int i) {
  cnt[a[i]]++;
  if (cnt[a[i]] == 1)
    res++;
}

void sub(int i) {
  cnt[a[i]]--;
  if (cnt[a[i]] == 0)
    res--;
}

void solve() {
  sort(query, query + q, cmp);
  int cur_l = 0, cur_r = -1;
  queries x;
  for (int i = 0; i < q; i++) {
    x = query[i];
    while (cur_l > x.l) {
      cur_l--;
      add(cur_l);
    }
    while (cur_r < x.r) {
      cur_r++;
      add(cur_r);
    }
    while (cur_l < x.l) {
      sub(cur_l);
      cur_l++;
    }
    while (cur_r > x.r) {
      sub(cur_r);
      cur_r--;
    }
    ans[x.id] = res;
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}

signed main() {
  //    freopen("test.inp","r",stdin);
  //    freopen("test.out","w",stdout);
  fastio();

  cin >> n >> q;
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, q) {
    cin >> query[i].l >> query[i].r;
    query[i].l--, query[i].r--;
    query[i].id = i;
  }
  solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/rope>

#define ll long long
#define ld long double
#define fi first
#define se second
#define sz(x) x.size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define rep(i, x, n) for (ll i = x; i < n; i++)
#define fastio()                                                               \\
  ios_base::sync_with_stdio(NULL);                                             \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<pll> vll;

inline void EXECUTE_TIME() {
  cout << endl
       << setprecision(10) << fixed
       << "CPU Time: " << clock() / double(CLOCKS_PER_SEC) << " sec(s)\\n";
}

template <class T> inline T RD(T &a) {
  T x;
  cin >> x;
  return a = x;
}
template <class T> inline T gcd(T a, T b) {
  if (b)
    return gcd(b, a % b);
  else
    return a;
}
template <class T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef trie<string, null_type, trie_string_access_traits<>, pat_trie_tag,
             trie_prefix_search_node_update>
    pref_trie;

const int N = 5e5 + 10;
const int bl = 700;
int n, q;
vector<int> a;

int cnt[N];
int res = 0;

int ans[N];

struct queries {
  int l, r, id;

  bool operator<(queries a) const { return mp(l / bl, r) < mp(a.l / bl, a.r); }
};

vector<queries> query;

void add(int i) {
  cnt[a[i]]++;
  if (cnt[a[i]] == 1)
    res++;
}

void sub(int i) {
  cnt[a[i]]--;
  if (cnt[a[i]] == 0)
    res--;
}

void solve() {
  sort(all(query));
  int cur_l = 0, cur_r = -1;
  for (queries x : query) {
    while (cur_l > x.l) {
      cur_l--;
      add(cur_l);
    }
    while (cur_r < x.r) {
      cur_r++;
      add(cur_r);
    }
    while (cur_l < x.l) {
      sub(cur_l);
      cur_l++;
    }
    while (cur_r > x.r) {
      sub(cur_r);
      cur_r--;
    }
    ans[x.id] = res;
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}

signed main() {
  //    freopen("test.inp","r",stdin);
  //    freopen("ROAD.out","w",stdout);
  fastio();

  cin >> n >> q;
  a.assign(n, 0);
  query.assign(q, {});
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, q) {
    cin >> query[i].l >> query[i].r;
    query[i].l--, query[i].r--;
    query[i].id = i;
  }
  solve();
}