Language: C++

#define LOCAL
#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second	
#define db 0
#define EPS (1e-7)    //0.0000001 the value
#define PI (acos((ld)-1.0))
#define MAXN (100006)
#define ll long long int 
#define ld long double
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
#define FOR(ii, ss, ee) for(ll ii = ss; ii < (ll)ee; ++ii)
#define space " "
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define ph push
#define btinpct(x) __builtin_popcountll((x))
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusivesss
string to_string(char c) {string s(1,c);return s;}string to_string(bool b){return (b ? "true" : "false");}template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}template <typename A>string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}void degug_out() { cerr << endl; }template <typename Head, typename... Tail>void degug_out(Head H, Tail... T) {cerr << " " << to_string(H);degug_out(T...);}inline ll gcd(ll a,ll b){if(a>b)swap(a,b);if(a==0)return b;return gcd(b%a,a);}
#ifdef LOCAL
#define degug(...) cerr << "[" << #__VA_ARGS__ << "]:", degug_out(__VA_ARGS__)
#else
#define degug(...) 42
#define cerr if(0)cout
#endif
ll n, q;
struct edge {
	ll x,w,c;
};vector<edge>v[MAXN];
int st[MAXN],en[MAXN],co=1; ll dist[MAXN];
ll p[MAXN][21];
void dfs(ll x,ll par){
	st[x]=co++;p[x][0]=par;
	for(auto i:v[x])if(i.x!=par){
		dist[i.x]=dist[x]+i.w; dfs(i.x,x);
	}
	en[x]=co;
}
void t2k() { for(ll j=1;j<21;++j) for(ll i=1;i<=n;++i) p[i][j]=p[p[i][j-1]][j-1]; }
bool isp(ll a,ll b){return st[a]<=st[b]&&en[a]>=en[b];}
ll lca(ll x,ll y){
	if(isp(x,y))return x;
	if(isp(y,x))return y;
	for(ll i=20;i>=0;--i){
		if(!isp(p[x][i],y))x=p[x][i];
	}
	return p[x][0];
}
vector<pair<ll,dpi>>Q[MAXN];
struct node {
	int s,e,m;
	ll v; ll add;
	node *l,*r;
	node(ll _S,ll _E){
		s=_S,e=_E,m=(s+e)/2;
		v=0;add=0;
		l=r=0;
	}
	void update(ll x,ll y,ll nv){
		if(s==x&&e==y){add+=nv;return;}
		if(x>m){if(!r)r=new node(m+1,e);r->update(x,y,nv);}
		else if(y<=m){if(!l)l=new node(s,m);l->update(x,y,nv);}
		else {if(!l)l=new node(s,m);if(!r)r=new node(m+1,e);l->update(x,m,nv),r->update(m+1,y,nv);}
		v=l->value()+r->value();
	}
	ll value(){
		v+=add;
		if(s!=e){
			l->add+=add;
			r->add+=add;
		}
		add=0;
		return v;
	}
	ll rmq(ll x){
		value();
		if(s==e)return v;
		if(x>m)return (r?r->rmq(x):0); else return (l?l->rmq(x):0); 
	}
} *seg[MAXN], *times[MAXN];
ll ans[MAXN];
ll disty(ll x,ll y){return dist[x]+dist[y]-2*dist[lca(x,y)]; }
void solve(ll x,ll p){
	for(auto i:v[x])if(i.x!=p){
		solve(i.x,x);
	}
	for(auto i:Q[x]) {
		ans[i.f]=disty(i.s.f.f,i.s.f.s)-seg[i.s.s.f]->rmq(st[i.s.f.f])-seg[i.s.s.f]->rmq(st[i.s.f.s])+
		(times[i.s.s.f]->rmq(st[i.s.f.f])+times[i.s.s.f]->rmq(st[i.s.f.s]))*i.s.s.s;
	}
	for(auto i:v[x])if(i.x==p){
		seg[i.c]->update(st[x],en[x]-1,i.w);
		times[i.c]->update(st[x],en[x]-1,1);
	}
}
int main()
{
	FAST
	cin>>n>>q;
	FOR(i,1,n){
		ll a,b,c,d;cin>>a>>b>>c>>d;
		v[a].pb({b,d,c});
		v[b].pb({a,d,c});
	}
	dfs(1,1);
	t2k();
	seg=new node(1,n);
	times=new node(1,n);
	FOR(i,0,q){
		ll a,b,c,d;cin>>c>>d>>a>>b;
		Q[lca(a,b)].pb(make_pair(i,dpi(pi(a,b),pi(c,d))));
	}
	solve(1,1); FOR(i,1,n+1)seg[i]=new node(1,n),times[i]=new node(1,n);
	FOR(i,0,q)cout<<ans[i]<<'\n';
}


Compiler output:
a.cc: In function 'int main()':
a.cc:121:12: error: incompatible types in assignment of 'node*' to 'node* [100006]'
  121 |         seg=new node(1,n);
      |         ~~~^~~~~~~~~~~~~~
a.cc:122:14: error: incompatible types in assignment of 'node*' to 'node* [100006]'
  122 |         times=new node(1,n);
      |         ~~~~~^~~~~~~~~~~~~~