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

#ifndef ___Class_RangeTree
#define ___Class_RangeTree

// ------ Includes ------ //
#include <algorithm>
#include <limits>
#include <vector>

template <typename Type> class RangeTree {
private:
  int size_;
  std::vector<std::vector<Type>> dat;
  inline int query_(int a, int b, Type x, int k, int l, int r) {
    // Find range [a, b) : value < x : Now k-th node and the range is [l, r)
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return lower_bound(dat[k].begin(), dat[k].end(), x) - dat[k].begin();
    int lc = query_(a, b, x, (k << 1), l, (l + r) >> 1);
    int rc = query_(a, b, x, (k << 1) + 1, (l + r) >> 1, r);
    return lc + rc;
  }

public:
  RangeTree() : size_(0), dat(std::vector<Type>()){};
  template <class T> RangeTree(T begin_, T end_) {
    int n = (end_ - begin_);
    for (size_ = 1; size_ < n; size_ <<= 1)
      ;
    dat.resize(size_ << 1, std::vector<Type>());
    for (int i = 0; i < n; i++)
      dat[i + size_] = std::vector<Type>({*(begin_ + i)});
    for (int i = size_ - 1; i > 0; i--) {
      dat[i].resize(dat[i << 1].size() + dat[(i << 1) + 1].size());
      std::merge(dat[i << 1].begin(), dat[i << 1].end(),
                 dat[(i << 1) + 1].begin(), dat[(i << 1) + 1].end(),
                 dat[i].begin());
    }
  }
  inline int size() { return size_; }
  inline int query(int l, int r, Type x) {
    // Find range [a, b) : value < x
    return query_(l, r, x, 1, 0, size_);
  }
};

#endif

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;
typedef long long ll;
const ll inf = (1LL << 61);
struct edge {
  int to;
  ll cost;
};
const bool operator<(const edge &e1, const edge &e2) {
  return e1.cost < e2.cost;
}
int N, M, Q, a, b, c;
ll z1, z2;
vector<edge> G[100009];
vector<ll> dist(int s) {
  vector<ll> ret(N, inf);
  ret[s] = 0;
  priority_queue<edge> que;
  que.push(edge{s, 0});
  while (!que.empty()) {
    edge u = que.top();
    que.pop();
    for (edge e : G[u.to]) {
      if (ret[e.to] > ret[u.to] + e.cost) {
        ret[e.to] = ret[u.to] + e.cost;
        que.push(edge{e.to, -ret[e.to]});
      }
    }
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++) {
    scanf("%d%d%d", &a, &b, &c);
    G[a].push_back(edge{b, c});
    G[b].push_back(edge{a, c});
  }
  vector<ll> res1 = dist(0);
  vector<ll> res2 = dist(N - 1);
  vector<pair<ll, ll>> x0(N);
  for (int i = 0; i < N; i++)
    x0[i] = make_pair(res2[i], res1[i]);
  sort(x0.begin(), x0.end());
  vector<ll> x1(N), x2(N);
  for (int i = 0; i < N; i++)
    x1[i] = x0[i].second, x2[i] = x0[i].first;
  RangeTree<ll> q(x1.begin(), x1.end());
  scanf("%d", &Q);
  while (Q--) {
    scanf("%lld%lld", &z1, &z2);
    int ptr = lower_bound(x2.begin(), x2.end(), z2) - x2.begin();
    printf("%d\\n", q.query(ptr, N, z1 + 1));
  }
  return 0;
}

Error: -11

Fixed submission:

#ifndef ___Class_RangeTree
#define ___Class_RangeTree

// ------ Includes ------ //
#include <algorithm>
#include <limits>
#include <vector>

template <typename Type> class RangeTree {
private:
  int size_;
  std::vector<std::vector<Type>> dat;
  inline int query_(int a, int b, Type x, int k, int l, int r) {
    // Find range [a, b) : value < x : Now k-th node and the range is [l, r)
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return lower_bound(dat[k].begin(), dat[k].end(), x) - dat[k].begin();
    int lc = query_(a, b, x, (k << 1), l, (l + r) >> 1);
    int rc = query_(a, b, x, (k << 1) + 1, (l + r) >> 1, r);
    return lc + rc;
  }

public:
  RangeTree() : size_(0), dat(std::vector<Type>()){};
  template <class T> RangeTree(T begin_, T end_) {
    int n = (end_ - begin_);
    for (size_ = 1; size_ < n; size_ <<= 1)
      ;
    dat.resize(size_ << 1, std::vector<Type>());
    for (int i = 0; i < n; i++)
      dat[i + size_] = std::vector<Type>({*(begin_ + i)});
    for (int i = size_ - 1; i > 0; i--)
      std::merge(dat[i << 1].begin(), dat[i << 1].end(),
                 dat[(i << 1) + 1].begin(), dat[(i << 1) + 1].end(),
                 dat[i].begin());
  }
  inline int size() { return size_; }
  inline int query(int l, int r, Type x) {
    // Find range [a, b) : value < x
    return query_(l, r, x, 1, 0, size_);
  }
};

#endif

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;
typedef long long ll;
const ll inf = (1LL << 61);
struct edge {
  int to;
  ll cost;
};
const bool operator<(const edge &e1, const edge &e2) {
  return e1.cost < e2.cost;
}
int N, M, Q, a, b, c;
ll z1, z2;
vector<edge> G[100009];
vector<ll> dist(int s) {
  vector<ll> ret(N, inf);
  ret[s] = 0;
  priority_queue<edge> que;
  que.push(edge{s, 0});
  while (!que.empty()) {
    edge u = que.top();
    que.pop();
    for (edge e : G[u.to]) {
      if (ret[e.to] > ret[u.to] + e.cost) {
        ret[e.to] = ret[u.to] + e.cost;
        que.push(edge{e.to, -ret[e.to]});
      }
    }
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++) {
    scanf("%d%d%d", &a, &b, &c);
    G[a].push_back(edge{b, c});
    G[b].push_back(edge{a, c});
  }
  vector<ll> res1 = dist(0);
  vector<ll> res2 = dist(N - 1);
  vector<pair<ll, ll>> x0(N);
  for (int i = 0; i < N; i++)
    x0[i] = make_pair(res2[i], res1[i]);
  sort(x0.begin(), x0.end());
  vector<ll> x1(N), x2(N);
  for (int i = 0; i < N; i++)
    x1[i] = x0[i].second, x2[i] = x0[i].first;
  RangeTree<ll> q(x1.begin(), x1.end());
  scanf("%d", &Q);
  while (Q--) {
    scanf("%lld%lld", &z1, &z2);
    int ptr = lower_bound(x2.begin(), x2.end(), z2) - x2.begin();
    printf("%d\\n", q.query(ptr, N, z1 + 1));
  }
  return 0;
}