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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define INF 1000000021
#define MOD 1000000007
#define pb push_back
#define sqr(a) (a) * (a)
#define M(a, b) make_pair(a, b)
#define T(a, b, c) make_pair(a, make_pair(b, c))
#define F first
#define S second
#define all(x) (x.begin(), x.end())
#define deb(x) cerr << #x << " = " << x << '\\n'
#define N 622222

using namespace std;
using namespace __gnu_pbds;

typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

const ld pi = 2 * acos(0.0);
template <class T> bool umin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool umax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class TT> bool pal(T a, TT n) {
  int k = 0;
  for (int i = 0; i <= n / 2; i++) {
    if (a[i] != a[n - i - 1]) {
      k = 1;
      break;
    }
  }
  return k ? 0 : 1;
}

// int month[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
vector<pii> v[N];
int a[N];
int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= q; i++) {
    int x, y;
    cin >> x >> y;
    v[y].pb(M(x, i));
  }
  vi ans(q + 1, 0);
  map<int, int> mp;
  indexed_set s;
  for (int i = 1; i <= n; i++) {
    if (mp.count(a[i])) {
      s.erase(mp[a[i]]);
    }
    s.insert(i);
    mp[a[i]] = i;
    for (auto it : v[i]) {
      int u = s.order_of_key(it.F - 1);
      int e = *s.find_by_order(u);
      u++;
      u -= (e > it.F - 1);
      ans[it.S] = s.size() - u;
    }
  }
  for (int i = 1; i <= q; i++)
    cout << ans[i] << '\\n';

  getchar();
  getchar();
  return 0;
  // ios::sync_with_stdio(false);
  // cin.tie(0);
}

Error: 0

Fixed submission:

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

#define ll long long
#define ar array
#define INF INT_MAX

const int mxN = 200000;
int n, q, a[mxN], b[mxN];
map<int, vector<int>> m;

vector<int> combine(vector<int> a, vector<int> b) {
  vector<int> v(a.size() + b.size());
  merge(a.begin(), a.end(), b.begin(), b.end(), v.begin());
  return v;
}
// segment tree time!
vector<int> st[4 * mxN];
void build(int u = 1, int l = 0, int r = n - 1) {
  if (l == r) {
    st[u].push_back(b[l]);
    return;
  }
  int mid = (l + r) / 2;
  build(2 * u, l, mid);
  build(2 * u + 1, mid + 1, r);
  st[u] = combine(st[2 * u], st[2 * u + 1]);
}
int qry(int x, int y, int u = 1, int l = 0, int r = n - 1) {
  if (l > y || r < x)
    return 0;
  if (x <= l && r <= y) {
    int below = lower_bound(st[u].begin(), st[u].end(), y + 1) - st[u].begin();
    return st[u].size() - below;
  }
  int mid = (l + r) / 2;
  return qry(x, y, 2 * u, l, mid) + qry(x, y, 2 * u + 1, mid + 1, r);
}
//

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> q;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    m[a[i]].push_back(i);
  }
  for (auto &x : m) {
    vector<int> v = x.second;
    for (int i = 0; i < v.size() - 1; ++i)
      b[v[i]] = v[i + 1];
    b[v.back()] = INF;
  }
  m.clear();
  build();
  while (q--) {
    int l, r;
    cin >> l >> r, --l, --r;
    cout << qry(l, r) << '\\n';
  }
  return 0;
}