Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>
using namespace std;

long long BLOCK_SZ = 555;
int freqArr[10000000];
typedef struct query {
  long long start, end, index;
} query;
bool myComp(query a, query b);
void add(long long index, long long &curAns);
void remove(long long index, long long &curAns);

int main() {
  long long N, Q;
  scanf("%lld", &N);
  scanf("%lld", &Q);

  BLOCK_SZ = sqrt(N);
  vector<long long> arr(N);
  for (long long i = 0; i < N; i++)
    scanf("%lld", &arr[i]);

  vector<query> qq(Q);
  for (long long i = 0; i < Q; i++) {
    scanf("%lld%lld", &qq[i].start, &qq[i].end);
    qq[i].start--;
    qq[i].end--;
    qq[i].index = i;
  }
  sort(qq.begin(), qq.end(), myComp);

  vector<long long> queryAns(Q);
  long long curL = 0, curR = 0, curAns = 0;
  for (long long i = 0; i < Q; i++) {
    while (curL < qq[i].start) {
      remove(arr[curL], curAns);
      curL++;
    }
    while (curL > qq[i].start) {
      add(arr[curL - 1], curAns);
      curL--;
    }
    while (curR <= qq[i].end) {
      add(arr[curR], curAns);
      curR++;
    }
    while (curR > qq[i].end + 1) {
      remove(arr[curR - 1], curAns);
      curR--;
    }
    queryAns[qq[i].index] = curAns;
  }

  for (long long i = 0; i < Q; i++)
    printf("%lld\\n", queryAns[i]);
  return 0;
}

bool myComp(query a, query b) {
  if (a.start / BLOCK_SZ == b.start / BLOCK_SZ)
    return a.end < b.end;
  return a.start / BLOCK_SZ < b.start / BLOCK_SZ;
}

void add(long long index, long long &curAns) {
  freqArr[index]++;
  curAns += (freqArr[index] == 1);
}

void remove(long long index, long long &curAns) {
  freqArr[index]--;
  curAns -= (freqArr[index] == 0);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define MAX_SIZE 200001
#define debug(x) cout << #x << " " << x << "\\n"
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define file                                                                   \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);                                          \\
  freopen("error.txt", "w", stderr);
#define vi(vec, n) vector<ll> vec(n)
#define vio(vec, n, a) vector<ll> vec(n, a)
#define FOR(i, a, b, c)                                                        \\
  for (ll i = a; (c) ? i < (ll)b : i >= (ll)b; (c) ? i += 1 : i -= 1)
#define eb emplace_back
#define mod (ll)(1e9 + 7)
#define all(a, b) memset(a, b, sizeof a)

inline ll 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;
  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 {
  int l;
  int r;
  ll idx;
  ll ord;

  inline void calcOrder() { ord = gilbertOrder(l, r, 21, 0); }
};

Query Q[MAX_SIZE];

inline bool operator<(const Query &a, const Query &b) { return a.ord < b.ord; }

int arrForMos[MAX_SIZE];
int ans[MAX_SIZE];

int res = 0;
int counts[MAX_SIZE];
bool flags[MAX_SIZE];
void add(int index) {
  counts[arrForMos[index]]++;
  if (!flags[arrForMos[index]]) {
    flags[arrForMos[index]] = true;
    res++;
  }
}

void remove(int index) {
  counts[arrForMos[index]]--;
  if (!counts[arrForMos[index]] && flags[arrForMos[index]]) {
    flags[arrForMos[index]] = false;
    res--;
  }
}

void compress(int n) {
  int d[n];
  memcpy(d, arrForMos, 4 * n);
  sort(d, d + n);
  int k = unique(d, d + n) - d;
  FOR(i, 0, n, 1) arrForMos[i] = lower_bound(d, d + k, arrForMos[i]) - d;
}

int main() {
  // clock_t tStart = clock();
  // file;
  fast;

  all(flags, false);
  int n, q;
  cin >> n;
  cin >> q;

  FOR(i, 0, n, 1) { cin >> arrForMos[i]; }
  compress(n);
  FOR(i, 0, q, 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;

    // Q[i] = (Query){a,b,i};
    Q[i].l = a;
    Q[i].r = b;
    Q[i].idx = i;
    Q[i].calcOrder();
  }

  sort(Q, Q + q);

  int currentL = Q[0].l, currentR = Q[0].l - 1;
  FOR(i, 0, q, 1) {
    int l = Q[i].l, r = Q[i].r, index = Q[i].idx;
    while (currentL > l) {
      currentL--;
      add(currentL);
    }
    while (currentR > r) {
      remove(currentR);
      currentR--;
    }
    while (currentR < r) {
      currentR++;
      add(currentR);
    }
    while (currentL < l) {
      remove(currentL);
      currentL++;
    }
    ans[index] = res;
  }

  FOR(i, 0, q, 1) cout << ans[i] << "\\n";

  // printf("Time taken: %.6fs\\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
  return 0;
}