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;

// 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 //

#define int long long
#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 all(v) v.begin(), v.end()

// 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 //

const int N = 3e6 + 7, M = 1000000007, inf = INT_MAX;
const long long INF = LLONG_MAX;

int temp[N];

int a[N], cnt[N], n, q;

struct Q {
  int id;
  int L, R;
};

vector<vector<Q>> tre;

vector<int> ans;

void AddQ(int L, int R) {
  Q q;
  q.L = L;
  q.R = R;

  q.id = (int)ans.size();
  ans.push_back(0);
  tre[R].push_back(q);
}

void work() {
  lop(i, 0, n) {
    if (temp[a[i]] != -1)
      cnt[temp[a[i]]]--;
    temp[a[i]] = i;
    cnt[temp[a[i]]]++;

    for (int query = 0; query < (int)tre[i].size(); ++query) {
      Q q = tre[i][query];
      int sum = 0;

      for (int j = q.L; j <= q.R; j++)
        sum += cnt[j];
      ans[q.id] = sum;
    }
  }
  lop(i, 0, q) cout << ans[i] << endl;
}

void work1() {
  cin >> n >> q;
  tre.resize(n);
  lop(i, 0, n) cin >> a[i];
  lop(i, 0, q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    AddQ(l, r);
  }
}

int32_t main() {
  memset(temp, -1, sizeof(temp));

  work1();
  work();

  return 0;
}