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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;

vector<P> E[100000];
int d[100000];
P G[200000];
int s[100000], t[100000], l[100000], r[100000];
vector<int> x[200000];

int par[100000];
int find(int x) {
  if (par[x] == x)
    return x;
  return par[x] = find(par[x]);
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x != y)
    par[x] = y;
}
int main() {
  int n, m, k, q;
  scanf("%d%d%d%d", &n, &m, &k, &q);
  rep(i, m) {
    int a, b, l;
    scanf("%d%d%d", &a, &b, &l);
    a--;
    b--;
    E[a].push_back(P(l, b));
    E[b].push_back(P(l, a));
    G[i] = P(a, b);
  }
  memset(d, 0x3f, sizeof(d));
  priority_queue<P, vector<P>, greater<P>> que;
  rep(i, k) {
    int f;
    scanf("%d", &f);
    f--;
    d[f] = 0;
    que.push(P(0, f));
  }
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (d[p.second] != p.first)
      continue;
    for (P u : E[p.second]) {
      if (d[u.second] > p.first + u.first) {
        d[u.second] = p.first + u.first;
        que.push(P(d[u.second], u.second));
      }
    }
  }
  sort(G, G + m, [](P a, P b) {
    return min(d[a.first], d[a.second]) > min(d[b.first], d[b.second]);
  });
  rep(i, q) {
    scanf("%d%d", &s[i], &t[i]);
    s[i]--;
    t[i]--;
    r[i] = m - 1;
  }
  rep(k, 20) {
    rep(i, m) x[i].clear();
    rep(i, n) par[i] = i;
    rep(i, q) x[(l[i] + r[i]) / 2].push_back(i);
    rep(i, m) {
      unite(G[i].first, G[i].second);
      for (int j : x[i]) {
        if (find(s[j]) == find(t[j]))
          r[j] = i;
        else
          l[j] = i + 1;
      }
    }
  }
  rep(i, q) printf("%d\\n", min(d[G[l[i]].first], d[G[l[i]].second]));
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

vector<P> E[100000];
int d[100000];
P G[200000];
int s[100000], t[100000], l[100000], r[100000];
vector<int> x[100000];

int par[100000];
int find(int x) {
  if (par[x] == x)
    return x;
  return par[x] = find(par[x]);
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x != y)
    par[x] = y;
}
int main() {
  int n, m, k, q;
  scanf("%d%d%d%d", &n, &m, &k, &q);
  rep(i, m) {
    int a, b, l;
    scanf("%d%d%d", &a, &b, &l);
    a--;
    b--;
    E[a].push_back(P(l, b));
    E[b].push_back(P(l, a));
    G[i] = P(a, b);
  }
  memset(d, 0x3f, sizeof(d));
  priority_queue<P, vector<P>, greater<P>> que;
  rep(i, k) {
    int f;
    scanf("%d", &f);
    f--;
    d[f] = 0;
    que.push(P(0, f));
  }
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (d[p.second] != p.first)
      continue;
    for (P u : E[p.second]) {
      if (d[u.second] > p.first + u.first) {
        d[u.second] = p.first + u.first;
        que.push(P(d[u.second], u.second));
      }
    }
  }
  vector<int> v, p;
  rep(i, n) v.push_back(d[i]);
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  rep(i, n) d[i] = lower_bound(v.begin(), v.end(), d[i]) - v.begin();
  sort(G, G + m, [](P a, P b) {
    return min(d[a.first], d[a.second]) > min(d[b.first], d[b.second]);
  });
  for (int i = m - 1; i >= 0; i--)
    p.push_back(min(d[G[i].first], d[G[i].second]));
  rep(i, q) {
    scanf("%d%d", &s[i], &t[i]);
    s[i]--;
    t[i]--;
    r[i] = p.back();
  }
  rep(k, 20) {
    rep(i, n) x[i].clear(), par[i] = i;
    rep(i, q) {
      int md = (l[i] + r[i]) / 2;
      if (r[i] - l[i] == 1)
        md = r[i];
      x[m - (lower_bound(p.begin(), p.end(), md) - p.begin()) - 1].push_back(i);
    }
    rep(i, m) {
      unite(G[i].first, G[i].second);
      int c = min(d[G[i].first], d[G[i].second]);
      for (int j : x[i]) {
        if (find(s[j]) == find(t[j]))
          l[j] = c;
        else
          r[j] = c - 1;
      }
    }
  }
  rep(i, q) printf("%d\\n", v[l[i]]);
}