Language: C++

#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
const int MX=2e5+5;
vector<pair<int,pair<int,int>>> a[MX];
int check[MX];
int b[MX*2];
int c[MX],d[MX],e[MX],dd[MX];
bool visit[MX];
int p[MX][20];
int dist[MX];
int in[MX],out[MX],dth[MX];
int k;
int cnt;
int n,m;
int ans[MX];

inline int get_lca(int u,int v){
    int i;
    for(i=19 ; i>=0 ; i--)if(dth[v]-dth[u]>=(1<<i))v=p[v][i];
    if(u==v)return v;
    for(i=19 ; i>=0 ; i--)if(p[v][i]!=p[u][i])v=p[v][i],u=p[u][i];
    return p[v][0];
}
struct A{
    int x,y,u,v,l,r,idx,lca;
    void process(){
        if(in[u]>in[v])swap(u,v);
        lca=get_lca(u,v);
        if(lca==u){
            l=in[u]+1;
            r=in[v];
        }
        else{
            l=out[u];
            r=in[v];
        }
    }
    bool operator<(const A&t){
        if(l/k==t.l/k)return r<t.r;
        return l/k<t.l;
    }
}q[MX];
void dfs(int v){
    in[v]=++cnt;
    b[in[v]]=u;
    visit[v]=1;
    for(auto &i: a[v]){
        int u=i.f;
        if(visit[u]==0){
            dth[u]=dth[v]+1;
            dist[u]=dist[v]+i.s.s;
            p[u][0]=v;
            c[u]=i.s.f;
            d[u]=i.s.s;
            dfs(u);
        }
    }
    out[v]=++cnt;
    b[out[v]]=u;
}
int main(){
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    int i,j;
    cin>>n>>m;
    k=n/sqrt(n);
    for(i=1 ; i<n ; i++){
        int u,v,cc,dd;
        cin>>u>>v>>cc>>dd;
        a[u].push_back({v,{cc,dd}});
        a[v].push_back({u,{cc,dd}});
    }

    dfs(1);
    for(i=1 ; i<20 ; i++)for(j=1 ; j<=n ; j++)p[j][i]=p[p[j][i-1]][i-1];
    for(i=1 ; i<=m ; i++){
        int x,y,u,v;
        cin>>x>>y>>u>>v;
        q[i]={x,y,u,v,0,0,i,0};
        q[i].process();
    }
    sort(q+1,q+m+1);

    for(i=1 ; i<=m ; i++){
        ans[q[i].idx]=dist[q[i].u]+dist[q[i].v]-dist[q[i].lca];
        for(j=q[i-1].r+1 ; j<=q[i].r ; j++){
            int t=b[j];
            check[t]++;
            if(check[t]==1)e[c[t]]++,dd[c[t]]+=d[t];
            if(check[t]==2) e[c[t]]--,dd[c[t]]-=d[t];
        }
        for(j=q[i].l ; j<q[i-1].l ; j++){
            int t=b[j];
            check[t]++;
            if(check[t]==1)e[c[t]]++,dd[c[t]]+=d[t];
            if(check[t]==2)e[c[t]]--,dd[c[t]]-=d[t];
        }
        for(j=q[i].r+1 ; j<=q[i-1].r ; j++){
            int t=b[j];
            check[t]--;
            if(check[t]==1)e[c[t]]++,dd[c[t]]+=d[t];
            if(check[t]==0)e[c[t]]--,dd[c[t]]-=d[t];
        }
        for(j=q[i-1].l ; j<q[i].l ; j++){
            int t=b[j];
            check[t]--;
            if(check[t]==1)e[c[t]]++,dd[c[t]]+=d[t];
            if(check[t]==0)e[c[t]]--,dd[c[t]]-=d[t];
        }
        ans[q[i].idx]-=dd[q[i].x];
        ans[q[i].idx]+=e[q[i].x]*q[i].y;
    }
    for(i=1 ; i<=m ; i++)cout<<ans[i]<<"\n";
    return 0;
}


Compiler output:
a.cc: In function 'void dfs(int)':
a.cc:47:14: error: 'u' was not declared in this scope
   47 |     b[in[v]]=u;
      |              ^
a.cc:48:5: error: reference to 'visit' is ambiguous
   48 |     visit[v]=1;
      |     ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:1:
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:10:6: note:                 'bool visit [200005]'
   10 | bool visit[MX];
      |      ^~~~~
a.cc:51:12: error: reference to 'visit' is ambiguous
   51 |         if(visit[u]==0){
      |            ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:10:6: note:                 'bool visit [200005]'
   10 | bool visit[MX];
      |      ^~~~~