Problem:
Score : 600 points

Problem Statement

There is a tree with N vertices numbered 1 to N.
The i-th edge in this tree connects Vertex a_i and Vertex b_i, and the color and length of that edge are c_i and d_i, respectively.
Here the color of each edge is represented by an integer between 1 and N-1 (inclusive). The same integer corresponds to the same color, and different integers correspond to different colors.

Answer the following Q queries:

Query j (1 \leq j \leq Q): assuming that the length of every edge whose color is x_j is changed to y_j, find the distance between Vertex u_j and Vertex v_j. (The changes of the lengths of edges do not affect the subsequent queries.)

Constraints

2 \leq N \leq 10^5

1 \leq Q \leq 10^5

1 \leq a_i, b_i \leq N

1 \leq c_i \leq N-1

1 \leq d_i \leq 10^4

1 \leq x_j \leq N-1

1 \leq y_j \leq 10^4

1 \leq u_j < v_j \leq N

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
a_1 b_1 c_1 d_1
:
a_{N-1} b_{N-1} c_{N-1} d_{N-1}
x_1 y_1 u_1 v_1
:
x_Q y_Q u_Q v_Q

Output

Print Q lines. The j-th line (1 \leq j \leq Q) should contain the answer to Query j.

Sample Input 1

5 3
1 2 1 10
1 3 2 20
2 4 4 30
5 2 1 40
1 100 1 4
1 100 1 5
3 1000 3 4

Sample Output 1

130
200
60

The graph in this input is as follows:

Here the edges of Color 1 are shown as solid red lines, the edge of Color 2 is shown as a bold green line, and the edge of Color 4 is shown as a blue dashed line.

Query 1: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 4 is 100 + 30 = 130.

Query 2: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 5 is 100 + 100 = 200.

Query 3: Assuming that the length of every edge whose color is 3 is changed to 1000 (there is no such edge), the distance between Vertex 3 and Vertex 4 is 20 + 10 + 30 = 60. Note that the edges of Color 1 now have their original lengths.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)



#include<iostream>
#include<cstdio>
#include<cstring>
#include <cstdlib>  
#include <math.h>
#include <cmath>
#include<cctype>
#include<string>
#include<set>
#include<iomanip>
#include <map>
#include<algorithm>
#include <functional>
#include<vector>
#include<climits>
#include<stack>
#include<queue>
#include<bitset>
#include <deque>
#include <climits>
#include <typeinfo>
#include <utility> 
using namespace std;
using ll = long long;
template<typename T>using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll inf = 1LL << 60;
#define all(x) (x).begin(),(x).end()
#define puts(x) cout << x << endl
#define rep(i,m,n) for(ll i = m;i < n;++i)
#define pb push_back
#define fore(i,a) for(auto &i:a)
#define rrep(i,m,n) for(ll i = m;i >= n;--i)



struct LCA {
	static const int MAX = 1 << 18;
	vector<int>seg;
	vector<int>idx;
	void st(int p, int v) {
		p += MAX - 1;
		seg[p] = v;
		idx[p] = p - MAX + 1;
		while (p > 0) {
			p = (p - 1) / 2;
			seg[p] = min(seg[2 * p + 1], seg[2 * p + 2]);
			if (seg[2 * p + 1] > seg[2 * p + 2])idx[p] = idx[2 * p + 2];
			else if (seg[2 * p + 1] < seg[2 * p + 2])idx[p] = idx[2 * p + 1];
			else idx[p] = min(idx[2 * p + 1], idx[2 * p + 2]);
		}
	}
	pair<int, int> query(int l, int r, int a, int b, int k) {
		if (r <= a || b <= l)return { INT_MAX / 2,INT_MAX / 2 };
		if (l <= a && b <= r)return { seg[k],idx[k] };
		int m = (a + b) / 2;
		auto p1 = query(l, r, a, m, 2 * k + 1);
		auto p2 = query(l, r, m, b, 2 * k + 2);
		if (p1.first < p2.first) return p1;
		else if (p1.first > p2.first)return p2;
		else return { p1.first,min(p1.second,p2.second) };
	}

	void seginit() {
		seg.resize(2 * MAX);
		idx.resize(2 * MAX);
		for (int i = 0; i < 2 * MAX; i++)seg[i] = INT_MAX / 2;
	}
	//[l,r]
	int get(int l, int r) {
		return query(l, r + 1, 0, MAX, 0).first;
	}
	int getidx(int l, int r) {
		return query(l, r + 1, 0, MAX, 0).second;
	}

	vector<int>vs;
	vector<int>id;
	int k = 0;
	void lcadfs(int now, int p, int d, vector<int>edges[]) {
		vs[k] = now;
		id[now] = k;
		st(k++, d);
		fore(x, edges[now]) {
			if (x != p) {
				lcadfs(x, now, d + 1, edges);
				vs[k] = now;
				st(k++, d);
			}
		}
	}

	void lcainit(int n, int root, vector<int>edges[]) {
		vs.resize(2 * n);
		id.resize(n);
		seginit();
		for (int i = 0; i < n; i++)st(i, 0);
		int k = 0;
		lcadfs(root, -1, 0, edges);
	}

	int calc_lca(int x, int y) {
		return vs[getidx(min(id[x], id[y]), max(id[x], id[y]))];
	}
};

vector<int>edges[101010];
vector<vector<int>>edges2[101010];
vector<int>need[101010];
int X[101010], Y[101010], U[101010], V[101010],L[101010];
int dist[101010];
map<int, int>sum[101010], cnt[101010];
int cursum[101010], curcnt[101010];

void dfs(int now,int p) {
	fore(x, need[now]) {
		sum[now][x] = cursum[x];
		cnt[now][x] = curcnt[x];
	}
	fore(x,edges2[now]) {
		int to = x[0];
		int cost = x[1];
		int col = x[2];
		if (to != p) {
			cursum[col] += cost;
			curcnt[col]++;
			dist[to] = dist[now] + cost;
			dfs(to, now);
			cursum[col] -= cost;
			curcnt[col]--;
		}
	}
}

int main() {
	int n, q;
	cin >> n >> q;
	for (int i = 0; i < n - 1; i++) {
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		a--, b--, c--;
		edges[a].pb(b);
		edges[b].pb(a);
		edges2[a].pb({b,d,c});
		edges2[b].pb({a,d,c});
	}
	LCA lca;
	lca.lcainit(n,0,edges);

	puts(lca.calc_lca(0, 4));
	for (int i = 0; i < q; i++) {
		cin >> X[i] >> Y[i] >> U[i] >> V[i];
		X[i]--, U[i]--, V[i]--;
		L[i] = lca.calc_lca(U[i], V[i]);
		need[U[i]].pb(X[i]);
		need[V[i]].pb(X[i]);
		need[L[i]].pb(X[i]);
	}
	dfs(0, -1);
	for (int i = 0; i < q; i++) {
		int distu = dist[U[i]] - sum[U[i]][X[i]] + Y[i] * cnt[U[i]][X[i]];
		int distv = dist[V[i]] - sum[V[i]][X[i]] + Y[i] * cnt[V[i]][X[i]];
		int distl = dist[L[i]] - sum[L[i]][X[i]] + Y[i] * cnt[L[i]][X[i]];
		puts(distu+distv - 2*distl);
	}
	
	return 0;
}