[Ynoi2004] rpmtdq

Luogu
IDLGP9058
Time2000ms
Memory512MB
DifficultyP7
2004O2优化Ynoi
给定一棵有边权的无根树,需要回答一些询问。 定义 $\texttt{dist(i,j)}$ 代表树上点 $i$ 和点 $j$ 之间的距离。 对于每一组询问,会给出 $l,r$,你需要输出 $\min(\texttt{dist(i,j)})$ 其中 $l\leq i < j \leq r$。 ## Input 第一行一个整数 $n$,表示树的节点个数。 接下来 $n-1$ 行,每行三个整数 $x,y,z$ 表示一条连接 $x,y$ 边权为 $z$ 的树边,输入保证构成一棵树。 之后一行一个数 $q$,表示询问个数。 之后 $q$ 行,每行两个整数 $l,r$ 表示一组询问,如果对于一组询问,找不到任何二元组 $(i,j)$ 满足 $l\le i<j\le r$,则输出 $-1$。 ## Output 输出 $q$ 行,每行一个整数,表示这组询问的答案。 [samples] ## Note Idea:nzhtl1477,Solution:Kubic&ccz181078,Code:Kubic,Data:Kubic 对于$100\%$的数据,满足 $n\leq2\times 10^5$,$q\leq 10^6$,$1\le z\le 10^9$。
Samples
Input #1
5
1 2 5
1 3 3
1 4 4
3 5 2
5
1 1
1 4
2 4
3 4
2 5
Output #1
-1
3
7
7
2
API Response (JSON)
{
  "problem": {
    "name": "[Ynoi2004] rpmtdq",
    "description": {
      "content": "给定一棵有边权的无根树,需要回答一些询问。 定义 $\\texttt{dist(i,j)}$ 代表树上点 $i$ 和点 $j$ 之间的距离。 对于每一组询问,会给出 $l,r$,你需要输出 $\\min(\\texttt{dist(i,j)})$ 其中 $l\\leq i < j \\leq r$。",
      "description_type": "Markdown"
    },
    "platform": "Luogu",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 524288
    },
    "difficulty": {
      "LuoguStyle": "P7"
    },
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "LGP9058"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "给定一棵有边权的无根树,需要回答一些询问。\n\n定义 $\\texttt{dist(i,j)}$ 代表树上点 $i$ 和点 $j$ 之间的距离。\n\n对于每一组询问,会给出 $l,r$,你需要输出 $\\min(\\texttt{dist(i,j)})$ 其中 $l\\leq i < j \\leq r$。\n\n## Input\n\n第一行一个整数 $n$,表示树的节点个数。\n\n接下来 $n-1$ 行,每行三个整数...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments