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

#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL)
typedef long long ll;
typedef long double ld;
#define trace(x) cerr << #x << ": " << x << endl;
#define rep(i, a) for (ll i = 0; i < a; i++)
#define pb push_back
#define vl vector<ll>
const ll N = 5 * 1e5 + 5;
vector<ll> tree(4 * N);
void update(ll cur, ll l, ll r, ll pos) {
  if (l > pos || r < pos)
    return;
  if (l == r && r == pos) {
    tree[cur] = 1;
    return;
  }
  ll m = (r + l) / 2;
  update(2 * cur, l, m, pos);
  update(2 * cur + 1, m + 1, r, pos);
  tree[cur] = tree[2 * cur] + tree[2 * cur + 1];
}
ll sum(ll i, ll l, ll r, ll x, ll y) {
  if (l > y || r < x)
    return 0;
  if (x <= l && r <= y)
    return tree[i];
  ll m = (r + l) / 2;
  return sum(2 * i, l, m, x, y) + sum(2 * i + 1, m + 1, r, x, y);
}
bool comp(pair<pair<ll, ll>, ll> a, pair<pair<ll, ll>, ll> b) {
  return a.first.second < b.first.second;
}
int main() {
  fast;
  ll n;
  cin >> n;
  ll q;
  cin >> q;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  vector<ll> nxt(n);
  map<ll, ll> occ;
  vector<pair<ll, ll>> toup;
  for (ll i = n - 1; i >= 0; i--) {
    if (occ.count(a[i]) == 0)
      nxt[i] = n + 1;
    else
      nxt[i] = occ[a[i]];
    occ[a[i]] = i;
    toup.push_back({nxt[i], i});
  }
  vector<pair<pair<ll, ll>, ll>> query;
  for (ll i = 0; i < q; i++) {
    ll l, r;
    cin >> l >> r;
    l--;
    r--;
    query.push_back({{l, r}, i});
  }
  sort(query.begin(), query.end(), comp);
  reverse(query.begin(), query.end());
  sort(toup.begin(), toup.end());
  reverse(toup.begin(), toup.end());
  vector<ll> ans(q);
  ll j = 0;
  for (ll i = 0; i < q; i++) {
    while (j < n && toup[j].first > query[i].first.second)
      update(1, 0, n - 1, toup[j++].second);
    ans[query[i].second] =
        sum(1, 0, n - 1, query[i].first.first, query[i].first.second);
  }
  for (ll i = 0; i < q; i++)
    cout << ans[i] << "\\n";
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL)
typedef long long ll;
typedef long double ld;
#define trace(x) cerr << #x << ": " << x << endl;
#define rep(i, a) for (ll i = 0; i < a; i++)
#define pb push_back
#define vl vector<ll>
const ll N = 1e5 + 5;
vector<ll> tree(4 * N);
void update(ll cur, ll l, ll r, ll pos) {
  if (l > pos || r < pos)
    return;
  if (l == r && r == pos) {
    tree[cur] = 1;
    return;
  }
  ll m = (r + l) / 2;
  update(2 * cur, l, m, pos);
  update(2 * cur + 1, m + 1, r, pos);
  tree[cur] = tree[2 * cur] + tree[2 * cur + 1];
}
ll sum(ll i, ll l, ll r, ll x, ll y) {
  if (l > y || r < x)
    return 0;
  if (x <= l && r <= y)
    return tree[i];
  ll m = (r + l) / 2;
  return sum(2 * i, l, m, x, y) + sum(2 * i + 1, m + 1, r, x, y);
}
bool comp(pair<pair<ll, ll>, ll> a, pair<pair<ll, ll>, ll> b) {
  return a.first.second < b.first.second;
}
int main() {
  fast;
  ll n;
  cin >> n;
  ll q;
  cin >> q;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  vector<ll> nxt(n);
  map<ll, ll> occ;
  vector<pair<ll, ll>> toup;
  for (ll i = n - 1; i >= 0; i--) {
    if (occ.count(a[i]) == 0)
      nxt[i] = n + 1;
    else
      nxt[i] = occ[a[i]];
    occ[a[i]] = i;
    toup.push_back({nxt[i], i});
  }
  vector<pair<pair<ll, ll>, ll>> query;
  for (ll i = 0; i < q; i++) {
    ll l, r;
    cin >> l >> r;
    l--;
    r--;
    query.push_back({{l, r}, i});
  }
  sort(query.begin(), query.end(), comp);
  reverse(query.begin(), query.end());
  sort(toup.begin(), toup.end());
  reverse(toup.begin(), toup.end());
  vector<ll> ans(q);
  ll j = 0;
  for (ll i = 0; i < q; i++) {
    while (j < n && toup[j].first > query[i].first.second)
      update(1, 0, n - 1, toup[j++].second);
    ans[query[i].second] =
        sum(1, 0, n - 1, query[i].first.first, query[i].first.second);
  }
  for (ll i = 0; i < q; i++)
    cout << ans[i] << "\\n";
}