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 (GCC 5.4.1) (judge verdict: Accepted)

#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<inttypes.h>

typedef int32_t i32;

#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define ALLOC(size,type) ((type*)calloc((size),sizeof(type)))

typedef struct count_sum {
  i32 sum;
  i32 cnt;
} node;

typedef node persistent_array_value;

typedef struct persistent_array_node {
  persistent_array_value val;
  i32 size;
  struct persistent_array_node *left;
  struct persistent_array_node *right;
} persistent_array;

i32 get_size (persistent_array *t) {
  return t == NULL ? 0 : t->size;
}

persistent_array* build_persistent_array (i32 n) {
  if (n <= 0) return NULL;
  persistent_array *t = ALLOC (1, persistent_array);
  i32 m = n / 2;
  t->size = n;
  t->left = build_persistent_array (m);
  t->right = build_persistent_array (n - m - 1);
  return t;
}

persistent_array_value get_at (persistent_array *t, i32 x) {
  i32 l = get_size (t->left);
  if (x == l) {
    return t->val;
  }
  if (x < l) {
    return get_at (t->left, x);
  } else {
    return get_at (t->right, x - l - 1);
  }
}

persistent_array* assign_at (persistent_array *t, i32 x, persistent_array_value val) {
  persistent_array *s = ALLOC (1, persistent_array);
  *s = *t;
  i32 l = get_size (t->left);
  if (l == x) {
    s->val = val;
  } else if (x < l) {
    s->left = assign_at (s->left, x, val);
  } else {
    s->right = assign_at (s->right, x - l - 1, val);
  }
  return s;
}

typedef struct graph_edge {
  i32 v, c, d, next;
} graph_edge;

typedef struct graph {
  graph_edge *edge;
  i32 *start;
  i32 size;
  i32 p;
} graph;

graph* new_graph (i32 v, i32 e) {
  graph *g = ALLOC (1, graph);
  g->edge = ALLOC (e, graph_edge);
  g->start = ALLOC (v, i32);
  g->size = v;
  g->p = 0;
  for (i32 i = 0; i < v; ++i) {
    g->start[i] = -1;
  }
  return g;
}

void add_edge (graph *g, i32 a, i32 b, i32 c, i32 d) {
  g->edge[g->p] = (graph_edge) {b, c, d, g->start[a]};
  g->start[a] = g->p++;
}

typedef struct LCA_node {
  i32 vertex;
  i32 depth;
} LCA_node;

LCA_node min_LCA_node (LCA_node a, LCA_node b) {
  return a.depth <= b.depth ? a : b;
}

typedef struct lowest_common_ancestor_structure {
  i32 *left;
  i32 *right;
  LCA_node *s;
  i32 size;
} LCA;

void fill_lca (i32 v, i32 d, graph *g, LCA *lca, i32 *index, uint8_t *used) {
  used[v] = 1;
  lca->left[v] = *index;
  lca->right[v] = *index;
  lca->s[*index + lca->size] = (LCA_node) {v, d};
  *index += 1;
  for (i32 p = g->start[v]; p != -1; p = g->edge[p].next) {
    i32 u = g->edge[p].v;
    if (used[u]) continue;
    fill_lca (u, d + 1, g, lca, index, used);
    lca->right[v] = *index;
    lca->s[*index + lca->size] = (LCA_node) {v, d};
    *index += 1;
  }
}

LCA* build_LCA (graph *g, i32 root) {
  const i32 n = g->size;
  i32 size = 1;
  while (size < 2 * n - 1) size *= 2;
  LCA *lca = ALLOC (1, LCA);
  lca->left = ALLOC (n, i32);
  lca->right = ALLOC (n, i32);
  lca->s = ALLOC (2 * size, LCA_node);
  lca->size = size;
  uint8_t *used = ALLOC (n, uint8_t);
  i32 index = 0;
  fill_lca (root, 0, g, lca, &index, used);
  for (i32 i = size - 1; i >= 1; --i) {
    lca->s[i] = min_LCA_node (lca->s[2 * i], lca->s[2 * i + 1]);
  }
  free (used);
  return lca;
}

i32 query (LCA *lca, i32 v, i32 u) {
  i32 l = MIN (lca->left[v], lca->left[u]);
  i32 r = MAX (lca->right[v], lca->right[u]) + 1;
  LCA_node ans = {-1, lca->size + 1};
  for (l += lca->size, r += lca->size; l < r; l >>= 1, r >>= 1) {
    if (l & 1) ans = min_LCA_node (ans, lca->s[l++]);
    if (r & 1) ans = min_LCA_node (ans, lca->s[--r]);
  }
  return ans.vertex;
}

void euler_tour (i32 v, i32 dsum, persistent_array *buf, persistent_array **a, i32 *depth, graph *g, uint8_t *used) {
  used[v] = 1;
  a[v] = buf;
  depth[v] = dsum;
  for (i32 p = g->start[v]; p != -1; p = g->edge[p].next) {
    i32 u = g->edge[p].v;
    if (used[u]) continue;
    i32 c = g->edge[p].c;
    i32 d = g->edge[p].d;
    node t = get_at (buf, c);
    euler_tour (u, dsum + d, assign_at (buf, c, (node){t.sum + d, t.cnt + 1}), a, depth, g, used);
  }
}

i32 eval (i32 v, i32 *depth, persistent_array **a, i32 x, i32 y) {
  node t = get_at (a[v], x);
  return depth[v] - t.sum + t.cnt * y;
}

void run (void) {
  i32 n, q;
  scanf ("%" SCNi32 "%" SCNi32, &n, &q);
  graph *g = new_graph (n, 2 * n);
  for (i32 i = 0; i < n - 1; ++i) {
    i32 a, b, c, d;
    scanf ("%" SCNi32 "%" SCNi32 "%" SCNi32 "%" SCNi32, &a, &b, &c, &d);
    a--; b--;
    add_edge (g, a, b, c, d);
    add_edge (g, b, a, c, d);
  }
  const i32 root = 0;
  LCA *lca = build_LCA (g, root);
  persistent_array **a = ALLOC (n, persistent_array *);
  persistent_array *buf = build_persistent_array (n);
  i32 *depth = ALLOC (n, i32);
  uint8_t *used = ALLOC (n, uint8_t);
  euler_tour (root, 0, buf, a, depth, g, used);
  while (q--) {
    i32 x, y, u, v;
    scanf ("%" SCNi32 "%" SCNi32 "%" SCNi32 "%" SCNi32, &x, &y, &u, &v);
    u--; v--;
    i32 p = query (lca, u, v);
    i32 ans = 0;
    ans += eval (u, depth, a, x, y);
    ans += eval (v, depth, a, x, y);
    ans -= 2 * eval (p, depth, a, x, y);
    printf ("%" PRIi32 "\n", ans);
  }
}

int main (void) {
  run();
  return 0;
}