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

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

#define DEBUG
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " = " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " = " << arg1 << " || ";
  __f(comma + 1, args...);
}
#else
#define debug(...)
#endif

template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os,
                                  Container const &x) {
  os << "{ ";
  for (auto &y : x)
    os << y << " ; ";
  return os << "}";
}

template <class X, class Y>
ostream &operator<<(ostream &os, pair<X, Y> const &p) {
  return os << "[ " << p.first << ", " << p.second << "]";
}

#define FAST_IO                                                                \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long int
#define pb push_back
#define mp make_pair
#define ld long double
#define sz(a) (ll)(a).size()
#define endl "\\n"

// priority_queue<ll, vector<ll>, less<ll>>pq;     // Max Heap
// priority_queue<ll, vector<ll>, greater<ll>>pq;

const int MAXN = 5e5 + 5;

ll n, q, a[MAXN], nx[MAXN];
vector<ll> adj[MAXN], segtree[2 * MAXN];

void build() {
  for (int i = n - 1; i > 0; i--) {
    ll p = 0, q = 0;
    while (p < segtree[2 * i].size() && q < segtree[2 * i + 1].size()) {
      if (segtree[2 * i][p] < segtree[2 * i + 1][q])
        segtree[i].pb(segtree[2 * i][p++]);
      else
        segtree[i].pb(segtree[2 * i + 1][q++]);
    }
    while (p < segtree[2 * i].size())
      segtree[i].pb(segtree[2 * i][p++]);
    while (q < segtree[2 * i + 1].size())
      segtree[i].pb(segtree[2 * i + 1][q++]);
  }
}
ll query(ll l, ll r, ll x) {
  ll ans = 0;
  for (l += n - 1, r += n; l < r; l /= 2, r /= 2) {
    if (l % 2) {
      ans += upper_bound(segtree[l].begin(), segtree[l].end(), x) -
             segtree[l].begin();
      l++;
    }
    if (r % 2) {
      r--;
      ans += upper_bound(segtree[r].begin(), segtree[r].end(), x) -
             segtree[r].begin();
    }
  }
  return ans;
}

int main() {
  FAST_IO;

  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = n - 1; i >= 0; i--) {
    if (!sz(adj[a[i]]))
      nx[i] = n;
    else
      nx[i] = adj[a[i]].back();
    adj[a[i]].pb(i);
  }
  for (int i = 0; i < n; i++)
    segtree[n + i].pb(nx[i]);
  build();
  while (q--) {
    ll l, r;
    cin >> l >> r;
    cout << r - l + 1 - query(l, r, r - 1) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

#define DEBUG
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " = " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " = " << arg1 << " || ";
  __f(comma + 1, args...);
}
#else
#define debug(...)
#endif

template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os,
                                  Container const &x) {
  os << "{ ";
  for (auto &y : x)
    os << y << " ; ";
  return os << "}";
}

template <class X, class Y>
ostream &operator<<(ostream &os, pair<X, Y> const &p) {
  return os << "[ " << p.first << ", " << p.second << "]";
}

#define FAST_IO                                                                \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long int
#define pb push_back
#define mp make_pair
#define ld long double
#define sz(a) (ll)(a).size()
#define endl "\\n"

// priority_queue<ll, vector<ll>, less<ll>>pq;     // Max Heap
// priority_queue<ll, vector<ll>, greater<ll>>pq;

const int MAXN = 5e5 + 5;

ll n, q, a[MAXN], nx[MAXN];
vector<ll> adj[MAXN], segtree[MAXN];

void build() {
  for (int i = n - 1; i > 0; i--) {
    ll p = 0, q = 0;
    while (p < segtree[2 * i].size() && q < segtree[2 * i + 1].size()) {
      if (segtree[2 * i][p] < segtree[2 * i + 1][q])
        segtree[i].pb(segtree[2 * i][p++]);
      else
        segtree[i].pb(segtree[2 * i + 1][q++]);
    }
    while (p < segtree[2 * i].size())
      segtree[i].pb(segtree[2 * i][p++]);
    while (q < segtree[2 * i + 1].size())
      segtree[i].pb(segtree[2 * i + 1][q++]);
  }
}
ll query(ll l, ll r, ll x) {
  ll ans = 0;
  for (l += n - 1, r += n; l < r; l /= 2, r /= 2) {
    if (l % 2) {
      ans += upper_bound(segtree[l].begin(), segtree[l].end(), x) -
             segtree[l].begin();
      l++;
    }
    if (r % 2) {
      r--;
      ans += upper_bound(segtree[r].begin(), segtree[r].end(), x) -
             segtree[r].begin();
    }
  }
  return ans;
}

int main() {
  FAST_IO;

  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = n - 1; i >= 0; i--) {
    if (!sz(adj[a[i]]))
      nx[i] = n;
    else
      nx[i] = adj[a[i]].back();
    adj[a[i]].pb(i);
  }
  for (int i = 0; i < n; i++)
    segtree[n + i].pb(nx[i]);
  build();
  while (q--) {
    ll l, r;
    cin >> l >> r;
    cout << r - l + 1 - query(l, r, r - 1) << endl;
  }
  return 0;
}