Language: C++

#include <bits/stdc++.h>
using namespace std;

#define for_(i,a,b) for(int i=(a);i<(b);++i)
#define for_rev(i,a,b) for(int i=(a);i>=(b);--i)

typedef long long lint;
typedef pair< int, int > pii;

// Segment Tree with Lazy Evaluate
template< typename DATA >
class SegmentTreeLazy {
private:
	int size__;
	vector< DATA > data, lazy;
	
	inline int left_t(int k) {
		return (k << 1) + 1;
	}
	
	inline int right_t(int k) {
		return (k << 1) + 2;
	}
	
	inline int center(int l, int r) {
		return (l + r) >> 1;
	}
	
public:
	SegmentTreeLazy() {}
	
	SegmentTreeLazy(int n, DATA ini, DATA lini) {
		for (size__ = 1; size__ < n; size__ <<= 1);
		data.assign(2 * size__ - 1, ini);
		lazy.assign(2 * size__ - 1, lini);
	}
	
	DATA calc(DATA d1, DATA d2) {
		return d1 + d2;
	}
	
	void lazyCalc(int k, DATA val) {
		lazy[k] += val;
	}
	
	inline void lazyEval(int l, int r, int k) {
		data[k] += lazy[k] * (r - l);
		
		if (k < size__ - 1) {
			lazyCalc(left_t(k), lazy[k]);
			lazyCalc(right_t(k), lazy[k]);
		}
		
		lazy[k] = DATA(0);
	}
	
	inline void updateAt(int k) {
		data[k] = calc(data[left_t(k)], data[right_t(k)]);
	}
	
	void update(int a, int b, DATA x) {
		update(a, b, x, 0, 0, size__);
	}
	
	void update(int a, int b, DATA x, int k, int l, int r) {
		lazyEval(l, r, k);
		
		if (r <= a || b <= l) return;
		if (a <= l && r <= b) {
			lazyCalc(k, x);
			lazyEval(l, r, k);
			return;
		}
		
		update(a, b, x, left_t(k), l, center(l, r));
		update(a, b, x, right_t(k), center(l, r), r);
		updateAt(k);
	}
	
	DATA query(int a, int b) {
		return query(a, b, 0, 0, size__);
	}
	
	DATA query(int a, int b, int k, int l, int r) {
		lazyEval(l, r, k);
		
		if (r <= a || b <= l) return 0;
		if (a <= l && r <= b) return data[k];
		DATA res =  calc(query(a, b, left_t(k), l, center(l, r)),
						 query(a, b, right_t(k), center(l, r), r));
		
		updateAt(k);
		return res;
	}
	
	int size() {
		return size__;
	}
};

int N, Q;
vector< int > edges[150010];

const int OPEN = 0, CLOSE = 1, NORMAL = 0, REV = 1;
int depth[150010], parent[150010][30], index[150010][2][2];
	
SegmentTreeLazy< lint > add_stl, sub_stl;

void makeLCA() {
	memset(index, -1, sizeof(index));
	memset(depth, 0, sizeof(depth));
	
	int open_cnt = 0, close_cnt = 0;
	stack< pii > st; st.push(pii(0, -1));
	
	while (!st.empty()) {
		pii var = st.top(); st.pop();
		int v = var.first, p = var.second;
		
		if (p != -2) {
			parent[v][0] = p;
			depth[v] = (p < 0) ? 0 : depth[p] + 1;
		}
		
		if (index[v][OPEN][NORMAL] < 0) {
			index[v][OPEN][NORMAL] = open_cnt; ++open_cnt;
			index[v][OPEN][REV] = close_cnt;
			
			st.push(pii(v, -2));
			for (int u : edges[v]) st.push(pii(u, v));
		} else {
			index[v][CLOSE][NORMAL] = close_cnt; ++close_cnt;
			index[v][CLOSE][REV] = open_cnt;
		}
	}
	
	for_(k,0,24) for_(v,0,N) {
		if (parent[v][k] < 0) parent[v][k + 1] = -1;
		else parent[v][k + 1] = parent[ parent[v][k] ][k];
	}
}

int getLCA(int a, int b) {
	if (depth[a] > depth[b]) swap(a, b);
	
	for_(k,0,25) if ((depth[b] - depth[a]) >> k & 1) b = parent[b][k];
	
	if (a == b) return a;
	
	for_rev(k,24,0) {
		if (parent[a][k] != parent[b][k]) {
			a = parent[a][k];
			b = parent[b][k];
		}
	}
	
	return parent[a][0];
}

int getCost(int v) {
	int A = index[v][OPEN][NORMAL] + 1, S = index[v][OPEN][REV];
	return add_stl.query(0, A) - sub_stl.query(0, S);
}

int main() {
	cin >> N >> Q;
	
	for_(i,0,N-1) {
		int a, b;
		scanf("%d %d", &a, &b);
		edges[a].push_back(b);
	}
	
	for_(i,0,N) sort(edges[i].begin(), edges[i].end(), greater< int >());
	
	makeLCA();
	
	add_stl = SegmentTreeLazy< lint >(N, 0, 0);
	sub_stl = SegmentTreeLazy< lint >(N, 0, 0);
		
	for_(i,0,Q) {
		int q, a, b;
		scanf("%d %d %d", &q, &a, &b);
		
		if (q) {
			int oL = index[a][OPEN][NORMAL] + 1, oR = index[a][CLOSE][REV];
			int cL = index[a][OPEN][REV], cR = index[a][CLOSE][NORMAL];
			
			add_stl.update(oL, oR, b);
			sub_stl.update(cL, cR, b);
		} else {
			int p = getLCA(a, b);
			cout << getCost(a) + getCost(b) - 2 * getCost(p) << endl;
		}
	}
}

Compiler output:
a.cc:105:58: error: 'int index [150010][2][2]' redeclared as different kind of entity
  105 | int depth[150010], parent[150010][30], index[150010][2][2];
      |                                                          ^
In file included from /usr/include/string.h:462,
                 from /usr/include/c++/14/cstring:43,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:121,
                 from a.cc:1:
/usr/include/strings.h:50:20: note: previous declaration 'const char* index(const char*, int)'
   50 | extern const char *index (const char *__s, int __c)
      |                    ^~~~~
a.cc: In function 'void makeLCA()':
a.cc:110:27: error: ISO C++ forbids applying 'sizeof' to an expression of function type [-fpermissive]
  110 |         memset(index, -1, sizeof(index));
      |                           ^~~~~~~~~~~~~
a.cc:110:15: error: cannot resolve overloaded function 'index' based on conversion to type 'void*'
  110 |         memset(index, -1, sizeof(index));
      |         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:125:26: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  125 |                 if (index[v][OPEN][NORMAL] < 0) {
      |                          ^
a.cc:126:30: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  126 |                         index[v][OPEN][NORMAL] = open_cnt; ++open_cnt;
      |                              ^
a.cc:127:30: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  127 |                         index[v][OPEN][REV] = close_cnt;
      |                              ^
a.cc:132:30: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  132 |                         index[v][CLOSE][NORMAL] = close_cnt; ++close_cnt;
      |                              ^
a.cc:133:30: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  133 |                         index[v][CLOSE][REV] = open_cnt;
      |                              ^
a.cc: In function 'int getCost(int)':
a.cc:161:22: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  161 |         int A = index[v][OPEN][NORMAL] + 1, S = index[v][OPEN][REV];
      |                      ^
a.cc:162:55: error: 'S' was not declared in this scope
  162 |         return add_stl.query(0, A) - sub_stl.query(0, S);
      |                                                       ^
a.cc: In function 'int main()':
a.cc:186:39: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  186 |                         int oL = index[a][OPEN][NORMAL] + 1, oR = index[a][CLOSE][REV];
      |                                       ^
a.cc:187:39: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  187 |                         int cL = index[a][OPEN][REV], cR = index[a][CLOSE][NORMAL];
      |                                       ^
a.cc:189:44: error: 'oR' was not declared in this scope; did you mean 'oL'?
  189 |                         add_stl.update(oL, oR, b);
      |                                            ^~
      |                                            oL
a.cc:190:44: error: 'cR' was not declared in this scope; did you mean 'cL'?
  190 |                         sub_stl.update(cL, cR, b);
      |                                            ^~
      |                                            cL