C++ solution with disjoint set + two pointers, ~O(nlogn)
\nclass disjoint_set {\npublic:\n\tvector<int> R;\n\tdisjoint_set(int n) : R(n) { iota(R.begin(), R.end(), 0); }\n\tint find(int i) { return R[i] == i ? i : R[i
```\nclass disjoint_set {\npublic:\n\tvector<int> R;\n\tdisjoint_set(int n) : R(n) { iota(R.begin(), R.end(), 0); }\n\tint find(int i) { return R[i] == i ? i : R[i] = find(R[i]); }\n\tbool join(int i, int j) { i = find(i); j = find(j); if (i == j) return false; R[i] = j; return true; }\n};\nclass Solution {\npublic:\n    vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& el, vector<vector<int>>& qs) {\n        disjoint_set ds(n);\n        sort(el.begin(), el.end(), [](auto &a, auto &b){return a[2] < b[2];});\n\n        vector<int> idx(qs.size()); // index sorting instead of data sorting\n        iota(idx.begin(), idx.end(), 0);\n        sort(idx.begin(), idx.end(), [&](int a, int b){return qs[a][2] < qs[b][2];});\n\n        int x = 0;\n        vector<bool> ans(qs.size());\n        for (auto &i : idx) {\n            for (; x < el.size() && el[x][2] < qs[i][2]; x++)\n                ds.join(el[x][0], el[x][1]);\n            ans[i] = ds.find(qs[i][0]) == ds.find(qs[i][1]);\n        }\n        return ans;\n    }\n};\n```
[]