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

#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define pb push_back
#define mp make_pair
#define sz(a) (ll)(a).size()
#define endl "\\n"

const int MAXN = 500005;
const int LMAXN = 20;

inline int64_t gilbertOrder(int x, int y, int pow, int rotate) {
  if (pow == 0) {
    return 0;
  }
  int hpow = 1 << (pow - 1);
  int seg = (x < hpow) ? ((y < hpow) ? 0 : 3) : ((y < hpow) ? 1 : 2);
  seg = (seg + rotate) & 3;
  const int rotateDelta[4] = {3, 0, 0, 1};
  int nx = x & (x ^ hpow), ny = y & (y ^ hpow);
  int nrot = (rotate + rotateDelta[seg]) & 3;
  int64_t subSquareSize = int64_t(1) << (2 * pow - 2);
  int64_t ans = seg * subSquareSize;
  int64_t add = gilbertOrder(nx, ny, pow - 1, nrot);
  ans += (seg == 1 || seg == 2) ? add : (subSquareSize - add - 1);
  return ans;
}

struct Query {
  ll l, r, ind;
  int64_t ord;
  inline void calcOrder() { ord = gilbertOrder(l, r, LMAXN, 0); }
};
inline bool operator<(const Query &a, const Query &b) { return a.ord < b.ord; }

ll n, m, a[MAXN], ans[MAXN], curr, cc[MAXN];
vector<Query> q;

inline void add(ll x) {
  cc[x]++;
  if (cc[x] == 1)
    curr++;
}

inline void remove(ll x) {
  if (cc[x] == 1)
    curr--;
  cc[x]--;
}

int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m;
  q.resize(m);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++) {
    ll l, r;
    cin >> l >> r;
    q[i].l = l - 1;
    q[i].r = r - 1;
    q[i].ind = i;
    q[i].calcOrder();
  }
  sort(q.begin(), q.end());
  ll mo_left = 0, mo_right = -1;
  for (int i = 0; i < m; i++) {
    ll left = q[i].l;
    ll right = q[i].r;
    while (mo_right < right)
      add(a[++mo_right]);
    while (mo_right > right)
      remove(a[mo_right--]);
    while (mo_left < left)
      remove(a[mo_left++]);
    while (mo_left > left)
      add(a[--mo_left]);
    ans[q[i].ind] = curr;
  }
  for (int i = 0; i < m; i++)
    cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ll int
#define pb push_back
#define mp make_pair
#define sz(a) (ll)(a).size()
#define endl "\\n"

const int MAXN = 500005;
const int LMAXN = 20;

inline ll gilbertOrder(ll x, ll y, ll pow, ll rotate) {
  if (pow == 0) {
    return 0;
  }
  ll hpow = 1ll << (pow - 1);
  ll seg = (x < hpow) ? ((y < hpow) ? 0 : 3) : ((y < hpow) ? 1 : 2);
  seg = (seg + rotate) & 3;
  const ll rotateDelta[4] = {3, 0, 0, 1};
  ll nx = x & (x ^ hpow), ny = y & (y ^ hpow);
  ll nrot = (rotate + rotateDelta[seg]) & 3;
  ll subSquareSize = 1ll << (2 * pow - 2);
  ll ans = seg * subSquareSize;
  ll add = gilbertOrder(nx, ny, pow - 1, nrot);
  ans += (seg == 1 || seg == 2) ? add : (subSquareSize - add - 1);
  return ans;
}

struct Query {
  ll l, r, ind;
  ll ord;
  inline void calcOrder() { ord = gilbertOrder(l, r, LMAXN, 0); }
};
inline bool operator<(const Query &a, const Query &b) { return a.ord < b.ord; }

ll n, m, a[MAXN], ans[MAXN], curr, cc[MAXN];
vector<Query> q;

inline void add(ll x) {
  cc[x]++;
  if (cc[x] == 1)
    curr++;
}

inline void remove(ll x) {
  if (cc[x] == 1)
    curr--;
  cc[x]--;
}

int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m;
  q.resize(m);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++) {
    ll l, r;
    cin >> l >> r;
    q[i].l = l - 1;
    q[i].r = r - 1;
    q[i].ind = i;
    q[i].calcOrder();
  }
  sort(q.begin(), q.end());
  ll mo_left = 0, mo_right = -1;
  for (int i = 0; i < m; i++) {
    ll left = q[i].l;
    ll right = q[i].r;
    while (mo_right < right)
      add(a[++mo_right]);
    while (mo_right > right)
      remove(a[mo_right--]);
    while (mo_left < left)
      remove(a[mo_left++]);
    while (mo_left > left)
      add(a[--mo_left]);
    ans[q[i].ind] = curr;
  }
  for (int i = 0; i < m; i++)
    cout << ans[i] << endl;
  return 0;
}