QOJ.ac

QOJ

Time Limit: 4 s Memory Limit: 512 MB Total points: 100 Difficulty: [show]

#5148. rpmtdq

统计

给定一棵有边权的无根树,需要回答一些询问。

定义 $\texttt{dist(i,j)}$ 代表树上点 $i$ 和点 $j$ 之间的距离。

对于每一组询问,会给出 $l,r$,你需要输出 $\min(\texttt{dist(i,j)})$ 其中 $l\leq i < j \leq r$。

输入格式

第一行一个整数 $n$,表示树的节点个数。

接下来 $n-1$ 行,每行三个整数 $x,y,z$ 表示一条连接 $x,y$ 边权为 $z$ 的树边,输入保证构成一棵树。

之后一行一个数 $q$,表示询问个数。

之后 $q$ 行,每行两个整数 $l,r$ 表示一组询问,如果对于一组询问,找不到任何二元组 $(i,j)$ 满足 $l\le i < j\le r$,则输出 $-1$。

输出格式

输出 $q$ 行,每行一个整数,表示这组询问的答案。

样例数据

样例 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

样例 1 输出

-1
3
7
7
2

子任务

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$。

Discussions

About Discussions

The discussion section is only for posting: Editorials, General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues. Submitting multiple issues may cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.