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;

// template <typename T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>;

typedef long long ll;
typedef long double ld;
typedef pair<long long, long long> ii;
// typedef complex<long double> point;

ll n;
const ll s = 700;

struct querie {
  ll l, r, id;
  bool operator<(const querie &rhs) const {
    if (l / s != rhs.l / s)
      return make_pair(l, r) < make_pair(rhs.l, rhs.r);
    return (l / s & 1) ? (r < rhs.r) : (r > rhs.r);
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  ll q;
  cin >> q;
  vector<ll> arr(n);
  for (int z = 0; z < n; z++) {
    cin >> arr[z];
  }

  vector<querie> que(q);
  for (int z = 0; z < q; z++) {
    cin >> que[z].l >> que[z].r;
    que[z].l--;
    que[z].r--;
    que[z].id = z;
  }
  sort(que.begin(), que.end());
  ll l = 0;
  ll r = 0;
  ll count = 1;
  vector<ll> mapp(n + 1);
  mapp[arr[0]]++;
  vector<ll> ans(q);
  for (int z = 0; z < q; z++) {
    while (que[z].l < l) {
      l--;
      mapp[arr[l]]++;
      if (mapp[arr[l]] == 1)
        count++;
    }
    while (que[z].l > l) {
      mapp[arr[l]]--;
      if (mapp[arr[l]] == 0)
        count--;
      l++;
    }
    while (que[z].r < r) {
      mapp[arr[r]]--;
      if (mapp[arr[r]] == 0)
        count--;
      r--;
    }
    while (que[z].r > r) {
      r++;
      mapp[arr[r]]++;
      if (mapp[arr[r]] == 1)
        count++;
    }
    ans[que[z].id] = count;
  }
  for (ll i : ans) {
    cout << i << endl;
  }
}


Error: TLE

Fixed submission:

#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;

// template <typename T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>;

typedef long long ll;
typedef long double ld;
typedef pair<long long, long long> ii;
// typedef complex<long double> point;

ll n;
const ll s = 700;

struct querie {
  ll l, r, id;
  bool operator<(const querie &rhs) const {
    return make_pair(l / s, r) < make_pair(rhs.l / s, rhs.r);
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  ll q;
  cin >> q;
  vector<ll> arr(n);
  for (int z = 0; z < n; z++) {
    cin >> arr[z];
  }

  vector<querie> que(q);
  for (int z = 0; z < q; z++) {
    cin >> que[z].l >> que[z].r;
    que[z].l--;
    que[z].r--;
    que[z].id = z;
  }
  sort(que.begin(), que.end());
  ll l = 0;
  ll r = 0;
  ll count = 1;
  vector<ll> mapp(n + 1);
  mapp[arr[0]]++;
  vector<ll> ans(q);
  for (int z = 0; z < q; z++) {
    while (que[z].l < l) {
      l--;
      mapp[arr[l]]++;
      if (mapp[arr[l]] == 1)
        count++;
    }
    while (que[z].l > l) {
      mapp[arr[l]]--;
      if (mapp[arr[l]] == 0)
        count--;
      l++;
    }
    while (que[z].r < r) {
      mapp[arr[r]]--;
      if (mapp[arr[r]] == 0)
        count--;
      r--;
    }
    while (que[z].r > r) {
      r++;
      mapp[arr[r]]++;
      if (mapp[arr[r]] == 1)
        count++;
    }
    ans[que[z].id] = count;
  }
  for (ll i : ans) {
    cout << i << endl;
  }
}