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

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
// #pragma warning(disable : 4996)
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

#define int long long
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define len(s) (int)(s).size()
// #define endl '\\n'

inline void accell() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
}

template <class T> ostream &operator<<(ostream &out, const pair<int, int> &X) {
  out << X.first << ' ' << X.second << endl;
  return out;
}

template <class T> ostream &operator<<(ostream &out, const vector<T> &X) {
  for (const auto &it : X)
    out << it << ' ';
  return out;
}
template <class T> ostream &operator<<(ostream &out, const set<T> &X) {
  for (const auto &it : X)
    out << it << ' ';
  return out;
}
template <class T> void dbg(const T &X) {
  cerr << "DEBUG: "
       << ": ";
  cerr << X << endl;
}

const int MOD1 = 1e9 + 7;
const int N = 5e5 + 25;
const int MOD2 = 998244353;
const int inf = 1e13;
const int mx = (1 << 19) - 1;
const int mod = 1e9 + 7;

int fenv[N];

void add(int v, int x) {
  if (v < 0)
    return;
  for (int i = v; i < N; i |= (i + 1))
    fenv[i] += x;
}
int sum(int r) {
  int res = 0;
  for (int i = r; i >= 0; i = (i & (i + 1)) - 1)
    res += fenv[i];
  return res;
}
int get(int l, int r) { return sum(r) - sum(l - 1); }

signed main() {
  accell();
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
    --c[i];
  }
  vector<int> lst(n, -1);
  vector<int> cur(n, -1);
  for (int i = 0; i < n; ++i) {
    cur[i] = lst[c[i]];
    lst[c[i]] = i;
  }
  vector<pair<pair<int, int>, int>> v(q);
  for (int i = 0; i < q; ++i) {
    cin >> v[i].first.first >> v[i].first.second;
    v[i].first.first--, v[i].first.second--;
    v[i].second = i;
  }
  sort(all(v), [&](pair<pair<int, int>, int> x, pair<pair<int, int>, int> y) {
    return x.first.second < y.first.second;
  });
  int j = 0;
  vector<int> ans(q);
  for (int i = 0; i < n; ++i) {
    add(i, 1);
    add(cur[i], -1);
    while (j < len(v) && v[j].first.second == i) {
      ans[v[j].second] = get(v[j].first.first, v[j].first.second);
      ++j;
    }
  }
  for (int i = 0; i < q; ++i) {
    cout << ans[i] << '\\n';
  }
  return 0;
}


Error: 0

Fixed submission:

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
// #pragma warning(disable : 4996)
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

#define int long long
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define len(s) (int)(s).size()
// #define endl '\\n'

inline void accell() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
}

template <class T> ostream &operator<<(ostream &out, const pair<int, int> &X) {
  out << X.first << ' ' << X.second << endl;
  return out;
}

template <class T> ostream &operator<<(ostream &out, const vector<T> &X) {
  for (const auto &it : X)
    out << it << ' ';
  return out;
}
template <class T> ostream &operator<<(ostream &out, const set<T> &X) {
  for (const auto &it : X)
    out << it << ' ';
  return out;
}
template <class T> void dbg(const T &X) {
  cerr << "DEBUG: "
       << ": ";
  cerr << X << endl;
}

const int MOD1 = 1e9 + 7;
const int N = 3e5;
const int MOD2 = 998244353;
const int inf = 1e13;
const int mx = (1 << 19) - 1;
const int mod = 1e9 + 7;

int fenv[N];

void add(int v, int x) {
  if (v < 0)
    return;
  for (int i = v; i < N; i |= (i + 1))
    fenv[i] += x;
}
int sum(int r) {
  int res = 0;
  for (int i = r; i >= 0; i = (i & (i + 1)) - 1)
    res += fenv[i];
  return res;
}
int get(int l, int r) { return sum(r) - sum(l - 1); }

signed main() {
  accell();
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
    --c[i];
  }
  vector<int> lst(n, -1);
  vector<int> cur(n, -1);
  for (int i = 0; i < n; ++i) {
    cur[i] = lst[c[i]];
    lst[c[i]] = i;
  }
  vector<pair<pair<int, int>, int>> v(q);
  for (int i = 0; i < q; ++i) {
    cin >> v[i].first.first >> v[i].first.second;
    v[i].first.first--, v[i].first.second--;
    v[i].second = i;
  }
  sort(all(v), [&](pair<pair<int, int>, int> x, pair<pair<int, int>, int> y) {
    return x.first.second < y.first.second;
  });
  int j = 0;
  vector<int> ans(q);
  for (int i = 0; i < n; ++i) {
    add(i, 1);
    add(cur[i], -1);
    while (j < len(v) && v[j].first.second == i) {
      ans[v[j].second] = get(v[j].first.first, v[j].first.second);
      ++j;
    }
  }
  for (int i = 0; i < q; ++i) {
    cout << ans[i] << '\\n';
  }
  return 0;
}