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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
struct Edge {
  int u, v;
  ll c;
  Edge(int u, int v, ll c) : u(u), v(v), c(c) {}
  bool operator<(const Edge &e) const { return c > e.c; }
};
typedef vector<Edge> Node;
typedef vector<Node> Graph;
void dijkstra(vector<ll> &dist, Graph &G, int s) {
  int N = G.size();
  dist = vector<ll>(N, -1);
  priority_queue<Edge> que;
  que.push(Edge(-1, s, 0));
  dist[s] = 0;
  while (!que.empty()) {
    Edge e = que.top();
    que.pop();
    if (dist[e.v] < e.c)
      continue;
    REP(i, G[e.v].size()) {
      Edge f = G[e.v][i];
      if (dist[f.v] == -1 || dist[f.v] > e.c + f.c) {
        dist[f.v] = e.c + f.c;
        que.push(Edge(f.u, f.v, e.c + f.c));
      }
    }
  }
}

struct RangeTree {
  static const int ST_SIZE = (1 << 17);
  vector<ll> dat[2 * ST_SIZE];
  void init(int k, int l, int r, vector<ll> &v) {
    if (r - l == 1) {
      dat[k].push_back(v[l]);
    } else {
      int lch = k * 2 + 1, rch = k * 2 + 2;
      init(lch, l, (l + r) / 2, v);
      init(rch, (l + r) / 2, r, v);

      dat[k].resize(r - l);
      merge(dat[lch].begin(), dat[lch].end(), dat[rch].begin(), dat[rch].end(),
            dat[k].begin());
    }
  }
  int query(int i, int j, ll x, int k, int l, int r) {
    if (j <= l || r <= i)
      return 0;
    if (i <= l && r <= j) {
      return upper_bound(dat[k].begin(), dat[k].end(), x) - dat[k].begin();
    } else {
      int lc = query(i, j, x, 2 * k + 1, l, (l + r) / 2);
      int rc = query(i, j, x, 2 * k + 2, (l + r) / 2, r);
      return lc + rc;
    }
  }
};

int main() {
  int N, M;
  while (cin >> N >> M && N) {
    Graph G(N);
    REP(i, M) {
      int a, b, c;
      cin >> a >> b >> c;
      G[a].push_back(Edge(a, b, c));
      G[b].push_back(Edge(b, a, c));
    }
    vector<ll> dist, rdist;
    dijkstra(dist, G, 0);
    dijkstra(rdist, G, N - 1);

    vector<pair<ll, ll>> ps(N);
    REP(i, N) { ps[i] = make_pair(dist[i], -rdist[i]); }
    sort(ps.begin(), ps.end());
    REP(i, N) {
      dist[i] = ps[i].first;
      rdist[i] = ps[i].second;
    }
    RangeTree tree;
    tree.init(0, 0, N, rdist);

    int Q;
    cin >> Q;
    REP(i, Q) {
      ll fs, fg;
      cin >> fs >> fg;
      int e = upper_bound(dist.begin(), dist.end(), fs) - dist.begin();
      int ans = tree.query(0, e, -fg, 0, 0, N);
      cout << ans << endl;
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
struct Edge {
  int u, v;
  ll c;
  Edge(int u, int v, ll c) : u(u), v(v), c(c) {}
  bool operator<(const Edge &e) const { return c > e.c; }
};
typedef vector<Edge> Node;
typedef vector<Node> Graph;
void dijkstra(vector<ll> &dist, Graph &G, int s) {
  int N = G.size();
  dist = vector<ll>(N, -1);
  priority_queue<Edge> que;
  que.push(Edge(-1, s, 0));
  dist[s] = 0;
  while (!que.empty()) {
    Edge e = que.top();
    que.pop();
    if (dist[e.v] < e.c)
      continue;
    REP(i, G[e.v].size()) {
      Edge f = G[e.v][i];
      if (dist[f.v] == -1 || dist[f.v] > e.c + f.c) {
        dist[f.v] = e.c + f.c;
        que.push(Edge(f.u, f.v, e.c + f.c));
      }
    }
  }
}

struct RangeTree {
  static const int ST_SIZE = (1 << 20) - 1;
  vector<ll> dat[ST_SIZE];
  void init(int k, int l, int r, vector<ll> &v) {
    if (r - l == 1) {
      dat[k].push_back(v[l]);
    } else {
      int lch = k * 2 + 1, rch = k * 2 + 2;
      init(lch, l, (l + r) / 2, v);
      init(rch, (l + r) / 2, r, v);

      dat[k].resize(r - l);
      merge(dat[lch].begin(), dat[lch].end(), dat[rch].begin(), dat[rch].end(),
            dat[k].begin());
    }
  }
  int query(int i, int j, ll x, int k, int l, int r) {
    if (j <= l || r <= i)
      return 0;
    if (i <= l && r <= j) {
      return upper_bound(dat[k].begin(), dat[k].end(), x) - dat[k].begin();
    } else {
      int lc = query(i, j, x, 2 * k + 1, l, (l + r) / 2);
      int rc = query(i, j, x, 2 * k + 2, (l + r) / 2, r);
      return lc + rc;
    }
  }
};

int main() {
  int N, M;
  while (cin >> N >> M && N) {
    Graph G(N);
    REP(i, M) {
      int a, b, c;
      cin >> a >> b >> c;
      G[a].push_back(Edge(a, b, c));
      G[b].push_back(Edge(b, a, c));
    }
    vector<ll> dist, rdist;
    dijkstra(dist, G, 0);
    dijkstra(rdist, G, N - 1);

    vector<pair<ll, ll>> ps(N);
    REP(i, N) { ps[i] = make_pair(dist[i], -rdist[i]); }
    sort(ps.begin(), ps.end());
    REP(i, N) {
      dist[i] = ps[i].first;
      rdist[i] = ps[i].second;
    }
    RangeTree tree;
    /*
    tree.init(0, 0, N, rdist);

    int Q; cin>>Q;
    REP(i, Q){
      ll fs, fg;
      cin>>fs>>fg;
      int e = upper_bound(dist.begin(), dist.end(), fs) - dist.begin();
      int ans = tree.query(0, e, -fg, 0, 0, N);
      cout<<ans<<endl;
    }
    */
  }
  return 0;
}