QOJ.ac

QOJ

実行時間制限: 15.0 s メモリ制限: 256 MB 満点: 100

#17337. Never Wait for Weights

統計

在一个实验室里,助理 Nathan Wada 正在成对地测量样品之间的重量差。他使用天平,因为当两个样品的重量非常接近时,天平比弹簧秤能更精确地测量它们之间的重量差。

他偶尔会被问及某对样品之间的重量差。根据已经获得的测量结果,他可能可以回答,也可能无法回答。

由于他正在积累大量的测量数据,现在要他迅速说出重量差并不容易。Nathan 请你开发一个程序,记录测量结果并自动计算出重量差。

输入格式

输入包含多个数据集。

每个数据集的第一行包含两个整数 $N$ 和 $M$。$N$ 表示样品的数量($2 \le N \le 100,000$)。每个样品都被分配了一个从 $1$ 到 $N$ 的唯一数字作为标识符。

数据集的其余部分由 $M$ 行组成($1 \le M \le 100,000$),每行对应一个测量结果或一个查询。它们按时间顺序给出。

测量结果的格式为:

! a b w

表示编号为 $b$ 的样品比编号为 $a$ 的样品重 $w$ 微克($a \ne b$)。即 $w = w_b - w_a$,其中 $w_a$ 和 $w_b$ 分别是 $a$ 和 $b$ 的重量。这里 $w$ 是一个不超过 $1,000,000$ 的非负整数。

你可以假设所有的测量都是准确且一致的。

查询的格式为:

? a b

表示询问编号为 $a$ 和 $b$ 的样品之间的重量差($a \ne b$)。

最后一个数据集之后是一行由空格分隔的两个零。

输出格式

对于每个查询 ? a b,如果根据该查询之前的测量结果可以计算出重量差,则输出编号为 $a$ 和 $b$ 的样品之间的重量差(以微克为单位,即 $w_b - w_a$),并换行。该差值可以为零,也可以为正数或负数。你可以假设其绝对值最多为 $1,000,000$。如果根据查询之前的测量结果无法计算出重量差,则输出 UNKNOWN 并换行。

样例

输入样例 1

2 2
! 1 2 1
? 1 2
2 2
! 1 2 1
? 2 1
4 7
! 1 2 100
? 2 3
! 2 3 100
? 2 3
? 1 3
! 4 3 150
? 4 1
0 0

输出样例 1

1
-1
UNKNOWN
100
200
-50

Discussions

About Discussions

The discussion section is only for posting: 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.
  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.