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

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

template <typename T>
vector<T> dijkstra(Int s, vector<vector<pair<Int, T>>> &G, T INF) {
  using P = pair<T, Int>;
  Int n = G.size();
  vector<T> d(n, INF);
  vector<Int> b(n, -1);
  priority_queue<P, vector<P>, greater<P>> q;
  d[s] = 0;
  q.emplace(d[s], s);
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    Int v = p.second;
    if (d[v] < p.first)
      continue;
    for (auto &e : G[v]) {
      Int u = e.first;
      T c = e.second;
      if (d[u] > d[v] + c) {
        d[u] = d[v] + c;
        b[u] = v;
        q.emplace(d[u], u);
      }
    }
  }
  return d;
}

using P = pair<Int, Int>;
struct SegmentTree {
  Int n;
  vector<P> v;
  vector<vector<Int>> dat;
  SegmentTree(){};
  SegmentTree(Int n, vector<P> v) : v(v) {
    init(n);
    build();
  };

  void init(Int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    dat.clear();
    dat.resize(2 * n - 1);
  }

  void build() {
    for (Int i = 0; i < (Int)v.size(); i++)
      dat[v[i].first + (n - 1)].emplace_back(v[i].second);

    for (Int i = 0; i < n; i++)
      sort(dat[i + n - 1].begin(), dat[i + n - 1].end());

    for (Int i = n - 2; i >= 0; i--) {
      merge(dat[i * 2 + 1].begin(), dat[i * 2 + 1].end(),
            dat[i * 2 + 2].begin(), dat[i * 2 + 2].end(),
            back_inserter(dat[i]));
    }
  }
  // [a,b) * [c,d)
  inline Int query(Int a, Int b, Int c, Int d) {
    Int res = 0;
    auto calc = [a, b, c, d](vector<Int> &x) {
      auto latte = lower_bound(x.begin(), x.end(), d);
      auto malta = lower_bound(x.begin(), x.end(), c);
      return Int(latte - malta);
    };
    for (Int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        res += calc(dat[(l++) - 1]);
      if (r & 1)
        res += calc(dat[(--r) - 1]);
    }
    return res;
  }
};

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

  Int n, m;
  Int INF = 1LL << 61;
  cin >> n >> m;

  using P = pair<Int, Int>;
  vector<vector<P>> G(n);
  for (Int i = 0; i < m; i++) {
    Int a, b, c;
    cin >> a >> b >> c;
    G[a].push_back(P(b, c));
    G[b].push_back(P(a, c));
  }

  vector<Int> ds = dijkstra(0, G, INF);
  vector<Int> df = dijkstra(n - 1, G, INF);

  vector<Int> fs, fg;
  Int q;
  cin >> q;
  for (Int i = 0; i < q; i++) {
    Int a, b;
    cin >> a >> b;
    // cout << a << " " << b << endl;
    fs.push_back(a);
    fg.push_back(b);
  }

  map<Int, Int> ms, mf;
  {
    vector<Int> d = ds;
    for (Int i = 0; i < q; i++)
      d.push_back(fs[i]);
    d.push_back(INF);
    sort(d.begin(), d.end());
    d.erase(unique(d.begin(), d.end()), d.end());
    // for ( int i = 0; i < (Int)d.size(); i++ ) cout << d[i] << " ";
    // cout << endl;
    for (Int i = 0; i < (Int)d.size(); i++)
      ms[d[i]] = i;
  }
  {
    vector<Int> d = df;
    for (Int i = 0; i < q; i++)
      d.push_back(fg[i]);
    d.push_back(INF);
    sort(d.begin(), d.end());
    d.erase(unique(d.begin(), d.end()), d.end());
    for (Int i = 0; i < (Int)d.size(); i++)
      mf[d[i]] = i;
  }

  vector<P> vp;
  for (Int i = 0; i < n; i++) {
    vp.push_back(P(ms[ds[i]], mf[df[i]]));
  }

  SegmentTree t(n + q + 1000, vp);

  for (Int i = 0; i < q; i++) {
    cout << t.query(0, ms[fs[i]] + 1, mf[fg[i]], mf[1LL << 61]) << endl;
    // cout << fs[i] << " " << fg[i] << endl;
    // cout << ms[fs[i]]+1 << " " << mf[fg[i]] << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

template <typename T>
vector<T> dijkstra(Int s, vector<vector<pair<Int, T>>> &G, T INF) {
  using P = pair<T, Int>;
  Int n = G.size();
  vector<T> d(n, INF);
  vector<Int> b(n, -1);
  priority_queue<P, vector<P>, greater<P>> q;
  d[s] = 0;
  q.emplace(d[s], s);
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    Int v = p.second;
    if (d[v] < p.first)
      continue;
    for (auto &e : G[v]) {
      Int u = e.first;
      T c = e.second;
      if (d[u] > d[v] + c) {
        d[u] = d[v] + c;
        b[u] = v;
        q.emplace(d[u], u);
      }
    }
  }
  return d;
}

