#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
template<class T, int SZ> struct LazySegTree {
T sum[2*SZ], mn[2*SZ], lazy[2*SZ]; // set SZ to a power of 2
LazySegTree() {
memset (sum,0,sizeof sum);
memset (mn,0,sizeof mn);
memset (lazy,0,sizeof lazy);
}
void push(int ind, int L, int R) {
sum[ind] += (R-L+1)*lazy[ind];
mn[ind] += lazy[ind];
if (L != R) lazy[2*ind] += lazy[ind], lazy[2*ind+1] += lazy[ind];
lazy[ind] = 0;
}
void pull(int ind) {
sum[ind] = sum[2*ind]+sum[2*ind+1];
mn[ind] = min(mn[2*ind],mn[2*ind+1]);
}
void build() {
for(int i = SZ-1;i>=0; i--){
pull(i);
}
}
T qsum(int lo, int hi, int ind = 1, int L = 0, int R = SZ-1) {
push(ind,L,R);
if (lo > R || L > hi) return 0;
if (lo <= L && R <= hi) return sum[ind];
int M = (L+R)/2;
return qsum(lo,hi,2*ind,L,M) + qsum(lo,hi,2*ind+1,M+1,R);
}
T qmin(int lo, int hi, int ind = 1, int L = 0, int R = SZ-1) {
push(ind,L,R);
if (lo > R || L > hi) return INF;
if (lo <= L && R <= hi) return mn[ind];
int M = (L+R)/2;
return min(qmin(lo,hi,2*ind,L,M), qmin(lo,hi,2*ind+1,M+1,R));
}
void upd(int lo, int hi, long long inc, int ind = 1, int L = 0, int R = SZ-1) {
push(ind,L,R);
if (hi < L || R < lo) return;
if (lo <= L && R <= hi) {
lazy[ind] = inc;
push(ind,L,R);
return;
}
int M = (L+R)/2;
upd(lo,hi,inc,2*ind,L,M); upd(lo,hi,inc,2*ind+1,M+1,R);
pull(ind);
}
};
vector<vector<int>> graph;
template <class T, int V>
class HeavyLight {
int parent[V], heavy[V], depth[V];
int root[V], treePos[V];
LazySegTree<T, V> tree;
template <class G>
int dfs(const G& graph, int v) {
int size = 1, maxSubtree = 0;
for (int u : graph[v]) if (u != parent[v]) {
parent[u] = v;
depth[u] = depth[v] + 1;
int subtree = dfs(graph, u);
if (subtree > maxSubtree) heavy[v] = u, maxSubtree = subtree;
size += subtree;
}
return size;
}
template <class BinaryOperation>
void processPath(int u, int v, BinaryOperation op) {
for (; root[u] != root[v]; v = parent[root[v]]) {
if (depth[root[u]] > depth[root[v]]) swap(u, v);
op(treePos[root[v]], treePos[v] + 1);
}
if (depth[u] > depth[v]) swap(u, v);
op(treePos[u], treePos[v] + 1);
}
public:
template <class G>
void init(const G& graph) {
int n = graph.size();
fill_n(heavy, n, -1);
parent[0] = -1;
depth[0] = 0;
dfs(graph, 0);
for (int i = 0, currentPos = 0; i < n; ++i)
if (parent[i] == -1 || heavy[parent[i]] != i)
for (int j = i; j != -1; j = heavy[j]) {
root[j] = i;
treePos[j] = currentPos++;
}
tree.build();
}
void set(int v, const T& value) {
tree.set(treePos[v], value);
}
void modifyPath(int u, int v, const T& value) {
processPath(u, v, [this, &value](int l, int r) { tree.upd(l, r, value); });
}
long long queryPath(int u, int v) {
long long res =0;
processPath(u, v, [this, &res](int l, int r) { res += (tree.qsum(l, r)); });
return res;
}
};
HeavyLight<int, 1<<17> H;
int main() {
int n = 8;
graph.resize(n+1);
for(int i = 0; i<n-1; i++) {
int a,b; cin >> a >> b;
//a--; b--;
graph[a].push_back(b); graph[b].push_back(a);
}
H.init(graph);
H.modifyPath(4, 5, 1);
cout << H.queryPath(4, 6) << endl;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNvbnN0IGludCBJTkYgPSAxZTk7Cgp0ZW1wbGF0ZTxjbGFzcyBULCBpbnQgU1o+IHN0cnVjdCBMYXp5U2VnVHJlZSB7CiAgICBUIHN1bVsyKlNaXSwgbW5bMipTWl0sIGxhenlbMipTWl07IC8vIHNldCBTWiB0byBhIHBvd2VyIG9mIDIKCiAgICBMYXp5U2VnVHJlZSgpIHsKICAgICAgICBtZW1zZXQgKHN1bSwwLHNpemVvZiBzdW0pOwogICAgICAgIG1lbXNldCAobW4sMCxzaXplb2YgbW4pOwogICAgICAgIG1lbXNldCAobGF6eSwwLHNpemVvZiBsYXp5KTsKICAgIH0KCiAgICB2b2lkIHB1c2goaW50IGluZCwgaW50IEwsIGludCBSKSB7CiAgICAgICAgc3VtW2luZF0gKz0gKFItTCsxKSpsYXp5W2luZF07CiAgICAgICAgbW5baW5kXSArPSBsYXp5W2luZF07CiAgICAgICAgaWYgKEwgIT0gUikgbGF6eVsyKmluZF0gKz0gbGF6eVtpbmRdLCBsYXp5WzIqaW5kKzFdICs9IGxhenlbaW5kXTsKICAgICAgICBsYXp5W2luZF0gPSAwOwogICAgfQoKICAgIHZvaWQgcHVsbChpbnQgaW5kKSB7CiAgICAgICAgc3VtW2luZF0gPSBzdW1bMippbmRdK3N1bVsyKmluZCsxXTsKICAgICAgICBtbltpbmRdID0gbWluKG1uWzIqaW5kXSxtblsyKmluZCsxXSk7CiAgICB9CgogICAgdm9pZCBidWlsZCgpIHsKICAgICAgICBmb3IoaW50IGkgPSBTWi0xO2k+PTA7IGktLSl7CiAgICAgICAgICAgIHB1bGwoaSk7CiAgICAgICAgfQogICAgfQoKICAgIFQgcXN1bShpbnQgbG8sIGludCBoaSwgaW50IGluZCA9IDEsIGludCBMID0gMCwgaW50IFIgPSBTWi0xKSB7CiAgICAgICAgcHVzaChpbmQsTCxSKTsKICAgICAgICBpZiAobG8gPiBSIHx8IEwgPiBoaSkgcmV0dXJuIDA7CiAgICAgICAgaWYgKGxvIDw9IEwgJiYgUiA8PSBoaSkgcmV0dXJuIHN1bVtpbmRdOwoKICAgICAgICBpbnQgTSA9IChMK1IpLzI7CiAgICAgICAgcmV0dXJuIHFzdW0obG8saGksMippbmQsTCxNKSArIHFzdW0obG8saGksMippbmQrMSxNKzEsUik7CiAgICB9CgogICAgVCBxbWluKGludCBsbywgaW50IGhpLCBpbnQgaW5kID0gMSwgaW50IEwgPSAwLCBpbnQgUiA9IFNaLTEpIHsKICAgICAgICBwdXNoKGluZCxMLFIpOwogICAgICAgIGlmIChsbyA+IFIgf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