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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> c(N);
  for (int i = 0; i < N; i++)
    // cin>>c[i],
    scanf("%d", &c[i]), c[i]--;
  vector<int> L(Q), R(Q);
  for (int i = 0; i < Q; i++)
    // cin>>L[i]>>R[i],
    scanf("%d%d", &L[i], &R[i]), L[i]--;

  // int N = 500000;
  // int Q = 500000;
  // vector<int> c(N);
  // for (int i=0; i<N; i++)
  //     c[i] = rand()%N;
  // vector<int> L(Q), R(Q);
  // for (int i=0; i<Q; i++)
  //{
  //     while (true)
  //     {
  //         L[i] = rand()%N;
  //         R[i] = rand()%N+1;
  //         if (L[i]<R[i])
  //             break;
  //     }
  // }

  int M = 0;
  while (M * M < N)
    M++;

  vector<int> QI(Q);
  for (int i = 0; i < Q; i++)
    QI[i] = i;

  sort(QI.begin(), QI.end(), [&](int x, int y) {
    if (L[x] / M < L[y] / M)
      return true;
    if (L[x] / M == L[y] / M && R[x] < R[y])
      return true;
    return false;
  });

  // for (int i=0; i<Q; i++)
  //     cout<<" "<<L[QI[i]]<<" "<<R[QI[i]]<<endl;

  vector<int> A(Q);
  vector<int> C(N);
  int a = 0;
  int l = 0;
  int r = 0;
  for (int i = 0; i < Q; i++) {
    int q = QI[i];
    while (l > L[q]) {
      l--;
      C[c[l]]++;
      if (C[c[l]] == 1)
        a++;
    }
    while (l < L[q]) {
      C[c[l]]--;
      if (C[c[l]] == 0)
        a--;
      l++;
    }
    while (r > R[q]) {
      r--;
      C[c[r]]--;
      if (C[c[r]] == 0)
        a--;
    }
    while (r < R[q]) {
      C[c[r]]++;
      if (C[c[r]] == 1)
        a++;
      r++;
    }
    A[q] = a;
  }
  for (int i = 0; i < Q; i++)
    // cout<<A[i]<<endl;
    printf("%d\\n", A[i]);
}


Error: 255

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> c(N);
  for (int i = 0; i < N; i++)
    cin >> c[i], c[i]--;
  vector<int> L(Q), R(Q);
  for (int i = 0; i < Q; i++)
    cin >> L[i] >> R[i], L[i]--;

  // int N = 500000;
  // int Q = 500000;
  // vector<int> c(N);
  // for (int i=0; i<N; i++)
  //     c[i] = rand()%N;
  // vector<int> L(Q), R(Q);
  // for (int i=0; i<Q; i++)
  //{
  //     while (true)
  //     {
  //         L[i] = rand()%N;
  //         R[i] = rand()%N+1;
  //         if (L[i]<R[i])
  //             break;
  //     }
  // }

  // int M = 0;
  // while (M*M<N)
  //     M++;
  const int M = 0x1000;

  vector<int> QI(Q);
  for (int i = 0; i < Q; i++)
    QI[i] = i;

  sort(QI.begin(), QI.end(), [&](int x, int y) {
    if (L[x] / M < L[y] / M)
      return true;
    if (L[x] / M == L[y] / M && R[x] < R[y])
      return true;
    return false;
  });

  return -1;

  // for (int i=0; i<Q; i++)
  //     cout<<" "<<L[QI[i]]<<" "<<R[QI[i]]<<endl;

  vector<int> A(Q);
  vector<int> C(N);
  int a = 0;
  int l = 0;
  int r = 0;
  for (int i = 0; i < Q; i++) {
    int q = QI[i];
    while (l > L[q]) {
      l--;
      C[c[l]]++;
      if (C[c[l]] == 1)
        a++;
    }
    while (l < L[q]) {
      C[c[l]]--;
      if (C[c[l]] == 0)
        a--;
      l++;
    }
    while (r > R[q]) {
      r--;
      C[c[r]]--;
      if (C[c[r]] == 0)
        a--;
    }
    while (r < R[q]) {
      C[c[r]]++;
      if (C[c[r]] == 1)
        a++;
      r++;
    }
    A[q] = a;
  }
  for (int i = 0; i < Q; i++)
    cout << A[i] << endl;
}