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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ff first
#define ss second
#define endl "\\n"
#define loop(i, s, e) for (int i = s; i < e; i++)
#define int long long
#define vi vector<int>
#define S(v) sort(v.begin(), v.end())
#define RS(v) sort(v.rbegin(), v.rend())
#define R(v) reverse(v.begin(), v.end())
#define mxpq(T) priority_queue<T>
#define mnpq(T) priority_queue<T, vector<T>, greater<T>>
#define prv(v)                                                                 \\
  for (auto &x : v)                                                            \\
    cout << x << " ";
#define piirv(v)                                                               \\
  for (auto &x : v)                                                            \\
    cout << x.ff << " " << x.ss << endl;
#define prvv(v)                                                                \\
  for (auto &x : v)                                                            \\
    for (auto &y : x.ss)                                                       \\
      cout << y << " ";
#define mp make_pair
#define pii pair<int, int>
#define mset(a, f) memset(a, f, sizeof(a))
#define pb push_back
#define inf INT_MAX
#define INF LLONG_MAX
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;

const int N = 2e6 + 6;
const int M = 1e9 + 7;
int A[N];
int frq[N];
int rid[N];
int n, q;

int bit[N + 1];
void update(int id, int val) {
  for (int i = id; i <= N; i += i & (-i))
    bit[i] += val;
}

int get(int id) {
  int sum = 0;
  for (int i = id; i > 0; i -= i & (-i))
    sum += bit[i];
  return sum;
}

int query(int r) { return get(r); }

void solve() {

  cin >> n >> q;

  mset(rid, -1);

  loop(i, 0, n) { cin >> A[i]; }

  vector<pair<int, pii>> vec;

  loop(i, 0, q) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    vec.pb({r, {l, i}});
  }

  S(vec);

  // loop(i,0,q)
  // {
  // 	cout << vec[i].ss.ss << " " << vec[i].ss.ff<< " " << vec[i].ff << endl;
  // }

  vector<int> ans(q);

  int j = 0;

  loop(i, 0, n) {
    if (rid[A[i]] != -1)
      update(rid[A[i]], -1);
    rid[A[i]] = i + 1;
    update(i + 1, 1);
    while (j < q && vec[j].ff == i) {
      ans[vec[j].ss.ss] = query(vec[j].ff + 1) - query(vec[j].ss.ff);
      j++;
    }
  }

  loop(i, 0, q) cout << ans[i] << endl;
}

int32_t main() {

  cout << fixed << setprecision(16);

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin >> t;
  while (t--)
    solve();
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

#define lop(i, s, e) for (int i = s; i < (int)e; i++)
#define rlop(i, e, s) for (int i = e; i >= s; i--)
#define prvec(v)                                                               \\
  lop(R15, 0, v.size()) cout << v[R15] << " ";                                 \\
  cout << endl;
#define prarr(arr, s1, e1)                                                     \\
  lop(R15, s1, e1) cout << arr[R15] << " ";                                    \\
  cout << endl;

#define endl '\\n'
#define ff first
#define ss second
#define pii pair<int, int>

#define pb push_back
#define vi vector<int>
#define vpii vector<pii>
#define all(v) v.begin(), v.end()
#define sz(re) (int)re.size()
#define mset(a, f) memset(a, f, sizeof(a))
#define M 1000000007
#define Think_Twice_Code_Once                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

bool J27 = 1;
#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)
#define rtn                                                                    \\
  if (!J27)                                                                    \\
    return;
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  rtn cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  rtn const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

inline void INP();
const int N = 5e5 + 7;
const long long INF = LLONG_MAX;
const int inf = INT_MAX;

int n, Q;
int arr[N + 2], cnt[N + 2];
class query {
public:
  int l, r, bi, id;
};
int answer = 0, cl = 0, cr = 0;

int getroot(int id) {
  int s = 0, e = id, ans = 0;
  while (s <= e) {
    int m = s + e >> 1;
    if (m * m <= id)
      ans = m, s = ++m;
    else
      e = --m;
  }
  return ans;
}

void add(int position) {
  cnt[arr[position]]++;
  if (cnt[arr[position]] == 1) {
    answer++;
  }
}

void remove(int position) {
  cnt[arr[position]]--;
  if (cnt[arr[position]] == 0) {
    answer--;
  }
}

void solve() {
  scanf("%d", &n);
  scanf("%d", &Q);
  lop(i, 1, n + 1) scanf("%d", arr + i);

  vector<query> vec(Q);
  int sq = getroot(n);

  lop(i, 0, Q) {
    int l, r;
    int x = scanf("%d%d", &l, &r);
    // bug(l,r,x);
    vec[i].l = l;
    vec[i].r = r;
    vec[i].id = i;
    vec[i].bi = l / sq;
  }

  sort(all(vec), [&](query q1, query q2) {
    if (q1.bi == q2.bi)
      return q1.r < q2.r;
    else
      return q1.bi < q2.bi;
  });

  int ans[Q];

  lop(i, 0, Q) {
    int L = vec[i].l, R = vec[i].r;
    // bug(L, R);

    while (cl < L) {
      remove(cl);
      cl++;
    }
    while (cl > L) {
      add(cl - 1);
      cl--;
    }
    while (cr <= R) {
      add(cr);
      cr++;
    }
    while (cr > R + 1) {
      remove(cr - 1);
      cr--;
    }
    ans[vec[i].id] = answer;
  }
  lop(i, 0, Q) cout << ans[i] << endl;
}

int32_t main() {

  J27 = 1;
  // INP();
  int t = 1;
  while (t--)
    solve();
}

inline void INP() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //