Language: C++

v#include <bits/stdc++.h>
#define MOD 1000000007
int ri() {
	int n;
	scanf("%d", &n);
	return n;
}
int64_t rll() {
	long long n;
	scanf("%lld", &n);
	return n;
}

struct Hen {
	int a;
	int b;
	int color;
	int dist;
};

std::vector<Hen> a;
std::vector<std::vector<Hen> > hen;
int n, q;

std::vector<std::vector<Hen> > child;
std::vector<int> parent;
std::vector<int64_t> dist;
std::vector<int> depth;
std::vector<std::vector<int> > up;
std::vector<std::map<int, std::pair<int, int> > > qpool;
void dfs1(int i, int par, int64_t _dist, int _depth) {
	if (i) {
		int cur = par;
		up[i].push_back(cur);
		for (int pos = 0; pos < up[cur].size(); pos++) up[i].push_back(cur = up[cur][pos]);
	}
	parent[i] = par;
	dist[i] = _dist;
	depth[i] = _depth;
	for (auto j : hen[i]) {
		if (j.b == par) continue;
		child[i].push_back(j);
		dfs1(j.b, i, _dist + j.dist, _depth + 1);
	}
}
int lca(int i, int j) {
	if (i == j) return i;
	if (depth[i] > depth[j]) std::swap(i, j);
	if (!i) return 0;
	int change = depth[j] - depth[i];
	int beki = 0;
	while (change) {
		if (change & 1 << beki) {
			assert((int) up[j].size() > beki);
			j = up[j][beki];
			change -= 1 << beki;
		}
		beki++;
	}
	assert(up[i].size() == up[j].size());
	assert(depth[i] == depth[j]);
	if (i == j) return i;
	beki = 20;
	while (beki) {
		assert(up[i].size() == up[j].size());
		beki--;
		if (beki >= (int) up[i].size()) continue;
		if (up[i][beki] == up[j][beki]) continue;
		i = up[i][beki];
		j = up[j][beki];
	}
	assert(i != j);
	assert(parent[i] == parent[j]);
	return parent[i];
}
void dfs2(int i, std::vector<std::pair<int, int> > &cur) {
	// qpool[i]
	for (auto &j : qpool[i]) j.second = cur[j.first];
	for (auto &j : child[i]) {
		cur[j.color].second += j.dist;
		cur[j.color].first++;
		dfs2(j.b, cur);
		cur[j.color].first--;
		cur[j.color].second -= j.dist;
	}
}


int main() {
	n = ri(), q = ri();
	a.resize(n - 1);
	hen.resize(n);
	for (int i = 0; i + 1 < n; i++) {
		int aa = ri() - 1;
		int b = ri() - 1;
		int c = ri() - 1;
		int d = ri();
		a[i] = {aa, b, c, d};
		hen[aa].push_back(a[i]);
		hen[b].push_back({b, aa, c, d});
	}
	parent.resize(n);
	child.resize(n);
	dist.resize(n);
	depth.resize(n);
	up.resize(n);
	dfs1(0, -1, 0, 0);
	std::cerr << "dfs1 finished" << std::endl;
	qpool.resize(n);
	std::vector<int> c(q), l(q), lc(q);
	std::vector<std::pair<int, int> > v(q);
	for (int i = 0; i < q; i++) {
		c[i] = ri() - 1, l[i] = ri();
		v[i] = {ri() - 1, ri() - 1};
		lc[i] = lca(v[i].first, v[i].second);
		qpool[v[i].first][c[i]] = {0, 0};
		qpool[v[i].second][c[i]] = {0, 0};
		qpool[lc[i]][c[i]] = {0, 0};
	}
	std::vector<std::pair<int, int> > cur(n);
	dfs2(0, cur);
	for (int i = 0; i < q; i++) {
		int res = dist[v[i].first] + dist[v[i].second] - 2 * dist[lc[i]];
		res += qpool[v[i].first][c[i]].first * l[i] - qpool[v[i].first][c[i]].second;
		res += qpool[v[i].second][c[i]].first * l[i] - qpool[v[i].second][c[i]].second;
		res -= 2 * (qpool[lc[i]][c[i]].first * l[i] - qpool[lc[i]][c[i]].second);
		std::cout << res << std::endl;
	}
	
	return 0;
}


Compiler output:
a.cc:1:2: error: stray '#' in program
    1 | v#include <bits/stdc++.h>
      |  ^
a.cc:1:1: error: 'v' does not name a type
    1 | v#include <bits/stdc++.h>
      | ^
a.cc:8:1: error: 'int64_t' does not name a type
    8 | int64_t rll() {
      | ^~~~~~~
a.cc:1:1: note: 'int64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
  +++ |+#include <cstdint>
    1 | v#include <bits/stdc++.h>
a.cc:21:6: error: 'vector' in namespace 'std' does not name a template type
   21 | std::vector<Hen> a;
      |      ^~~~~~
a.cc:1:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
  +++ |+#include <vector>
    1 | v#include <bits/stdc++.h>
a.cc:22:6: error: 'vector' in namespace 'std' does not name a template type
   22 | std::vector<std::vector<Hen> > hen;
      |      ^~~~~~
a.cc:22:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
   22 | std::vector<std::vector<Hen> > hen;
      | ^~~
a.cc:25:6: error: 'vector' in namespace 'std' does not name a template type
   25 | std::vector<std::vector<Hen> > child;
      |      ^~~~~~
a.cc:25:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
   25 | std::vector<std::vector<Hen> > child;
      | ^~~
a.cc:26:6: error: 'vector' in namespace 'std' does not name a template type
   26 | std::vector<int> parent;
      |      ^~~~~~
a.cc:26:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
   26 | std::vector<int> parent;
      | ^~~
a.cc:27:6: error: 'vector' in namespace 'std' does not name a template type
   27 | std::vector<int64_t> dist;
      |      ^~~~~~
a.cc:27:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
   27 | std::vector<int64_t> dist;
      | ^~~
a.cc:28:6: error: 'vector' in namespace 'std' does not name a template type
   28 | std::vector<int> depth;
      |      ^~~~~~
a.cc:28:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
   28 | std::vector<int> depth;
      | ^~~
a.cc:29:6: error: 'vector' in namespace 'std' does not name a template type
   29 | std::vector<std::vector<int> > up;
      |      ^~~~~~
a.cc:29:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
   29 | std::vector<std::vector<int> > up;
      | ^~~
a.cc:30:6: error: 'vector' in namespace 'std' does not name a template type
   30 | std::vector<std::map<int, std::pair<int, int> > > qpool;
      |      ^~~~~~
a.cc:30:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
   30 | std::vector<std::map<int, std::pair<int, int> > > qpool;
      | ^~~
a.cc:31:27: error: 'int64_t' has not been declared
   31 | void dfs1(int i, int par, int64_t _dist, int _depth) {
      |                           ^~~~~~~
a.cc: In function 'void dfs1(int, int, int, int)':
a.cc:34:17: error: 'up' was not declared in this scope
   34 |                 up[i].push_back(cur);
      |                 ^~
a.cc:37:9: error: 'parent' was not declared in this scope
   37 |         parent[i] = par;
      |         ^~~~~~
a.cc:38:9: error: 'dist' was not declared in this scope; did you mean '_dist'?
   38 |         dist[i] = _dist;
      |         ^~~~
      |         _dist
a.cc:39:9: error: 'depth' was not declared in this scope; did you mean '_depth'?
   39 |         depth[i] = _depth;
      |         ^~~~~
      |         _depth
a.cc:40:23: error: 'hen' was not declared in this scope; did you mean 'Hen'?
   40 |         for (auto j : hen[i]) {
      |                       ^~~
      |                       Hen
a.cc:42:17: error: 'child' was not declared in this scope
   42 |                 child[i].push_back(j);
      |                 ^~~~~
a.cc: In function 'int lca(int, int)':
a.cc:48:13: error: 'depth' was not declared in this scope
   48 |         if (depth[i] > depth[j]) std::swap(i, j);
      |             ^~~~~
a.cc:48:39: error: 'swap' is not a member of 'std'
   48 |         if (depth[i] > depth[j]) std::swap(i, j);
      |                                       ^~~~
a.cc:50:22: error: 'depth' was not declared in this scope
   50 |         int change = depth[j] - depth[i];
      |                      ^~~~~
a.cc:54:38: error: 'up' was not declared in this scope
   54 |                         assert((int) up[j].size() > beki);
      |                                      ^~
a.cc:54:25: error: 'assert' was not declared in this scope
   54 |                         assert((int) up[j].size() > beki);
      |                         ^~~~~~
a.cc:1:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
  +++ |+#include <cassert>
    1 | v#include <bits/stdc++.h>
a.cc:60:16: error: 'up' was not declared in this scope
   60 |         assert(up[i].size() == up[j].size());
      |                ^~
a.cc:60:9: error: 'assert' was not declared in this scope
   60 |         assert(up[i].size() == up[j].size());
      |         ^~~~~~
a.cc:60:9: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
a.cc:73:16: error: 'parent' was not declared in this scope
   73 |         assert(parent[i] == parent[j]);
      |                ^~~~~~
a.cc: At global scope:
a.cc:76:18: error: 'std::vector' has not been declared
   76 | void dfs2(int i, std::vector<std::pair<int, int> > &cur) {
      |                  ^~~
a.cc:76:29: error: expected ',' or '...' before '<' token
   76 | void dfs2(int i, std::vector<std::pair<int, int> > &cur) {
      |                             ^
a.cc: In function 'void dfs2(int, int)':
a.cc:78:24: error: 'qpool' was not declared in this scope
   78 |         for (auto &j : qpool[i]) j.second = cur[j.first];
      |                        ^~~~~
a.cc:78:45: error: 'cur' was not declared in this scope
   78 |         for (auto &j : qpool[i]) j.second = cur[j.first];
      |                                             ^~~
a.cc:79:24: error: 'child' was not declared in this scope
   79 |         for (auto &j : child[i]) {
      |                        ^~~~~
a.cc:80:17: error: 'cur' was not declared in this scope
   80 |                 cur[j.color].second += j.dist;
      |                 ^~~
a.cc: In function 'int main()':
a.cc:90:13: error: 'ri' was not declared in this scope
   90 |         n = ri(), q = ri();
      |             ^~
a.cc:91:9: error: 'a' was not declared in this scope
   91 |         a.resize(n - 1);
      |         ^
a.cc:92:9: error: 'hen' was not declared in this scope; did you mean 'Hen'?
   92 |         hen.resize(n);
      |         ^~~
      |         Hen
a.cc:102:9: error: 'parent' was not declared in this scope
  102 |         parent.resize(n);
      |         ^~~~~~
a.cc:103:9: error: 'child' was not declared in this scope
  103 |         child.resize(n);
      |         ^~~~~
a.cc:104:9: error: 'dist' was not declared in this scope
  104 |         dist.resize(n);
      |         ^~~~
a.cc:105:9: error: 'depth' was not declared in this scope
  105 |         depth.resize(n);
      |         ^~~~~
a.cc:106:9: error: 'up' was not declared in this scope
  106 |         up.resize(n);
      |         ^~
a.cc:108:14: error: 'cerr' is not a member of 'std'
  108 |         std::cerr << "dfs1 finished" << std::endl;
      |              ^~~~
a.cc:1:1: note: 'std::cerr' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
  +++ |+#include <iostream>
    1 | v#include <bits/stdc++.h>
a.cc:108:46: error: 'endl' is not a member of 'std'
  108 |         std::cerr << "dfs1 finished" << std::endl;
      |                                              ^~~~
a.cc:1:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
  +++ |+#include <ostream>
    1 | v#include <bits/stdc++.h>
a.cc:109:9: error: 'qpool' was not declared in this scope
  109 |         qpool.resize(n);
      |         ^~~~~
a.cc:110:14: error: 'vector' is not a member of 'std'
  110 |         std::vector<int> c(q), l(q), lc(q);
      |              ^~~~~~
a.cc:110:14: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
a.cc:110:21: error: expected primary-expression before 'int'
  110 |         std::vector<int> c(q), l(q), lc(q);
      |                     ^~~
a.cc:111:14: error: 'vector' is not a member of 'std'
  111 |         std::vector<std::pair<int, int> > v(q);
      |              ^~~~~~
a.cc:111:14: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
a.cc:111:26: error: 'pair' is not a member of 'std'
  111 |         std::vector<std::pair<int, int> > v(q);
      |                          ^~~~
a.cc:1:1: note: 'std::pair' is defined in header '<utility>'; this is probably fixable by adding '#include <utility>'
  +++ |+#include <utility>
    1 | v#include <bits/stdc++.h>
a.cc:111:31: error: expected primary-expression before 'int'
  111 |         std::vector<std::pair<int, int> > v(q);
      |                               ^~~
a.cc:113:17: error: 'c' was not declared in this scope
  113 |                 c[i] = ri() - 1, l[i] = ri();
      |                 ^
a.cc:113:34: error: 'l' was not declared in this scope
  113 |                 c[i] = ri() - 1, l[i] = ri();
      |                                  ^
a.cc:114:17: error: 'v' was not declared in this scope
  114 |                 v[i] = {ri() - 1, ri() - 1};
      |                 ^
a.cc:115:17: error: 'lc' was not declared in this scope; did you mean 'lca'?
  115 |                 lc[i] = lca(v[i].first, v[i].second);
      |                 ^~
      |                 lca
a.cc:120:14: error: 'vector' is not a member of 'std'
  120 |         std::vector<std::pair<int, int> > cur(n);
      |              ^~~~~~
a.cc:120:14: note: 'std::vector' is defined in header '<vector>'; this is probably fixable b