We need to determine if it is possible that Stepan's claim is true. He claims that he only reordered cubes in the subsegment [l, r] (i.e., permuted the cubes originally at positions l..r among themselves), leaving cubes outside that segment unchanged (both in identity and position). However note: The cubes are physical objects with numbers written on them. The numbers may not be distinct; there could be multiple cubes with same number. So we need to treat cubes as objects; each cube has an identifier (implicitly, its initial position) and a number. However we don't have identifiers; we only have the sequences a (initial numbers) and b (final numbers). Stepan says he only rearranged cubes between positions l and r. That means that for any position i outside [l, r], the cube that ends up at position i must be exactly the same cube that originally was at position i. For positions inside [l, r], the multiset of cubes originally in that segment must equal the multiset of cubes that end up in that segment (since no cubes added or removed). So we need to check if it's possible that the sequences a and b are related by a permutation that only moves cubes within [l,r].
But note: The cubes are not distinguishable beyond their numbers; multiple cubes might have the same number. So the question is: Is there a way to assign identities to cubes with numbers a_i such that after a permutation that fixes positions outside [l,r] and permutes within [l,r], we get the sequence b? Equivalently, check whether:
-
For every i not in [l,r], a_i must equal b_i because the cube at position i remains the same. But careful: The cube is the same object, so its number must be the same because the number is written on the cube and doesn't change. So if the cube is unchanged, the number stays the same. So if Stepan did not touch cubes outside [l,r], then for each i outside [l,r], the number at position i must be unchanged: a_i = b_i. If any such i has a_i != b_i, then it's impossible, so Stepan definitely lied -> print LIE.
-
For positions inside [l,r], the multiset of numbers originally in that segment must match the multiset of numbers finally in that segment, because the same cubes (with same numbers) are just rearranged. So we need that the multiset {a_i | i in [l,r]} equals the multiset {b_i | i in [l,r]}. If not, then it's impossible -> LIE.
If both conditions hold, then it's always possible? Let's think: Suppose a_i = b_i for all i outside [l,r], and the multisets within [l,r] match. Then can we always assign identities such that the cubes from [l,r] are permuted to achieve b? Since cubes are physical objects, each cube has a fixed number. However, if there are duplicates, we might need to check if we can permute them to match b, but the multiset condition ensures that the multiset of numbers is the same, so there exists some permutation of the cubes within the segment that yields the desired order b (since cubes with same number are indistinguishable). But is there any additional constraint? For instance, suppose the cubes outside the segment have some numbers that also appear inside the segment. Could there be a scenario where we need to swap a cube from inside with outside? That's not allowed because outside cubes must stay fixed. But if the multiset inside matches and outside fixed, then it's possible: just rearrange the cubes inside arbitrarily; we can always produce b because we have exactly the same numbers. However, consider that cubes are objects; if there are duplicate numbers, we might worry about which specific cube goes where, but since they are identical in terms of number, we can assign any cube with the required number to each position. But is there a subtlety? The cubes themselves are distinct objects but we only care about the numbers observed. If the multiset matches, we can rearrange the cubes to produce any order that uses those numbers. Indeed, suppose we have cubes: at positions inside we have certain numbers; we can permute them arbitrarily; the result can be any permutation of that multiset. Since b inside is some permutation of the multiset, it is achievable. So the conditions seem sufficient.
But wait: The problem statement: "Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i.e. he said that he reordered cubes between positions l and r, inclusive, in some way)." This implies that cubes outside [l,r] are not moved at all; they remain in the same position. That's exactly our condition.
Thus, algorithm: Check for i from 1 to n: if i < l or i > r, then require a_i == b_i. If any mismatch, output LIE.
If pass, then check that for i in [l,r], the multiset counts of numbers are equal. That is, for each value v, count of v in a[l..r] must equal count of v in b[l..r]. Since values are between 1 and n, we can use frequency arrays of size n+1. For indices in the segment, increment freq_a[ a[i] ] and freq_b[ b[i] ]. Then compare. If all frequencies match, output TRUTH else LIE.
But is it that simple? Let's test with given examples.
Example 1:
n=5, l=2, r=4
a: 3 4 2 3 1
b: 3 2 3 4 1
Outside: indices 1 and 5: a1=3, b1=3 ok; a5=1, b5=1 ok.
Inside: indices 2-4: a: 4,2,3 -> multiset: {2,3,4}
b: 2,3,4 -> {2,3,4} matches. So TRUTH. correct.
Example 2:
n=3, l=1, r=2
a:1 2 3
b:3 1 2
Outside: index 3: a3=3, b3=2 -> mismatch -> LIE. correct.
Example 3:
n=4, l=2, r=4
a:1 1 1 1
b:1 1 1 1
Outside: index1:1=1 ok.
Inside: all ones, multiset matches -> TRUTH. correct.
Thus seems straightforward.
But are there any corner cases? What if l and r cover whole array? Then all positions are in segment. Then condition outside trivially holds; we need multiset equality over entire array, i.e., the sequence b must be a permutation of a. That makes sense: he could have permuted everything. So if a and b are permutations of each other, it's possible; else not.
What if the multiset condition holds but there is a mismatch outside? That's already caught.
Thus, solution: O(n) time, O(n) memory.
But wait: The problem statement says "It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes." That is just guarantee that the total multiset of cubes (over all positions) is the same? Actually, if he only rearranged cubes, the multiset of numbers across whole array must be the same. But the guarantee says that the sequences b are derived by rearranging the cubes of a, but not necessarily a permutation? Actually they explicitly say: "It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes." That means the multiset of numbers across entire array must be equal. But they might not be; but they guarantee that it is true? They say it is guaranteed, meaning in the input it is always true that b is a permutation of a (i.e., the cubes are the same set). Let's read: "It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes." So that's a guarantee about the input: that the multiset of numbers in a and b are equal. So we don't need to check that globally. However, we still need to check the segment condition: outside positions unchanged; inside segment must have same multiset as inside segment. But since total multisets are equal, if outside positions have same numbers (a_i = b_i for all i outside), then automatically the inside multisets match? Not necessarily: It is possible that outside positions have the same numbers but maybe the numbers at outside positions in a and b are the same but the cubes could be different? Wait, if outside positions are unchanged, then the cube at position i (with number a_i) must be the same cube, so the number must be exactly a_i. So if a_i = b_i for all i outside, then those positions are fixed. Then the remaining cubes (the ones originally inside) must fill the inside positions. Since total multiset equality holds, the multiset of numbers inside a and inside b will automatically match if the outside parts match exactly. Let's reason: Let A_out = multiset of a_i for i not in [l,r]; B_out = multiset of b_i for i not in [l,r]. If A_out = B_out because a_i = b_i for each such i, then indeed A_out = B_out. Then total multiset A_total = B_total. Then A_in = A_total \ A_out, B_in = B_total \ B_out. Since A_total = B_total and A_out = B_out, we get A_in = B_in. So the inside multiset condition is automatically satisfied if outside matches and total multisets equal. But wait, is total multiset equality guaranteed? The problem says it's guaranteed that Stepan only rearranged cubes, so the multiset of numbers must be the same. That is indeed guaranteed. However, could there be input that violates that? The statement says it is guaranteed, meaning we can assume that the multiset of a and b are equal. So if we check only outside equality and find no mismatch, then inside automatically matches? Possibly. But it's safer to check inside as well, but it's redundant given guarantee. However, we might need to check inside if outside matches? Not needed; but the problem might not actually guarantee that total multiset equality holds? Let's read carefully: "It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes." That is a guarantee about the process that generated b, not about the input? Actually, it's typical in such problems: they guarantee that b is obtained by permuting a, i.e., the multisets are equal. So we can rely on that. In sample inputs, that's true. So if we only check outside positions, and they match, then answer is TRUTH. But wait, there could be a scenario where outside positions match but inside multiset does not match? That would contradict total multiset equality because if outside match individually, then outside multisets are equal, and total multisets are equal by guarantee, so inside multisets must be equal. So it's impossible. So we could simplify: Only need to check that for all i not in [l, r], a_i == b_i. If any mismatch, LIE; else TRUTH. But is that always correct? Let's test with possible tricky case: Suppose there are duplicate numbers. Consider n=3, l=2, r=2. a = [1,2,1]; b = [1,1,2]. Outside positions: i=1 and 3. i=1: a1=1, b1=1 matches. i=3: a3=1, b3=2 mismatches -> LIE. That's correct because position 3 changed. But if b = [2,1,1]? l=2,r=2. Outside: i=1: a1=1, b1=2 mismatch -> LIE. So fine.
Now consider n=3, l=1,r=3 (full segment). Then there are no outside positions, so condition vacuously true => TRUTH if total multiset equal (which is guaranteed). Indeed, he could have permuted all cubes, so it's always possible. That matches expectation.
Now consider scenario: n=5, l=2,r=4. a = [1,2,3,4,5]; b = [1,2,3,4,5] (identical). Outside positions: i=1 and 5 match, so TRUTH, which is correct (he could have done nothing).
Now consider a case: a = [1,2,3,4,5]; b = [1,3,2,4,5]; l=2,r=3. Outside: i=1:1=1; i=4:4=4; i=5:5=5 -> all match. So TRUTH, which is correct (swap inside).
Now consider a case where outside match but inside numbers are different? For instance, a = [1,2,2,3]; b = [1,2,3,2]; l=2,r=3. Outside: i=1:1=1; i=4:3=2? Wait, b4=2, a4=3 mismatch. So not all outside match. So would be LIE. But suppose we try: a = [1,2,2,3]; b = [1,2,2,3] with l=2,r=3, obviously matches.
What about: a = [1,2,3,2]; b = [1,3,2,2]; l=2,r=3. Outside: i=1:1=1; i=4:2=2 matches. So outside matches. Then total multisets equal: a has {1,2,2,3}, b has {1,2,2,3}. So inside multiset automatically matches? a inside indices 2,3: 2 and 3; b inside: 3 and 2. Multiset matches. So TRUTH. So okay.
Thus, it seems the only condition is that cubes outside [l,r] must be unchanged. That is necessary and sufficient given the guarantee. Let's double-check: Could there be a situation where outside positions match but the claim is still impossible because the cubes inside cannot be permuted to achieve b due to identity constraints? Since cubes are distinct objects, but they have numbers. If two cubes have the same number, they are indistinguishable to us. However, the condition "Stepan only swapped cubes within [l,r]" means that the specific cubes that were outside remain outside and are the same objects. That is satisfied if the numbers at those positions are unchanged. But what if there are multiple cubes with the same number? Could it be that a_i = b_i for all i outside, but the cube at position i outside might actually be a different cube with the same number? But Stepan claims he did not touch those cubes; i.e., the cube that ended at position i outside must be exactly the same physical cube that originally was at position i. Since numbers are on cubes, if the number matches, does that guarantee it's the same cube? Not necessarily if there are multiple cubes with the same number. For example: Suppose we have two cubes with number 1, at positions 1 and 2. Initially a = [1,1]. Stepan claims he swapped only cubes between positions l=2 and r=2 (i.e., no swap). But suppose b = [1,1]. Outside positions: if l=2,r=2, then position 1 is outside. a1=1, b1=1 matches. So according to our simple check, it's TRUTH. But is it possible that Stepan swapped cube at position 1 with position 2? That would involve moving a cube from outside the segment, which is not allowed. But if both cubes are number 1, we can't tell the difference. The claim is that he only swapped cubes in the segment (position 2). Since the segment is just a single position, swapping within it does nothing. So he claims he didn't move cube at position 1. But could he have swapped cubes 1 and 2 and still have the same numbers? Yes, if both have number 1, swapping yields the same sequence. But would that be considered "swapped only cubes between positions l and r"? If l=2,r=2, swapping within that segment alone does nothing. But if he swapped cubes at positions 1 and 2, that would involve a cube outside the segment. However, if the numbers are identical, the final sequence is the same. The question: Is it guaranteed that Stepan deceived his brother? If it is possible that he told the truth, we output TRUTH. In this scenario, it is possible that he told the truth (since doing nothing yields the same sequence). Even if he swapped cubes 1 and 2, that would be a lie because he moved a cube outside the segment. But we don't know which scenario happened. The problem asks: determine if it is possible that Stepan said the truth. That is, does there exist a sequence of swaps (only within [l,r]) that could transform a into b? If such a sequence exists, then his claim could be true, and we output TRUTH. If no such sequence exists, then he definitely lied, output LIE. So we need to check existence, not uniqueness.
Thus, in the example above, it is possible that he told the truth (by doing nothing). So TRUTH is correct. So our simple check (outside positions unchanged) is sufficient because if outside numbers match, we can always permute inside to match b? But wait, we also need to ensure that the permutation of inside is possible given the numbers inside. But if outside numbers match exactly, then the multiset of numbers inside a and inside b must be equal due to total multiset equality guarantee. But is the guarantee always that total multiset equality holds? The problem statement: "It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes." That is a guarantee about the process that produced b, i.e., b is a permutation of a. So yes, we can assume that the multiset of a and b are identical. So if outside numbers match (i.e., a_i = b_i for all i outside), then the multiset of numbers inside must be equal as argued. Therefore, we can rearrange the cubes inside arbitrarily to achieve b. So existence is guaranteed.
But is there any scenario where outside numbers match but the inside multiset does NOT match? That would contradict total multiset equality, so such input would violate the guarantee. Since we are guaranteed that b is a permutation of a, that cannot happen. So indeed, only condition is that a_i = b_i for all i not in [l,r].
But wait, what about the possibility of identical numbers causing ambiguity? Suppose outside numbers match, but the cubes that were originally outside might have been swapped among themselves? That would violate the claim because he said he only swapped cubes within [l,r]. But if the numbers outside match exactly, could it be that he swapped cubes among outside positions and still got the same numbers? For instance, outside positions have identical numbers, so swapping them doesn't change the sequence. That would be an action that is not allowed but yields same final sequence. However, we are only checking existence of a valid sequence of swaps (within [l,r]) that yields b. It is possible that the actual transformation Stepan performed involved illegal swaps, but we only need to check whether there exists some legal transformation (i.e., permuting only inside) that results in b. If such a legal transformation exists, then it's possible that he told the truth, even if he actually cheated. So as long as there exists a permutation of the cubes inside that maps a to b while keeping outside fixed, we output TRUTH. This existence condition is exactly: a_i = b_i for all i outside, and the multiset of numbers inside a equals multiset inside b (which automatically holds if outside match and total multisets equal). So we just need to check outside equality.
But careful: The total multiset equality is guaranteed, so we can rely on it. However, to be safe, we could also check inside equality explicitly. It doesn't cost much.
But there's another nuance: Suppose outside positions match numbers, but there is a scenario where the cubes originally outside have the same numbers but different physical identities? Since numbers may repeat, it's possible that the cube at an outside position i originally had number x, and the cube at an outside position j originally had number x as well. If we keep outside numbers unchanged, but could we have swapped those two cubes? That would still result in the same numbers at outside positions, but would that be detectable? The claim is about the actions, not about the final configuration. We only observe the final configuration. We need to determine if it's possible that his claim is true given the initial and final configurations. If there exists a legal permutation (only inside) that yields the final configuration, then his claim could be true. So even if the actual physical cubes at outside positions are swapped among themselves but they have same numbers, the final configuration would be identical to the legal one where they weren't swapped. Since we cannot distinguish identical-numbered cubes, it's possible that the legal transformation happened. So that's fine.
Thus, the problem reduces to checking whether a_i = b_i for all i not in [l,r].
But wait, let's double-check with a potential counterexample. Consider n=3, a = [1,2,1]; b = [1,1,2]; l=2,r=2. Outside positions: 1 and 3. a1=1, b1=1 OK; a3=1, b3=2 mismatch -> LIE. That's correct because position 3 changed number, so cube at position 3 must have been moved, which is outside the segment. So LIE.
Consider n=3, a = [1,2,1]; b = [2,1,1]; l=2,r=3? Actually l=2,r=3 (segment covers positions 2 and 3). Outside: position 1. a1=1, b1=2 mismatch -> LIE. So correct.
Now consider a more subtle case: Suppose a = [1,2,3]; b = [1,3,2]; l=2,r=3. Outside: position1 matches. So TRUTH, which is correct.
Now consider a = [1,2,2,3]; b = [2,2,1,3]; l=1,r=3? That would be segment covering positions 1-3. Outside: position4: a4=3, b4=3 matches. So TRUTH? But wait, segment is 1-3, so he claims he only rearranged cubes among positions 1,2,3. b's segment is [2,2,1] which is a permutation of [1,2,2]. So it's possible: he could have moved cube with 1 from position1 to position3, etc. So TRUTH correct.
Now what if a = [1,2,3,4]; b = [1,3,4,2]; l=2,r=3. Outside: positions1 and4: a1=1,b1=1 matches; a4=4,b4=2 mismatch -> LIE. So correct.
Now consider a case where outside numbers match but the multiset inside doesn't match because total multiset not equal? That's impossible under guarantee.
Thus the simple solution is to check positions outside [l,r] for equality. That yields O(n) time and O(1) extra memory.
But is there any scenario where even though a_i = b_i for i outside, it's still impossible to achieve b by permuting only inside? Only if the multiset inside doesn't match, which would mean total multisets differ. Since total multisets are guaranteed to be same, it's impossible. But what if the guarantee is not that total multisets are equal? Let's read the statement again: "It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes." This is an assertion about the process that produced b; it is not a condition on the input. Usually problem statements phrase "It is guaranteed that ..." to indicate that the input satisfies that condition. For example, Codeforces problems often say: "It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes." meaning that the multiset of numbers in a and b are the same. Yes, that is a guarantee about the input. So we can rely on it.
Thus, we can solve by:
for i in 1..n:
if i < l or i > r:
if a[i] != b[i]:
print("LIE")
return
print("TRUTH")
But wait, what about the case where l=1, r=n? Then there are no i outside, so we always print TRUTH. That matches expectation.
But is there any other subtle condition? Let's think about cubes being swapped only between l and r, but could the swapping involve multiple steps? It says he swapped only cubes which were on the positions between l and r. He could have swapped them arbitrarily, so any permutation of the segment is allowed. So indeed any permutation of the segment is possible.
Thus answer is simple.
But let's check if there might be a scenario where outside numbers match but the segment permutation is impossible because the cubes in the segment are not sufficient to produce b due to some identity constraints? For example, suppose there is a cube outside that has a number that also appears inside. But we are not allowed to move that cube. However, if the outside numbers match, that means the cube outside stays in place. That's fine. The cubes inside have a multiset of numbers. To produce b inside, we need to arrange them. Since the multiset matches, we can assign the cubes accordingly. But is there any situation where the multiset matches but we cannot assign because we might need to use a cube from outside to fill an inside position? That would require that some number inside b appears more times than inside a, but that's prevented by multiset equality. So it's fine.
Thus the solution is straightforward.
However, is there any twist? Let's re-read the problem statement: "Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way)." The phrase "swapped only cubes which where on the positions between l and r" could be interpreted as: He only performed swaps that involve cubes that are in that segment at the time of swapping. That is, he may have moved a cube from inside to outside if during the process a cube leaves the segment? Typically, "swapped only cubes which where on the positions between l and r" might mean that the cubes he moved were originally in that segment? Or that the swaps he performed were only between positions within [l,r]? The latter interpretation seems more natural: he only swapped cubes within that range, i.e., only performed operations that exchange the cubes at two positions both in [l,r]. That would mean that throughout the process, cubes outside that range are never touched. So indeed, final configuration must have those cubes unchanged. However, could it be that a cube originally inside moves outside via a sequence of swaps? If you only swap cubes within [l,r], then cubes initially inside stay inside; they never leave the segment because swaps are only between positions in the segment, so each cube that starts inside remains inside, and cubes outside remain outside. So final positions: all positions outside are occupied by the original outside cubes; all positions inside are occupied by the original inside cubes (but possibly permuted). That's exactly our condition.
But what about a cube originally inside moving to a position outside through a series of swaps that involve both inside and outside? That would require swapping an inside cube with an outside cube, which is not allowed because those swaps involve a position outside the segment. So not allowed.
Thus our interpretation is correct.
Given the simplicity, many participants might implement that.
But wait, I recall a Codeforces problem "Vasya and Books" or something? Actually this is CF Round 401? Or it might be from an older problem. I've seen a problem "Cubes" maybe. But I recall a twist: Because cubes may have same numbers, you need to check if the multiset of numbers inside the segment matches, not just the outside positions. However, many solutions I've seen do exactly that: check outside positions equality, and then check if the multiset inside matches. They don't rely on the guarantee. Actually, the guarantee says Stepan did not remove or add cubes, but that doesn't guarantee that the total multiset is equal? Actually, if he only rearranged cubes, then obviously the multiset of numbers stays the same because cubes are the same and numbers are attached to cubes. So it's guaranteed that the multiset of numbers in a and b are identical. So indeed, if outside matches, inside automatically matches. So some solutions only check outside positions. But is there any scenario where the guarantee "Stepan did not remove or add other cubes, he only rearranged Vasya's cubes" is not about the input, but about the statement? In competitive programming, "It is guaranteed" typically means that the input satisfies that property. So yes.
Let's test a custom case: n=2, l=1,r=1, a=[1,2], b=[2,1]. Outside positions: i=2 (since r=1, positions >1: i=2). a2=2, b2=1 mismatch -> LIE. That is correct because he claims he only swapped cube at position 1 with something? Actually only swapping within [1,1] means no swap, so b must equal a. So LIE. Good.
Another: n=2, l=1,r=2, a=[1,2], b=[2,1] -> TRUTH. Good.
Now consider a tricky case: n=4, l=2,r=3, a=[1,2,2,1], b=[1,2,2,1] (identical). Outside: positions 1 and 4 both match. So TRUTH. That's fine.
Now consider a case: a=[1,2,3,4], b=[1,4,2,3], l=2,r=3? Outside: positions 1 and 4: a1=1,b1=1; a4=4,b4=3 mismatch -> LIE. Indeed, to get b, cube at position 4 changed from 4 to 3, which is outside. So LIE.
Now consider a case: a=[1,2,3,4], b=[2,1,3,4], l=2,r=3? Outside: positions 1 and 4: a1=1,b1=2 mismatch -> LIE. So correct.
Now what about a case where outside matches but the segment contains a different multiset? Since total multiset is same, this can't happen. But suppose the input violates the guarantee? The problem says it's guaranteed, so we can trust that.
Thus solution: iterate over indices from 1 to n; if ir, check a[i]==b[i]; if any mismatch, print "LIE" and exit; else print "TRUTH".
But wait, we need to be careful with 1-indexing: l and r are given as 1-indexed positions. Input format: first line n, l, r. Then sequence a1..an, b1..bn.
Implementation: read n, l, r; read list a of length n; read list b. For i in range(n): if i+1 < l or i+1 > r: if a[i] != b[i]: print("LIE"); return. After loop, print("TRUTH").
But is there any hidden nuance? Let's read the problem statement again: "Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i.e. he said that he reordered cubes between positions l and r, inclusive, in some way)." The phrase "swapped only cubes which where on the positions between l and r" could be interpreted as: He only swapped cubes that were initially in that segment? Or he only used swaps that involve positions in that segment? Usually, "swapped only cubes which were on the positions between l and r" might mean that the cubes he moved were those that were originally located between l and r, but he could have moved them to outside positions? If he swapped two cubes, one inside and one outside, that would involve a cube originally inside and one originally outside. But the phrasing "swapped only cubes which where on the positions between l and r" could be ambiguous. However, the clarification in parentheses says: "i.e. he said that he reordered cubes between positions l and r, inclusive, in some way." That indicates he only reordered the cubes within that segment; i.e., he permuted the subarray. So the interpretation is consistent with our earlier reasoning.
Thus, the solution is trivial. But why would the problem be rated maybe not trivial? Possibly there is a trick: The cubes have numbers that are not necessarily distinct, so the condition for outside positions must be exact equality, not just multiset. That's still simple.
But I recall a Codeforces problem 896A? Actually I think it's "Cubes" from Codeforces Round #471 (Div. 2) maybe? Let's search memory: I think it's problem "Cubes" from Codeforces 374? Wait, I recall a problem "Vasya and Pairs" but not this.
Given that the problem is from some contest, the solution might be exactly that. But let's double-check with the example: second example: 3 1 2; a=1 2 3; b=3 1 2; we found mismatch at position 3, so LIE. Good.
But what if the input was: n=3, l=1,r=2, a=1 2 2, b=2 1 2. Outside: i=3: a3=2, b3=2 match. So TRUTH? Let's see: initial [1,2,2], final [2,1,2]. He claims he swapped only cubes between positions 1 and 2. Is it possible to get [2,1,2] by permuting only the first two cubes? Permuting first two yields [2,1,2]. Yes, that's exactly swapping them. So TRUTH. Our condition says TRUTH. Good.
Another: n=3, l=1,r=2, a=1 2 2, b=2 2 1. Outside: i=3: a3=2, b3=1 mismatch -> LIE. Indeed, to get b, cube at position 3 changed from 2 to 1, so not allowed.
Now consider a more subtle case: n=3, l=2,r=3, a=1 2 2, b=2 2 1. Outside: i=1: a1=1, b1=2 mismatch -> LIE. Indeed, can't because cube at position 1 changed.
All consistent.
Thus solution appears trivial. However, I'm a bit suspicious: why would they give a problem that's just a simple check? Possibly because it's a Div2 A or B? But the constraints are up to 1e5, but that's fine. I recall there is a known Codeforces problem "Cubes" (maybe 520B?) Not sure. Actually, I think it's problem "Cubes" from Codeforces Round #215 (Div. 2) or something. In any case, it's plausible.
But before finalizing, let's double-check the interpretation of the guarantee: "It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes." This means that b is a permutation of a. That's important because if it weren't guaranteed, then a scenario where outside matches but inside multiset mismatches could be a LIE even if outside matches? But then we would need to check multiset. But the problem statement explicitly says it's guaranteed, so we can skip that check. However, I'd like to verify from sample: In sample 2, a and b are both permutations of each other? a=[1,2,3], b=[3,1,2] yes permutation. So it holds.
But what about sample 1: a=[3,4,2,3,1], b=[3,2,3,4,1] also permutation. Sample 3: all ones, trivially permutation. So it's consistent.
Thus the problem is indeed trivial. But I'm still cautious: Could there be a scenario where outside positions match individually but it's still impossible because the cubes inside cannot be permuted to achieve b due to the fact that cubes are distinct objects, and the numbers on them might be the same, but we need to respect that cubes from outside cannot be moved, and cubes from inside cannot be moved outside. However, the outside positions match numbers, but what if there is a cube originally inside that has the same number as a cube originally outside, and the final configuration inside requires that a specific cube from inside goes to a position outside? That would require moving it outside, which is not allowed. But if outside numbers match, the cubes at outside positions are exactly the original outside cubes? Wait, the condition a_i = b_i for i outside only tells us that the number at each outside position is the same as before. However, does it guarantee that the cube occupying that outside position is the original cube from that position? Not necessarily if there are duplicate numbers: a cube from inside could have the same number as the original outside cube, and if it swapped with the outside cube, the number at that outside position would still be the same. But that would mean that a cube from inside moved to outside, which is not allowed. But the final configuration would have the same numbers outside, so from the number sequences alone, we cannot tell if the cube at an outside position is the original or an inside cube with the same number. So if numbers outside match, it's possible that Stepan lied by moving an inside cube to outside, but could he also have told the truth? We need to check if there exists a legal transformation (only permuting inside) that yields the final configuration. If numbers outside match, we can set the cubes at outside positions to be the original cubes (since they have the same numbers, we can just assume they stayed). However, we must also ensure that we can assign the cubes originally inside to the inside positions to match the numbers b[l..r] without using any outside cubes. This is possible if the multiset of numbers inside a matches the multiset inside b. But we argued that is automatically true if total multiset matches and outside numbers match exactly (by element, not just multiset). But does outside numbers matching guarantee that the multiset of numbers outside is the same? Yes, because at each outside position individually the numbers are identical, so the multiset of numbers outside in a and b are identical. Since total multisets are equal, the inside multisets must be identical. So we can assign the inside cubes to achieve b. But is there any scenario where outside numbers match individually, but the inside multiset does not match because the total multisets are not equal? That would violate the guarantee, but we trust guarantee. So inside multiset matches.
Thus we can always construct a legal permutation: keep each outside cube fixed (since its number matches, we can assume it's the same cube; even if there are duplicates, we can just assign the original cube to that position because it has the same number, and that's allowed as it's not moved). Then, for inside positions, we need to assign the inside cubes to positions to match b. Since multisets match, there exists a bijection (maybe multiple) between the inside cubes and inside positions such that each cube's number equals the target number. However, can we always assign? That requires that for each inside position, there is a cube originally inside with the required number. Since the multisets match, we can assign arbitrarily: For each inside position, we need a cube with that number. Since we have exactly the same counts of each number among the inside cubes as in the target inside positions, we can match them. But we need to ensure that we don't inadvertently use a cube that is needed for outside positions, but outside cubes are fixed, we are using only inside cubes for inside positions. So yes, it's possible.
But is there any subtlety about the distinctness of cubes? Suppose there is a cube originally outside with number x, and there is also a cube originally inside with number x. In the final configuration, the outside position still shows x. If we keep the original outside cube there, that's fine. Then the inside cube with number x can be used inside. But could there be a situation where the inside target numbers require a cube with number y, but the only cube with number y originally inside is actually needed for an outside position? But outside positions are fixed, they already have cubes with numbers matching. The outside cubes are not used for inside positions. The inside cubes are only used for inside positions. Since the multiset of numbers inside a equals multiset of numbers inside b, it's feasible.
Thus the condition is sufficient.
Therefore, the answer is indeed just checking equality outside.
But wait, what if there is a scenario where outside numbers match, but the inside multiset matches, but there is a constraint due to the fact that cubes are physical objects: you cannot "split" a cube; each cube must go to exactly one position. However, if multisets match, we can assign. So yes.
Thus the solution is O(n) and trivial.
But I recall reading some editorial for a problem "Cubes" maybe they also checked the frequencies inside. But maybe they did both checks for safety. The problem statement's guarantee might be interpreted differently: "It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes." That might be a guarantee about Stepan's claim, not about the input? Actually it's part of the input description: they give sequences a and b, and then they say "It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes." That is to assure that b is a permutation of a. So it's an input guarantee.
Given all that, I'll implement the simple check.
But let's also consider if the indices are 1-indexed and l, r are inclusive. We'll just loop.
Edge cases: l>r? But constraints say l ≤ r. n up to 1e5, fine.
Complexities: O(n) time, O(1) extra beyond input arrays.
Implementation in Python:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
l = int(next(it))
r = int(next(it))
a = [int(next(it)) for _ in range(n)]
b = [int(next(it)) for _ in range(n)]
for i in range(n):
pos = i+1
if pos < l or pos > r:
if a[i] != b[i]:
print("LIE")
return
print("TRUTH")
We should also consider if we need to check the case where the segment is empty? l and r at least 1 and l≤r, so segment non-empty? It could be l=r=n or something.
Thus final answer.
But before finalizing, let's test some random cases to be thorough.
We'll generate random a and b that are permutations of each other, then pick random l,r and see if our condition matches the possibility of permuting segment only. We can write a brute-force checker for small n to verify.
I'll do mental reasoning.
Consider n=4, a=[1,2,3,4], b=[2,1,4,3]. l=2,r=3. Outside positions: 1 and 4. a1=1 vs b1=2 mismatch -> LIE. Is it possible to get b by permuting only positions 2-3? Permuting 2-3 yields [1,3,2,4] or [1,2,3,4] if no swap, or [1,3,2,4] if swap. That never gives [2,1,4,3]. So LIE correct.
Consider a=[1,2,3,4], b=[1,4,2,3], l=2,r=3. Outside: pos1:1=1 ok; pos4:4 vs 3 mismatch -> LIE. Indeed impossible.
Consider a=[1,2,3,4], b=[1,3,2,4], l=2,r=3 -> outside pos1=1 ok; pos4=4 ok => TRUTH. Indeed swapping positions 2 and 3 yields that.
Consider a=[1,2,2,3], b=[2,2,1,3], l=2,r=3? Outside: pos1=1 vs 2 mismatch -> LIE. But is there a legal transformation? Permuting positions 2-3 only, we can get [1,2,2,3] (identity) or [1,2,2,3]? Actually swapping 2 and 3 yields [1,2,2,3] because they are both 2. So can't get [2,2,1,3]. So LIE correct.
Consider a=[1,2,2,3], b=[1,2,2,3], l=2,r=3 -> TRUTH (identity).
Consider a=[1,2,2,3], b=[1,2,3,2], l=2,r=4? Actually l=2,r=4: outside pos1:1=1 ok => TRUTH? Because segment covers 2-4, which includes all others. The outside condition holds. But need to check if we can permute positions 2-4 to get b. a[2..4] = [2,2,3]; b[2..4] = [2,3,2] which is a permutation. So yes, possible. So TRUTH correct.
Now consider a case where outside numbers match, but the inside multiset is different, but total multiset equal? Could that happen? Let's try to craft: Suppose a = [1,2,3]; b = [1,3,2]; total multisets equal. If l=2,r=2, outside: positions 1 and 3. a1=1,b1=1 ok; a3=3,b3=2 mismatch -> so we catch mismatch. So cannot have inside multiset mismatch without an outside mismatch if total multisets equal? Let's try to create a scenario where outside matches but inside multiset mismatches. Let total multiset equal. For outside to match, we need a_i = b_i for all i outside. Then the sum of frequencies over outside positions are identical. Since total multisets are equal, the remaining inside must also match. So indeed impossible.
Thus the condition is exactly the outside equality.
Now is there any scenario where outside matches but the inside multiset matches, but because of duplicates we might need to consider that the cube originally outside might be needed inside? But we are not allowed to move outside cubes. However, the inside target numbers can be satisfied by inside cubes only because the multiset matches. So it's fine.
One more nuance: The cubes are physical objects. If there is a duplicate number, we might assign the same cube to multiple positions? No, each cube goes to exactly one position. But if we have duplicate numbers, there could be multiple cubes with same number. The assignment is possible if the multiset matches. So yes.
Thus the solution stands.
Given the problem's examples and constraints, it's straightforward.
However, I recall that there might be a twist: Stepan said he swapped only cubes which were on the positions between l and r. That could be interpreted as: he only swapped cubes that were originally in that segment, but he could have moved them out of the segment if he swapped with cubes outside? Actually, if he only swapped cubes that were on those positions, that might mean that he only used swaps where at least one of the positions is in [l,r]? The wording is ambiguous. Let's examine: "swapped only cubes which where on the positions between l and r, inclusive" - The phrase "cubes which where on the positions between l and r" suggests the cubes themselves (the objects) were located in that segment initially. But then he could swap them with cubes outside? The phrase "swapped only cubes which where on the positions between l and r" might mean that the only cubes he moved were those that were originally in that segment; he could have moved them to outside positions by swapping with outside cubes? But then the outside cubes would also move, which would be moving cubes that were not originally in the segment. However, if he swaps a cube from inside with a cube from outside, then the cube from outside is also moved, but the claim says he only swapped cubes which where on the positions between l and r. That could be interpreted as the cubes he touched (the ones he moved) were only those that were originally in the segment. But if he swaps with an outside cube, that outside cube also moves, so he would have moved a cube that was not originally in the segment. That might contradict the claim. However, the parenthetical clarification says: "i.e. he said that he reordered cubes between positions l and r, inclusive, in some way." That seems to clarify that he only reordered cubes within that segment. So the segment's order changed but cubes outside remained untouched. So our interpretation is correct.
Given that, the solution is indeed trivial.
But I'm going to search my memory: There is a Codeforces problem "Cubes" (maybe 520B) that is about something else. Actually, I recall a problem "Cubes" from Codeforces Round #180 (Div. 2) but that's about something else. Maybe it's from an Olympiad. However, I think the solution is indeed trivial, but many might overcomplicate.
Nevertheless, to be safe, I'll implement both checks: outside equality and inside frequency equality. The extra check doesn't hurt and ensures correctness even if my reasoning about guarantee is misinterpreted. It's just O(n) extra memory and time.
I'll write:
- Read n,l,r.
- Read arrays a and b.
- For i in range(n):
if i < l-1 or i >= r: (since 0-indexed, positions from 0 to n-1 correspond to 1..n; outside if pos+1 < l or pos+1 > r)
if a[i] != b[i]:
print("LIE")
return
- Create freq array of size maybe up to n (since numbers between 1 and n). Or use dictionary.
- For i in range(l-1, r): (inclusive l-1 to r-1)
freq_a[a[i]] += 1
freq_b[b[i]] += 1
- If any freq difference, print "LIE"
- else print "TRUTH"
But careful: In zero-indexed, segment is indices from l-1 to r-1 inclusive.
But we can also skip the frequency check as argued, but I'll include for robustness. However, if we include it, note that we need to check outside equality first; if any mismatch, we print LIE. Then, if outside all match, we could still check frequency. But if outside all match and total multisets are guaranteed equal, frequency will match automatically. But if the guarantee is not present, we still need it. I'll include it anyway; it's trivial.
But wait: There is a subtlety: The problem statement says "It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes." That indeed implies that the multiset of numbers in a and b are the same. So we can trust that. But I'll still include the frequency check for completeness; it doesn't affect complexity.
However, what about the possibility that outside equality holds but the inside frequency doesn't? That would indicate the guarantee is violated, but the problem says it's guaranteed, so such case won't appear. But I'll still include check.
Time complexity O(n). Memory O(n) for freq arrays (or O(max value) which is n). Since n up to 1e5, we can have array of size n+1.
Now, I'll implement solve() accordingly.
But we must be careful about input: numbers up to n (1 ≤ ai, bi ≤ n). So array of size n+1 is fine.
I'll write Python code.
Potential pitfalls: Using input() for large n might be slower, but 1e5 is fine. We'll use sys.stdin.read.
Now, test with given examples.
Example 1:
n=5,l=2,r=4
a = [3,4,2,3,1]
b = [3,2,3,4,1]
Outside: indices 0 (pos1) and 4 (pos5). Compare: a[0]=3 b[0]=3 ok; a[4]=1 b[4]=1 ok.
Freq inside: a[1:4] (indices 1,2,3): [4,2,3]; b: [2,3,4] -> freq both: 2:1,3:1,4:1. So match. Output TRUTH.
Example 2:
n=3,l=1,r=2
a=[1,2,3] b=[3,1,2]
Outside: index 2 (pos3): a[2]=3 b[2]=2 mismatch -> LIE. Output LIE.
Example 3:
n=4,l=2,r=4
a=[1,1,1,1] b=[1,1,1,1]
Outside: index0:1=1 ok.
Freq inside: all 1 -> match. TRUTH.
All good.
Now, what about a case where outside matches but inside frequency mismatches? Could we get such input? Possibly not, but if we did, our code would output LIE, which would be correct because it would be impossible. Since problem guarantees not, it's fine.
Thus solution final.
I think the intended solution is exactly that: check that the arrays are equal outside [l,r] and that the multisets inside are equal. Many Codeforces solutions do that.
Let's search memory: There is a problem named "Cubes" from Codeforces Round #315 (Div. 2) maybe? Actually I recall a problem "Cubes" from Codeforces Round #243 (Div. 2) with a similar statement. I think the solution is indeed simple.
Thus, I'll finalize.
But before finalizing, double-check the indexing: l and r are 1-indexed. So in code, we treat positions i from 1 to n. For i in range(n): if i+1 < l or i+1 > r: check a[i] != b[i].
Alternatively, loop over i from 0 to l-2 (if l>1) and from r to n-1 (if r<n). But that's fine.
Now complexity: O(n).
Now produce final answer.
But wait, I'd like to consider a possible misinterpretation: "swapped only cubes which where on the positions between l and r" could be interpreted as: He only performed swaps involving at least one cube that was originally in that segment. But he could have swapped an inside cube with an outside cube, moving the inside cube out and the outside cube in. The claim "did not remove or add any other cubes" still holds. Would that interpretation change the answer? Let's explore that interpretation, because it might make the problem less trivial.
If he only swapped cubes that were originally in the segment, he might move them out, but then the cubes originally outside could also move if swapped, but they are not originally in the segment, so that would involve moving a cube not originally in the segment, which might be considered moving a cube that was not among "cubes which were on the positions between l and r". The phrase "swapped only cubes which where on the positions between l and r" is ambiguous. Could it mean that the only cubes he touched (i.e., moved) were those that were initially in the segment? If he swaps an inside cube with an outside cube, he touches both cubes: the inside cube (which was in the segment) and the outside cube (which was not). So he would be touching a cube not originally in the segment, which would violate "swapped only cubes which where on the positions between l and r". That suggests he cannot touch outside cubes at all. So the outside cubes must remain in place. Therefore our interpretation is correct.
But what about moving an inside cube to outside via a swap with an outside cube? That would move an outside cube into the segment, so that outside cube is moved, violating the condition. So not allowed.
Thus, the only allowed operation is to permute the cubes within the segment without affecting outside. So our condition is correct.
Now, I've seen a problem "Cubes" where the twist is that you need to check both conditions, but many people mistakenly only check the multiset condition inside, forgetting that outside must be exactly same. The samples illustrate that: sample 2 shows outside mismatch. So it's a common mistake.
Thus, the solution is as described.
Now we can produce final answer.
I'll write the code accordingly.
Also, note: the problem uses words "TRUTH" and "LIE" in uppercase.
Now, I'll craft the final solution.
Edge Cases: n=1, l=1, r=1: Then there are no outside positions. So always TRUTH if multiset of whole array matches (which is guaranteed). So output TRUTH.
Check: a=[1], b=[2] would violate guarantee (since b must be permutation of a). So input will have a and b same? Actually with n=1, if Stepan rearranges cubes within [1,1], he can't change order; so b must equal a. The guarantee ensures that he only rearranged cubes, so b must be a permutation of a; with n=1, b must equal a. So TRUTH.
Now, code:
import sys
def solve():
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
l = int(next(it))
r = int(next(it))
a = [int(next(it)) for _ in range(n)]
b = [int(next(it)) for _ in range(n)]
# Check outside positions
for i in range(n):
pos = i + 1
if pos < l or pos > r:
if a[i] != b[i]:
print("LIE")
return
# Check frequencies inside
freq_a = [0] * (n + 1) # since numbers up to n
freq_b = [0] * (n + 1)
for i in range(l-1, r):
freq_a[a[i]] += 1
freq_b[b[i]] += 1
for v in range(1, n+1):
if freq_a[v] != freq_b[v]:
print("LIE")
return
print("TRUTH")
if name == "main":
solve()
We could also combine the checks: after verifying outside equality, we could simply check that the multisets of a[l-1:r] and b[l-1:r] are equal. That's what we did.
Given constraints up to 1e5, O(n) is fine.
Now, let's consider memory: freq arrays of size n+1, fine.
Potential overflow? No.
Now, final answer.
But wait, what about Python's indexing: range(l-1, r) includes indices from l-1 to r-1 inclusive. Since r is inclusive, we need up to r-1. That's correct.
Testing with example: n=5, l=2, r=4 => l-1=1, r=4? Actually range(l-1, r) means range(1,4) gives indices 1,2,3. That's positions 2,3,4 inclusive, correct.
Thus code is fine.
Now, produce final answer with explanation.
Given the simplicity, the explanation in final answer will describe reasoning: outside must be unchanged, inside must have same multiset, and due to guarantee the latter is equivalent to checking outside equality, but we can just check both.
I'll write a concise explanation.
Now, produce final output. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
l = int(next(it))
r = int(next(it))
a = [int(next(it)) for _ in range(n)]
b = [int(next(it)) for _ in range(n)]
# 1. Positions outside [l, r] must be unchanged
for i in range(n):
pos = i + 1
if pos < l or pos > r:
if a[i] != b[i]:
print("LIE")
return
# 2. Inside the segment the multiset of numbers must be the same
freq_a = [0] * (n + 1) # numbers are between 1 and n
freq_b = [0] * (n + 1)
for i in range(l - 1, r):
freq_a[a[i]] += 1
freq_b[b[i]] += 1
for v in range(1, n + 1):
if freq_a[v] != freq_b[v]:
print("LIE")
return
print("TRUTH")
if name == "main":
solve()