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)

// ---------- begin Lowest Common Ancestor ----------
struct LCA {
    n: usize,
    g: Vec<Vec<usize>>,
    size: Vec<usize>,
    index: Vec<usize>,
    path_root: Vec<usize>,
    path_parent: Vec<usize>,
}

impl LCA {
    pub fn new(n: usize) -> LCA {
        LCA {
            n: n,
            g: vec![vec![]; n],
            size: vec![],
            index: vec![],
            path_root: vec![],
            path_parent: vec![],
        }
    }
    pub fn add_edge(&mut self, a: usize, b: usize) {
        assert!(a < self.n && b < self.n);
        self.g[a].push(b);
        self.g[b].push(a);
    }
    pub fn build(&mut self, root: usize) {
        assert!(root < self.n);
        let mut stack = vec![];
        let mut q = vec![];
        stack.push((root, root));
        while let Some((v, p)) = stack.pop() {
            q.push(v);
            for i in 0..self.g[v].len() {
                if self.g[v][i] == p {
                    self.g[v].swap_remove(i);
                    break;
                }
            }
            for &u in &self.g[v] {
                stack.push((u, v));
            }
        }
        assert!(q.len() == self.n);
        self.size = vec![1; self.n];
        for &v in q.iter().rev() {
            for &u in &self.g[v] {
                self.size[v] += self.size[u];
            }
        }
        self.index = vec![0; self.n];
        self.path_root = vec![root; self.n];
        self.path_parent = vec![root; self.n];
        self.dfs(root, &mut 0);
    }
    fn dfs(&mut self, v: usize, k: &mut usize) {
        self.index[v] = *k;
        *k += 1;
        if self.g[v].len() == 0 {
            return;
        }
        for i in 1..self.g[v].len() {
            if self.size[self.g[v][0]] < self.size[self.g[v][i]] {
                self.g[v].swap(0, i);
            }
        }
        let u = self.g[v][0];
        self.path_root[u] = self.path_root[v];
        self.path_parent[u] = self.path_parent[v];
        self.dfs(u, k);
        for i in 1..self.g[v].len() {
            let u = self.g[v][i];
            self.path_root[u] = u;
            self.path_parent[u] = v;
            self.dfs(u, k);
        }
    }
    fn query(&self, mut a: usize, mut b: usize) -> usize {
        let path = &self.path_root;
        let parent = &self.path_parent;
        let index = &self.index;
        while path[a] != path[b] {
            if index[a] < index[b] {
                b = parent[b];
            } else {
                a = parent[a];
            }
        }
        if index[a] < index[b] {a} else {b}
    }
}
// ---------- end Lowest Common Ancestor ----------
//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

//

use std::io::Write;

fn run() {
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    input! {
        n: usize,
        q: usize,
        e: [(usize1, usize1, usize1, u32); n - 1],
        ask: [(usize1, u32, usize1, usize1); q],
    }
    let mut g = vec![vec![]; n];
    let mut lca = LCA::new(n);
    for &(a, b, c, d) in &e {
        g[a].push((b, c, d));
        g[b].push((a, c, d));
        lca.add_edge(a, b);
    }
    lca.build(0);
    let mut eval = vec![vec![]; n];
    for &(x, y, u, v) in &ask {
        eval[u].push((x, y));
        eval[v].push((x, y));
        eval[lca.query(u, v)].push((x, y));
    }
    enum Operation {
        ADD(usize, u32),
        SUB(usize, u32),
        DFS(usize, usize),
    }
    let mut sum = vec![0; n];
    let mut cnt = vec![0; n];
    let mut sum_all = 0;
    let mut memo = std::collections::BTreeMap::<(usize, usize, u32), u32>::new();
    let mut stack = vec![Operation::DFS(0, 0)];
    while let Some(op) = stack.pop() {
        match op {
            Operation::ADD(c, w) => {
                sum[c] += w;
                cnt[c] += 1;
                sum_all += w;
            },
            Operation::SUB(c, w) => {
                sum[c] -= w;
                cnt[c] -= 1;
                sum_all -= w;
            },
            Operation::DFS(v, p) => {
                for &(c, d) in &eval[v] {
                    let ans = sum_all - sum[c] + cnt[c] * d;
                    memo.insert((v, c, d), ans);
                }
                for &(u, c, d) in &g[v] {
                    if u == p {
                        continue;
                    }
                    stack.push(Operation::SUB(c, d));
                    stack.push(Operation::DFS(u, v));
                    stack.push(Operation::ADD(c, d));
                }
            },
        }
    }
    for (x, y, u, v) in ask {
        let mut ans = 0;
        ans += *memo.get(&(u, x, y)).unwrap();
        ans += *memo.get(&(v, x, y)).unwrap();
        ans -= 2 * *memo.get(&(lca.query(u, v), x, y)).unwrap();
        writeln!(out, "{}", ans).ok();
    }
}

fn main() {
    run();
}