using P = pair<Int, Int>;
struct SegmentTree {
  Int n;
  vector<P> v;
  vector<vector<Int>> dat;
  SegmentTree(){};
  SegmentTree(Int n, vector<P> v) : v(v) {
    init(n);
    build();
  };

  void init(Int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    dat.clear();
    dat.resize(2 * n - 1);
  }

  void build() {
    for (Int i = 0; i < (Int)v.size(); i++)
      dat[v[i].first + (n - 1)].emplace_back(v[i].second);

    for (Int i = 0; i < n; i++)
      sort(dat[i + n - 1].begin(), dat[i + n - 1].end());

    for (Int i = n - 2; i >= 0; i--) {
      merge(dat[i * 2 + 1].begin(), dat[i * 2 + 1].end(),
            dat[i * 2 + 2].begin(), dat[i * 2 + 2].end(),
            back_inserter(dat[i]));
    }
  }
  // [a,b) * [c,d)
  inline Int query(Int a, Int b, Int c, Int d) {
    Int res = 0;
    auto calc = [a, b, c, d](vector<Int> &x) {
      auto latte = lower_bound(x.begin(), x.end(), d);
      auto malta = lower_bound(x.begin(), x.end(), c);
      return Int(latte - malta);
    };
    for (Int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        res += calc(dat[(l++) - 1]);
      if (r & 1)
        res += calc(dat[(--r) - 1]);
    }
    return res;
  }
};

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

  Int n, m;
  Int INF = 1e9;
  cin >> n >> m;

  using P = pair<Int, Int>;
  vector<vector<P>> G(n);
  for (Int i = 0; i < m; i++) {
    Int a, b, c;
    cin >> a >> b >> c;
    G[a].push_back(P(b, c));
    G[b].push_back(P(a, c));
  }

  vector<Int> ds = dijkstra(0, G, INF);
  vector<Int> df = dijkstra(n - 1, G, INF);

  vector<Int> fs, fg;
  Int q;
  cin >> q;
  for (Int i = 0; i < q; i++) {
    Int a, b;
    cin >> a >> b;
    // cout << a << " " << b << endl;
    fs.push_back(a);
    fg.push_back(b);
  }

  map<Int, Int> ms, mf;
  {
    vector<Int> d = ds;
    for (Int i = 0; i < q; i++)
      d.push_back(fs[i]);
    d.push_back(1LL << 61);
    sort(d.begin(), d.end());
    d.erase(unique(d.begin(), d.end()), d.end());
    // for ( int i = 0; i < (Int)d.size(); i++ ) cout << d[i] << " ";
    // cout << endl;
    for (Int i = 0; i < (Int)d.size(); i++)
      ms[d[i]] = i;
  }
  {
    vector<Int> d = df;
    for (Int i = 0; i < q; i++)
      d.push_back(fg[i]);
    d.push_back(1LL << 61);
    sort(d.begin(), d.end());
    d.erase(unique(d.begin(), d.end()), d.end());
    for (Int i = 0; i < (Int)d.size(); i++)
      mf[d[i]] = i;
  }

  vector<P> vp;
  for (Int i = 0; i < n; i++) {
    vp.push_back(P(ms[ds[i]], mf[df[i]]));
  }

  SegmentTree t(n, vp);

  for (Int i = 0; i < q; i++) {
    cout << t.query(0, ms[fs[i]] + 1, mf[fg[i]], mf[1LL << 61]) << endl;
    // cout << fs[i] << " " << fg[i] << endl;
    // cout << ms[fs[i]]+1 << " " << mf[fg[i]] << endl;
  }

  return 0;
}