Language: C++

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
# define  pii pair<ll,ll> 
# define pb push_back
# define f first 
# define s second 

class Edge
{
    public:
    ll b;
    ll c;
    ll d;
    
}

vector<Edge> Adj[100001];
ll visited[100001];
vector<ll> Euler_tour;
vector<ll> Col_Adj[100001];
vector<ll> count_Adj[100001];
vector<ll> value_Adj[100001];
map<ll,ll> mp;
ll distance[100001];

// have to deal root PARENT,mean associated Edge
// to deal with lower bound upper bound returning node


ll depth[100010];
ll parent[100010][50];




void dfs_lca(int u,int p,int d)
{
    depth[u]=d;
    parent[u][0]=p;
    
    
    for(auto v:Adj[u])
    {
    if(v.b==p) continue;
    
    dfs(v.b,u,d+1);
    
    }
    

}

int  lca(int u,int v)

{
    if(depth[u]>depth[v]) swap(u,v);
    ll diff=depth[v]-depth[u];
    for(int i=0;i<50;i++)
    {
       if((diff>>i)&1)
       {
           v=parent[v][i];
       }
    }
    
    
    if(u==v) return v;
    
    for(ll i=49;i>=0;i--)
    {
        if(parent[u][i]!=parent[v][i])
        {
            u=parent[u][i];
            v=parent[v][i];
        }
    }
    
    return parent[u][0];
    
}



void dfs(ll u)
{  visited[u]=1;
   
    for(auto v:Adj[u])
    {  
       if(visited[v.b]!=1)
       {
       distance[v.b]=distance[u]+v.d;   
       Euler_tour.pb(v.d);
       mp[v]=Euler_tour.size()-1;
       Col_Adj[v.c].pb(Euler_tour.size()-1);
       count_Adj[v.c].pb(1);
       value_Adj[v.c].pb(v.d);
       
       
       dfs(v.b);
       count_Adj[v.c].pb(-1);
       value_Adj[v.c].pb(-v.d);
       Euler_tour.pb(-v.d);
       }
    }
}

int main() {
     
     ll n,q;
     cin>>n>>q;
     
     memset(visited,0,sizeof(visited));
     memset(parent,-1,sizeof(parent))
     
     for(ll i=0;i<n-1;i++)
     {
         ll u,v,co,di;
         cin>>u>>v>>c>>d;
         Edge node1,node2;
         node.b=v;
         node.c=co;
         node.d=di;
         
         node2.b=u;
         node2.c=co;
         node2.d=di;
         
         Adj[u].pb(node);
         Adj[v].pb(node2);
         
     }
     
     dfs(1);
     dfs_lca(1,-1,0);
     
 
 for(ll j=1;j<50;j++)
 {
     for(int i=1;i<=n;i++)
     {   if(parent[i][j-1]!=-1)
         parent[i][j]=parent[parent[i][j-1]][j-1];
     }
 }
    
     
     
     for(int i=1;i<=n;i++)
     {
         for(j=0;j<Col_Adj[i].size()-1;j++)
         {
             value_Adj[j+1]=value_Adj[j]+value_Adj[J+1];
             count_Adj[j+1]=count_Adj[j]+count_Adj[j+1];
         }
     }
     
     for(ll i=0;i<q;i++)
     {
         ll x,y,cd,dt;
         cin>>x>>y>>cd>>dt;
         
         ll lca_no=lca(x,y);
         
         ll req_idx=mp[x];
         ll req_idx2=mp[y];
         
         ll req_idx2=mp[lca_no];
         
         ll my_idx=lower_bound(Col_Adj[cd].begin(),Col_Adj[cd].end(),req_idx)-Col_Adj[cd].begin();
        
         if(Col_Adj[cd][my_idx]!=req_idx) my_idx-1;
         
         ll req_sum=value_Adj[my_idx];
         ll dist_col=count_Adj[my_idx];
         
         
         
         ll my_idx2 =lower_bound(Col_Adj[cd].begin(),Col_Adj[cd].end(),req_idx2)-Col_Adj[cd].begin();
        
         if(Col_Adj[cd][my_idx2]!=req_idx) my_idx2-1;
         
         ll req_sum2=value_Adj[my_idx2];
         ll dist_col2=count_Adj[my_idx2];
         
         
         ll my_idx3 =lower_bound(Col_Adj[cd].begin(),Col_Adj[cd].end(),req_idx3)-Col_Adj[cd].begin();
        
         if(Col_Adj[cd][my_idx3]!=req_idx) my_idx3-1;
         
         ll req_sum3=value_Adj[my_idx3];
         ll dist_col3=count_Adj[my_idx3];
         
         ll rtyx= distance[x]- req_sum+dist_col*dt;
         ll rtyy= distance[y]- req_sum2+dist_col2*dt;
         ll rtylc= distance[lca_no]- req_sum3+dist_col3*dt;
         
         ll ans= rtyx+ rtyy - 2*rtylc;
         
         cout<<ans<<endl;
         
        }
     
	return 0;
}


Compiler output:
a.cc:19:14: error: invalid declarator before 'Adj'
   19 | vector<Edge> Adj[100001];
      |              ^~~
a.cc: In function 'void dfs_lca(int, int, int)':
a.cc:44:16: error: 'Adj' was not declared in this scope
   44 |     for(auto v:Adj[u])
      |                ^~~
a.cc:48:5: error: 'dfs' was not declared in this scope; did you mean 'ffs'?
   48 |     dfs(v.b,u,d+1);
      |     ^~~
      |     ffs
a.cc: In function 'void dfs(ll)':
a.cc:89:16: error: 'Adj' was not declared in this scope
   89 |     for(auto v:Adj[u])
      |                ^~~
a.cc:93:8: error: reference to 'distance' is ambiguous
   93 |        distance[v.b]=distance[u]+v.d;
      |        ^~~~~~~~
In file included from /usr/include/c++/14/filesystem:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:200,
                 from a.cc:1:
/usr/include/c++/14/bits/fs_path.h:1431:1: note: candidates are: 'std::ptrdiff_t std::distance(filesystem::__cxx11::path::iterator, filesystem::__cxx11::path::iterator)'
 1431 | distance(filesystem::path::iterator __first, filesystem::path::iterator __last)
      | ^~~~~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:148:5: note:                 'template<class _InputIterator> constexpr typename std::iterator_traits< <template-parameter-1-1> >::difference_type std::distance(_InputIterator, _InputIterator)'
  148 |     distance(_InputIterator __first, _InputIterator __last)
      |     ^~~~~~~~
a.cc:26:4: note:                 'll distance [100001]'
   26 | ll distance[100001];
      |    ^~~~~~~~
a.cc:93:22: error: reference to 'distance' is ambiguous
   93 |        distance[v.b]=distance[u]+v.d;
      |                      ^~~~~~~~
/usr/include/c++/14/bits/fs_path.h:1431:1: note: candidates are: 'std::ptrdiff_t std::distance(filesystem::__cxx11::path::iterator, filesystem::__cxx11::path::iterator)'
 1431 | distance(filesystem::path::iterator __first, filesystem::path::iterator __last)
      | ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:148:5: note:                 'template<class _InputIterator> constexpr typename std::iterator_traits< <template-parameter-1-1> >::difference_type std::distance(_InputIterator, _InputIterator)'
  148 |     distance(_InputIterator __first, _InputIterator __last)
      |     ^~~~~~~~
a.cc:26:4: note:                 'll distance [100001]'
   26 | ll distance[100001];
      |    ^~~~~~~~
a.cc: In function 'int main()':
a.cc:115:38: error: expected ';' before 'for'
  115 |      memset(parent,-1,sizeof(parent))
      |                                      ^
      |                                      ;
  116 | 
  117 |      for(ll i=0;i<n-1;i++)
      |      ~~~                              
a.cc:117:17: error: 'i' was not declared in this scope
  117 |      for(ll i=0;i<n-1;i++)
      |                 ^
a.cc:151:14: error: 'j' was not declared in this scope
  151 |          for(j=0;j<Col_Adj[i].size()-1;j++)
      |              ^
a.cc:153:52: error: 'J' was not declared in this scope
  153 |              value_Adj[j+1]=value_Adj[j]+value_Adj[J+1];
      |                                                    ^
a.cc:168:13: error: redeclaration of 'll req_idx2'
  168 |          ll req_idx2=mp[lca_no];
      |             ^~~~~~~~
a.cc:166:13: note: 'll req_idx2' previously declared here
  166 |          ll req_idx2=mp[y];
      |             ^~~~~~~~
a.cc:174:37: error: cannot convert 'std::vector<long long int>' to 'll' {aka 'long long int'} in initialization
  174 |          ll req_sum=value_Adj[my_idx];
      |                     ~~~~~~~~~~~~~~~~^
      |                                     |
      |                                     std::vector<long long int>
