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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

#[derive(Clone)]
struct Query {
    q_idx: usize,
    color: usize,
    cost: i64,
    coef: i64,
}

fn main() {
    let v = read_vec::<usize>();
    let (n, q) = (v[0], v[1]);

    let mut g = vec![Vec::new(); n];
    let mut color_dict = HashMap::new();
    let mut dist_dict = HashMap::new();
    for i in 0..n - 1 {
        let v = read_vec::<usize>();
        let (a, b, c, d) = (v[0] - 1, v[1] - 1, v[2], v[3] as i64);
        g[b].push(a);
        g[a].push(b);
        color_dict.insert((a, b), c);
        color_dict.insert((b, a), c);
        dist_dict.insert((a, b), d);
        dist_dict.insert((b, a), d);
    }
    let lca = LCA::new(&g);
    let mut tree: Vec<Vec<usize>> = vec![Vec::new(); n];
    let mut parent_dict: HashMap<usize, usize> = HashMap::new();
    make_tree(0, n, &g, &mut tree, &mut parent_dict);
    let mut topological_sorted_indexes = vec![0];
    topological_dfs(0, &tree, &mut topological_sorted_indexes);

    let mut dist_from_root = vec![0i64; n];

    for &ti in topological_sorted_indexes.iter().skip(1) {
        let parent = parent_dict[&ti];
        dist_from_root[ti] = dist_from_root[parent] + dist_dict[&(ti, parent)];
    }
    let mut answers = vec![0i64; q];
    let mut queries = vec![vec![]; n];
    for q_idx in 0..q {
        let v = read_vec::<usize>();
        let (color, cost, u, v) = (v[0], v[1] as i64, v[2] - 1, v[3] - 1);
        let common = lca.lca(u, v);
        queries[u].push(Query {
            q_idx: q_idx,
            color: color,
            cost: cost,
            coef: 1,
        });
        queries[v].push(Query {
            q_idx: q_idx,
            color: color,
            cost: cost,
            coef: 1,
        });
        queries[common].push(Query {
            q_idx: q_idx,
            color: color,
            cost: cost,
            coef: -2,
        });
        answers[q_idx] = dist_from_root[u] + dist_from_root[v] - 2 * dist_from_root[common];
    }
    let mut count = vec![0; n];
    let mut sum = vec![0; n];
    dfs(
        0,
        &tree,
        &color_dict,
        &dist_dict,
        &queries,
        &mut count,
        &mut sum,
        &mut answers,
    );
    for ans in answers {
        println!("{}", ans);
    }
}

fn dfs(
    cur: usize,
    tree: &Vec<Vec<usize>>,
    color_dict: &HashMap<(usize, usize), usize>,
    dist_dict: &HashMap<(usize, usize), i64>,
    queries: &Vec<Vec<Query>>,
    count: &mut Vec<i64>,
    sum: &mut Vec<i64>,
    answers: &mut Vec<i64>,
) {
    for ref q in queries[cur].iter() {
        answers[q.q_idx] += (q.cost * count[q.color] - sum[q.color]) * q.coef;
    }
    let childlen = tree[cur].iter().cloned().collect::<Vec<_>>();
    for child in childlen {
        let color = color_dict[&(cur, child)];
        count[color] += 1;
        sum[color] += dist_dict[&(cur, child)];
        dfs(
            child, tree, color_dict, dist_dict, queries, count, sum, answers,
        );
        count[color] -= 1;
        sum[color] -= dist_dict[&(cur, child)];
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

pub struct LCA {
    pub depth: Vec<usize>,
    pub parent: Vec<Vec<Option<usize>>>,
}

pub struct Tree {
    pub root: usize,
    pub parent: Vec<Option<usize>>,
    pub childs: Vec<Vec<usize>>,
}

impl LCA {
    pub fn new(g: &[Vec<usize>]) -> LCA {
        LCA::with_root(0, g)
    }

    pub fn with_root(root: usize, g: &[Vec<usize>]) -> LCA {
        fn dfs(
            i: usize,
            p: Option<usize>,
            d: usize,
            g: &[Vec<usize>],
            depth: &mut [usize],
            parent: &mut [Vec<Option<usize>>],
        ) {
            parent[i][0] = p;
            depth[i] = d;

            for &t in &g[i] {
                if Some(t) != p {
                    dfs(t, Some(i), d + 1, g, depth, parent);
                }
            }
        }
        let n = g.len();
        let l2 = (1..).find(|i| 1usize << i > n).unwrap();
        let mut depth = vec![0; n];
        let mut parent = vec![vec![None; l2 + 1]; n];

        dfs(root, None, 0, &g, &mut depth, &mut parent);

        for i in 1..l2 + 1 {
            for j in 0..n {
                if let Some(p) = parent[j][i - 1] {
                    parent[j][i] = parent[p][i - 1];
                }
            }
        }

        LCA {
            depth: depth,
            parent: parent,
        }
    }

    pub fn lca(&self, mut a: usize, mut b: usize) -> usize {
        use std::mem::swap;
        if self.depth[b] < self.depth[a] {
            swap(&mut a, &mut b);
        }

        while self.depth[a] != self.depth[b] {
            b = self.parent[b][(self.depth[b] - self.depth[a]).trailing_zeros() as usize].unwrap();
        }

        if a == b {
            return a;
        }

        for i in (0..self.parent[0].len()).rev() {
            if self.parent[a][i] != self.parent[b][i] {
                a = self.parent[a][i].unwrap();
                b = self.parent[b][i].unwrap();
            }
        }
        self.parent[a][0].unwrap()
    }
}

fn make_tree(
    cur_idx: usize,
    parent_idx: usize,
    edges: &Vec<Vec<usize>>,
    tree: &mut Vec<Vec<usize>>,
    parent_dict: &mut HashMap<usize, usize>,
) {
    for child_idx in edges[cur_idx].iter() {
        if *child_idx == parent_idx {
            continue;
        }
        tree[cur_idx].push(*child_idx);
        parent_dict.insert(*child_idx, cur_idx);
        make_tree(*child_idx, cur_idx, edges, tree, parent_dict);
    }
}

fn topological_dfs(cur_idx: usize, tree: &Vec<Vec<usize>>, result: &mut Vec<usize>) {
    for child_idx in tree[cur_idx].iter() {
        result.push(*child_idx);
        topological_dfs(*child_idx, tree, result);
    }
}