Surveyors' Altitude Log
A survey team is mapping n peaks in a mountain range, numbered 1 to n. No one has
measured any peak's absolute altitude, but surveyors keep radioing in claims about how two
peaks compare, and base camp keeps asking about pairs of peaks whose difference it wants to
know.
The log contains q lines, each one of two kinds:
CLAIM a b d— a surveyor reports that peakastands exactlydmeters above peakb. In other words,height(a) - height(b) = d. The valuedmay be negative (meaningais belowb) or zero (meaning they are at the same altitude). Peaksaandbmay be equal.QUERY a b— base camp asks forheight(a) - height(b).
Base camp accepts a claim only if it is consistent with every claim accepted so far (together
with everything those claims imply by combining with each other). Note that a claim is still
accepted, with no further comment, if it says nothing new (it already follows from what is
known). If instead a new claim contradicts the accepted claims — implying a different value for
some pair's height difference than what is already known — base camp radios back CONFLICT and
discards that claim entirely: it has no effect on anything that follows.
For each QUERY a b, base camp reports height(a) - height(b) if that value is pinned down by
the claims accepted so far, and UNKNOWN otherwise.
Note that CLAIM lines that are accepted produce no output at all — only rejected (CONFLICT)
claims and QUERY lines produce a line of output, in the order they appear in the log.
Input
The first line contains two integers n and q (1 ≤ n ≤ 2·10^5, 1 ≤ q ≤ 2·10^5).
Each of the next q lines contains one log entry, in one of the two forms above:
CLAIM a b dwith 1 ≤a,b≤nand -10^9 ≤d≤ 10^9.QUERY a bwith 1 ≤a,b≤n.
Output
For every CLAIM that is rejected, print a line containing CONFLICT.
For every QUERY, print a line containing either the integer value of height(a) - height(b)
(it is guaranteed to fit in a 64-bit signed integer), or UNKNOWN if it cannot be determined
from the claims accepted so far.
Print nothing for a CLAIM that is accepted. The total number of output lines therefore equals
the number of QUERY entries plus the number of rejected CLAIM entries, in the order the
corresponding entries appear in the log.
Notes
In the first example, the claim CLAIM 1 2 5 says peak 1 is 5 meters above peak 2, and
CLAIM 2 3 3 says peak 2 is 3 meters above peak 3, so peak 1 must be 8 meters above peak 3 —
the query confirms this. The later claim CLAIM 1 3 10 contradicts that (it would require a
difference of 10, not 8), so it is reported as CONFLICT and ignored; the final query shows the
original value of 8 still holds.