C++ online queries(MST and lowest common ancestor(powers of 2)) and offline (sorting,2 pointers))
Online: \n\nclass Solution {\n#define pb push_back\n#define pi pair\n#define fi first\n#define sc second\nvectorparent,lvl;\nvector>lis;\nvector>g,lca;\nvectorv
Online: \n\nclass Solution {\n#define pb push_back\n#define pi pair<int,int>\n#define fi first\n#define sc second\nvector<int>parent,lvl;\nvector<vector<int>>lis;\nvector<vector<pi>>g,lca;\nvector<bool>vis;\nint INF=1e9+5;\n\nstatic bool comp(vector<int>&x,vector<int>&y){\n    return x[0]<=y[0];\n}\n    \nbool Union(int u,int v){\n    u=parent[u];\n    v=parent[v];\n    if(u==v){\n        return 0;\n    }\n    if(lis[u].size()>lis[v].size()){\n        swap(u,v);\n    }\n    for(auto &child:lis[u]){\n        parent[child]=v;\n        lis[v].pb(child);\n    }\n    return 1;\n}\n  \nvoid dfs(int node,int height){\n    vis[node]=1;\n    lvl[node]=height;\n    for(auto &p:g[node]){\n        int child=p.fi,cost=p.sc;\n        if(!vis[child]){\n            lca[child][0]={node,cost};\n            dfs(child,height+1);\n        }\n    }\n}\n\nint solve(int u,int v){\n    if(lvl[u]<lvl[v]){\n        swap(u,v);\n    }\n    int dist=lvl[u]-lvl[v];\n    int ans=0;\n    for(int bit=0;bit<17;++bit){\n        if(dist & (1<<bit)){\n            ans=max(ans,lca[u][bit].sc);\n            u=lca[u][bit].fi;\n        }\n    }\n    if(u==v){\n        return ans;\n    }\n    for(int lift=16;lift>=0;lift--){\n        if(lca[u][lift].fi!=-1 && lca[u][lift].fi!=lca[v][lift].fi){\n            ans=max(ans,lca[u][lift].sc);\n            ans=max(ans,lca[v][lift].sc);\n            u=lca[u][lift].fi;\n            v=lca[v][lift].fi;\n        }\n    }\n    ans=max(ans,lca[u][0].sc);\n    ans=max(ans,lca[v][0].sc);\n    return ans;\n}\n                  \npublic:\n    vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edgeList, vector<vector<int>>& queries) {\n        parent.resize(n);\n        lis.resize(n);\n        vis.resize(n);\n        g.resize(n);\n        lvl.resize(n);\n        lca.resize(n,vector<pi>(17,{-1,INF}));\n        for(int i=0;i<n;i++){\n            parent[i]=i;\n            lis[i].pb(i);\n        }\n        vector<vector<int>>modifiedEdgeList;\n        for(auto &v:edgeList){\n            modifiedEdgeList.push_back(vector<int>{v[2],v[0],v[1]});\n        }\n        sort(modifiedEdgeList.begin(),modifiedEdgeList.end(),comp);\n        for(auto &x:modifiedEdgeList){\n            int u=x[1],v=x[2],cost=x[0];\n            if(Union(u,v)){\n                g[u].pb({v,cost});\n                g[v].pb({u,cost});\n           }\n        }\n        for(int i=0;i<n;i++){\n            if(!vis[parent[i]]){\n                dfs(parent[i],1);\n            }\n        }\n        for(int lift=1;lift<17;++lift){\n            for(int i=0;i<n;++i){\n                int x=lca[i][lift-1].fi;\n                if(x!=-1 && lca[x][lift-1].fi!=-1){\n                    lca[i][lift]={lca[x][lift-1].fi,max(lca[i][lift-1].sc,lca[x][lift-1].sc)};\n                }\n            }\n        }\n        vector<bool>ans;\n        for(auto &x:queries){\n            int u=x[0],v=x[1],limit=x[2];\n            if(parent[u]!=parent[v]){\n                ans.pb(false);\n                continue;\n            }\n            int maxi=solve(u,v);\n            ans.pb(maxi<limit);\n        }\n        return ans;\n    }\n};\n\nOffline: \n\nclass Solution {\n#define pb push_back\n#define fi first\n#define sc second\nvector<int>parent;\nvector<vector<int>>lis;\n    \nvoid Union(int u,int v){\n    u=parent[u];\n    v=parent[v];\n    if(u==v){\n        return ;\n    }\n    if(lis[u].size()>lis[v].size()){\n        swap(u,v);\n    }\n    for(auto &x:lis[u]){\n        parent[x]=v;\n        lis[v].pb(x);\n    }\n}\n    \npublic:\n    vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edgeList, vector<vector<int>>& queries) {\n        vector<vector<int>>modifiedQueries,modifiedEdgeList;\n        for(int i=0;i<queries.size();i++){\n            modifiedQueries.pb(vector<int>{queries[i][2],i,queries[i][0],queries[i][1]});\n        }\n        for(auto &v:edgeList){\n            modifiedEdgeList.pb(vector<int>{v[2],v[0],v[1]});\n        }\n        sort(modifiedQueries.begin(),modifiedQueries.end());\n        sort(modifiedEdgeList.begin(),modifiedEdgeList.end());\n        parent.resize(n);\n        lis.resize(n);\n        for(int i=0;i<n;i++){\n            parent[i]=i;\n            lis[i].pb(i);\n        }\n        int p=0;\n        vector<bool>ans(queries.size());\n        for(auto &x:modifiedQueries){\n            int limit=x[0],index=x[1],u=x[2],v=x[3];\n            while(p<modifiedEdgeList.size() && modifiedEdgeList[p][0]<limit){\n                int u1=modifiedEdgeList[p][1];\n                int v1=modifiedEdgeList[p][2];\n                Union(u1,v1);\n                p++;\n            }\n            ans[index]=parent[u]==parent[v];\n        }\n        return ans;\n    }\n};
[]