{"problem":{"name":"Ex - Sum of Min of Length","description":{"content":"You are given a tree with $N$ vertices. The vertices are numbered $1$ to $N$, and the $i$\\-th edge connects vertex $A_i$ and vertex $B_i$. Let $d(x,y)$ denote the distance between vertex $x$ and $y$ i","description_type":"Markdown"},"platform":"AtCoder","limit":{"time_limit":3000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"abc298_h"},"statements":[{"statement_type":"Markdown","content":"You are given a tree with $N$ vertices. The vertices are numbered $1$ to $N$, and the $i$\\-th edge connects vertex $A_i$ and vertex $B_i$.\nLet $d(x,y)$ denote the distance between vertex $x$ and $y$ in this tree. Here, the distance between vertex $x$ and $y$ is the number of edges on the shortest path from vertex $x$ to $y$.\nAnswer $Q$ queries in order. The $i$\\-th query is as follows.\n\n*   You are given integers $L_i$ and $R_i$. Find $\\displaystyle\\sum_{j = 1}^{N} \\min(d(j, L_i), d(j, R_i))$.\n\n## Constraints\n\n*   $1 \\leq N, Q \\leq 2 \\times 10^5$\n*   $1 \\leq A_i, B_i, L_i, R_i \\leq N$\n*   The given graph is a tree.\n*   All values in the input are integers.\n\n## Input\n\nThe input is given from Standard Input in the following format:\n\n$N$\n$A_1$ $B_1$\n$\\vdots$\n$A_{N-1}$ $B_{N-1}$\n$Q$\n$L_1$ $R_1$\n$\\vdots$\n$L_Q$ $R_Q$\n\n[samples]","is_translate":false,"language":"English"}],"meta":{"iden":"abc298_h","tags":[],"sample_group":[["5\n3 4\n4 5\n2 5\n1 5\n3\n4 1\n1 2\n5 3","4\n6\n3\n\nLet us explain the first query.  \nSince $d(1, 4) = 2$ and $d(1, 1) = 0$, we have $\\min(d(1, 4), d(1, 1)) = 0$.  \nSince $d(2, 4) = 2$ and $d(2, 1) = 2$, we have $\\min(d(2, 4), d(2, 1)) = 2$.  \nSince $d(3, 4) = 1$ and $d(3, 1) = 3$, we have $\\min(d(3, 4), d(3, 1)) = 1$.  \nSince $d(4, 4) = 0$ and $d(4, 1) = 2$, we have $\\min(d(4, 4), d(4, 1)) = 0$.  \nSince $d(5, 4) = 1$ and $d(5, 1) = 1$, we have $\\min(d(5, 4), d(5, 1)) = 1$.  \n$0 + 2 + 1 + 0 + 1 = 4$, so you should print $4$."],["8\n4 2\n4 1\n5 6\n6 1\n7 6\n8 1\n3 7\n7\n8 4\n4 4\n7 2\n4 4\n5 3\n4 4\n6 1","14\n16\n10\n16\n14\n16\n8"]],"created_at":"2026-03-03 11:01:14"}}