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

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

typedef long long ll;

ll ans[505050], t[505050], k[505050][3], v[505050];
set<pair<int, int>> s[1010];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // Älä käytä, jos tehtävä on interaktiivinen!
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; ++i)
    cin >> t[i];
  int nel = 1;
  while (nel * nel < (n + 5))
    nel++;
  for (int i = 1; i <= q; ++i) {
    int l, r;
    cin >> l >> r;
    k[i][1] = l;
    k[i][2] = r;
    s[l / nel].insert({r, i});
  }
  for (int i = 0; i <= nel; ++i) {
    for (int j = 0; j <= n; ++j)
      v[j] = 0;
    int col = 0;
    int a1 = -1;
    for (auto u : s[i]) {
      int j = u.second;
      // cout << i << " " << j << " " << k[j][1] << " " << k[j][2] << "\\n";
      if (k[j][1] / nel == k[j][2] / nel) {
        for (int a = k[j][1]; a <= k[j][2]; ++a) {
          if (v[t[a]] == 0)
            col++;
          v[t[a]]++;
        }
        ans[j] = col;
        for (int a = k[j][1]; a <= k[j][2]; ++a) {
          v[t[a]]--;
          if (v[t[a]] == 0)
            col--;
        }
      } else {
        if (a1 == -1) {
          for (int a = (i + 1) * nel; a <= k[j][2]; a++) {
            if (v[t[a]] == 0)
              col++;
            v[t[a]]++;
          }
          a1 = k[j][2];
        } else {
          for (int a = a1 + 1; a <= k[j][2]; a++) {
            if (v[t[a]] == 0)
              col++;
            v[t[a]]++;
          }
          a1 = k[j][2];
        }
        // cout << (i +1)*nel << " " << v[1] << " " << v[2] << " " << v[3] <<
        // "\\n";
        for (int a = k[j][1]; a < (i + 1) * nel; ++a) {
          if (v[t[a]] == 0)
            col++;
          v[t[a]]++;
        }
        ans[j] = col;
        for (int a = k[j][1]; a < (i + 1) * nel; ++a) {
          v[t[a]]--;
          if (v[t[a]] == 0)
            col--;
        }
      }
    }
  }
  for (int i = 1; i <= q; ++i)
    cout << ans[i] << "\\n";
}


Error: 0

Fixed submission:

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

typedef long long ll;

ll ans[202020], t[202020], k[202020][3], v[202020];
set<pair<int, int>> s[1010];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // Älä käytä, jos tehtävä on interaktiivinen!
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; ++i)
    cin >> t[i];
  int nel = 1;
  while (nel * nel < (n + 5))
    nel++;
  for (int i = 1; i <= q; ++i) {
    int l, r;
    cin >> l >> r;
    k[i][1] = l;
    k[i][2] = r;
    s[l / nel].insert({r, i});
  }
  for (int i = 0; i <= nel; ++i) {
    for (int j = 0; j <= n; ++j)
      v[j] = 0;
    int col = 0;
    int a1 = -1;
    for (auto u : s[i]) {
      int j = u.second;
      // cout << i << " " << j << " " << k[j][1] << " " << k[j][2] << "\\n";
      if (k[j][1] / nel == k[j][2] / nel) {
        for (int a = k[j][1]; a <= k[j][2]; ++a) {
          if (v[t[a]] == 0)
            col++;
          v[t[a]]++;
        }
        ans[j] = col;
        for (int a = k[j][1]; a <= k[j][2]; ++a) {
          v[t[a]]--;
          if (v[t[a]] == 0)
            col--;
        }
      } else {
        if (a1 == -1) {
          for (int a = (i + 1) * nel; a <= k[j][2]; a++) {
            if (v[t[a]] == 0)
              col++;
            v[t[a]]++;
          }
          a1 = k[j][2];
        } else {
          a1++;
          while (a1 <= k[j][2]) {
            if (v[t[a1]] == 0)
              col++;
            v[t[a1]]++;
            a1++;
          }
        }
        // cout << (i +1)*nel << " " << v[1] << " " << v[2] << " " << v[3] <<
        // "\\n";
        for (int a = k[j][1]; a < (i + 1) * nel; ++a) {
          if (v[t[a]] == 0)
            col++;
          v[t[a]]++;
        }
        ans[j] = col;
        for (int a = k[j][1]; a < (i + 1) * nel; ++a) {
          v[t[a]]--;
          if (v[t[a]] == 0)
            col--;
        }
      }
    }
  }
  for (int i = 1; i <= q; ++i)
    cout << ans[i] << "\\n";
}