a.cc:175:38: error: cannot convert 'std::vector<long long int>' to 'll' {aka 'long long int'} in initialization
  175 |          ll dist_col=count_Adj[my_idx];
      |                      ~~~~~~~~~~~~~~~~^
      |                                      |
      |                                      std::vector<long long int>
a.cc:183:39: error: cannot convert 'std::vector<long long int>' to 'll' {aka 'long long int'} in initialization
  183 |          ll req_sum2=value_Adj[my_idx2];
      |                      ~~~~~~~~~~~~~~~~~^
      |                                       |
      |                                       std::vector<long long int>
a.cc:184:40: error: cannot convert 'std::vector<long long int>' to 'll' {aka 'long long int'} in initialization
  184 |          ll dist_col2=count_Adj[my_idx2];
      |                       ~~~~~~~~~~~~~~~~~^
      |                                        |
      |                                        std::vector<long long int>
a.cc:187:72: error: 'req_idx3' was not declared in this scope; did you mean 'req_idx2'?
  187 |          ll my_idx3 =lower_bound(Col_Adj[cd].begin(),Col_Adj[cd].end(),req_idx3)-Col_Adj[cd].begin();
      |                                                                        ^~~~~~~~
      |                                                                        req_idx2
a.cc:191:39: error: cannot convert 'std::vector<long long int>' to 'll' {aka 'long long int'} in initialization
  191 |          ll req_sum3=value_Adj[my_idx3];
      |                      ~~~~~~~~~~~~~~~~~^
      |                                       |
      |                                       std::vector<long long int>
a.cc:192:40: error: cannot convert 'std::vector<long long int>' to 'll' {aka 'long long int'} in initialization
  192 |          ll dist_col3=count_Adj[my_idx3];
      |                       ~~~~~~~~~~~~~~~~~^
      |                                        |
      |                                        std::vector<long long int>
a.cc:194:19: error: reference to 'distance' is ambiguous
  194 |          ll rtyx= distance[x]- req_sum+dist_col*dt;
      |                   ^~~~~~~~
/usr/include/c++/14/bits/fs_path.h:1431:1: note: candidates are: 'std::ptrdiff_t std::distance(filesystem::__cxx11::path::iterator, filesystem::__cxx11::path::iterator)'
 1431 | distance(filesystem::path::iterator __first, filesystem::path::iterator __last)
      | ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:148:5: note:                 'template<class _InputIterator> constexpr typename std::iterator_traits< <template-parameter-1-1> >::difference_type std::distance(_InputIterator, _InputIterator)'
  148 |     distance(_InputIterator __first, _InputIterator __last)
      |     ^~~~~~~~
a.cc:26:4: note:                 'll distance [100001]'
   26 | ll distance[100001];
      |    ^~~~~~~~
a.cc:195:19: error: reference to 'distance' is ambiguous
  195 |          ll rtyy= distance[y]- req_sum2+dist_col2*dt;
      |                   ^~~~~~~~
/usr/include/c++/14/bits/fs_path.h:1431:1: note: candidates are: 'std::ptrdiff_t std::distance(filesystem::__cxx11::path::iterator, filesystem::__cxx11::path::iterator)'
 1431 | distance(filesystem::path::iterator __first, filesystem::path::iterator __last)
      | ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:148:5: note:                 'template<class _InputIterator> constexpr typename std::iterator_traits< <template-parameter-1-1> >::difference_type std::distance(_InputIterator, _InputIterator)'
  148 |     distance(_InputIterator __first, _InputIterator __last)
      |     ^~~~~~~~
a.cc:26:4: note:                 'll distance [100001]'
   26 | ll distance[100001];
      |    ^~~~~~~~
a.cc:196:20: error: reference to 'distance' is ambiguous
  196 |          ll rtylc= distance[lca_no]- req_sum3+dist_col3*dt;
      |                    ^~~~~~~~
/usr/include/c++/14/bits/fs_path.h:1431:1: note: candidates are: 'std::ptrdiff_t std::distance(filesystem::__cxx11::path::iterator, filesystem::__cxx11::path::iterator)'
 1431 | distance(filesystem::path::iterator __first, filesystem::path::iterator __last)
      | ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:148:5: note:                 'template<class _InputIterator> constexpr typename std::iterator_traits< <template-parameter-1-1> >::difference_type std::distance(_InputIterator, _InputIterator)'
  148 |     distance(_InputIterator __first, _InputIterator __last)
      |     ^~~~~~~~
a.cc:26:4: note:                 'll distance [100001]'
   26 | ll distance[100001];
      |    ^~~~~~~~