In this problem at each moment you have a set of intervals. You can move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d. Also there is a path from interval I1 from our set to interval I2 from our set if there is a sequence of successive moves starting from I1 so that we can reach I2.
Your program should handle the queries of the following two types:
"1 x y" (x < y) — add the new interval (x, y) to the set of intervals. The length of the new interval is guaranteed to be strictly greater than all the previous intervals.
"2 a b" (a ≠ b) — answer the question: is there a path from a-th (one-based) added interval to b-th (one-based) added interval?
Answer all the queries. Note, that initially you have an empty set of intervals.
Input
The first line of the input contains integer n denoting the number of queries, (1 ≤ n ≤ 100). Each of the following lines contains a query as described above. All numbers in the input are integers and don't exceed 109 by their absolute value.
It's guaranteed that all queries are correct.
Output
For each query of the second type print "YES" or "NO" on a separate line depending on the answer.
Examples
Input
5
1 1 5
1 5 11
2 1 2
1 2 9
2 1 2
Output
NO
YES
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to parse the problem carefully. The constraints: n <= 100 queries. So we can afford O(n^3) or similar.
We have a set of intervals added over time. Each addition is assigned an index (1,2,...) in order of addition. Queries of type 2 ask if there is a path from interval a to interval b according to the current set of intervals? The path is defined by moves: from interval I = (a,b) to interval J = (c,d) if either c < a < d or c < b < d. That is, one endpoint of I (either left or right) lies strictly inside J (i.e., between c and d, exclusive? The condition is c < x < d, not inclusive? The problem statement: "c < a < d or c < b < d". So endpoints must be strictly inside J, not on the boundary. Also note that the intervals are presumably open? But we treat as given. So we can move from I to J if J's interval covers either endpoint of I (but not exactly at the endpoints). Also note that the move is directional? The condition as given: "You can move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d." So it's directed? It says "move from interval (a,b) to interval (c,d)". So it defines a directed edge from I to J if J covers an endpoint of I. But then path definition: "there is a path from interval I1 to interval I2 if there is a sequence of successive moves starting from I1 so that we can reach I2." So the graph is directed.
But is the move symmetric? Not necessarily. Because from J to I, we would need a < c < b or a < d < b. Not necessarily true. So we need to treat as directed graph.
However, in typical solution discussions for similar problems (like Codeforces problem "Intervals" maybe?), the graph connectivity may become undirected due to properties? Let's think.
We have intervals being added with strictly increasing length (y - x) each time. That's important.
Given intervals (x, y) with length L = y - x. The new intervals have length strictly greater than all previous intervals. This property likely simplifies connectivity.
We need to answer connectivity queries (reachability) in a dynamic graph as intervals are added. n <= 100, so we can just build graph after each addition and answer queries by BFS/DFS.
But careful: The path existence is based on current set of intervals at the time of query? The problem statement: "Your program should handle the queries ... Answer all the queries." It doesn't specify if the set includes all intervals added so far. Since queries are interleaved, we should consider the state after previous queries. Typically, when you get query type 2 a b, you consider the set of intervals that have been added up to that point (including both a and b obviously, because they were added earlier). Since a and b refer to intervals that were added earlier (they are indices), they exist. So the graph includes all intervals added so far.
Given n <= 100, we can just maintain the list of intervals, and for each query type 2, perform a DFS from interval a to see if we can reach interval b.
But wait: The moves are defined between intervals based on coordinates, not based on indices. So we need to check condition between intervals.
Given small n, we can compute adjacency matrix after each addition, and then answer queries with BFS/Floyd-Warshall perhaps.
But there might be a nuance: The graph is directed; we need to check reachability from a to b. We could compute reachability by BFS each query.
Given n ≤ 100, O(n^3) overall is fine.
Let's check the sample:
5 queries:
1 1 5 -> interval1: (1,5) length=4
1 5 11 -> interval2: (5,11) length=6 (greater than 4) OK.
2 1 2 -> ask if path from 1 to 2? Let's check: from (1,5) to (5,11): can we move from 1 to 2? Condition: from (1,5) to (5,11): c=5, d=11; check c < a < d? a=1: 5<1<11 false; c < b < d? b=5: 5<5<11 false because 5<5 is false. So no direct edge. Any intermediate? Only two intervals, so answer NO. Output NO matches.
Then 1 2 9 -> interval3: (2,9) length=7 (greater than 6? Actually 9-2=7 >6, yes).
Then 2 1 2 -> ask again from 1 to 2. Now we have three intervals. Check if there is path 1->?->2.
Consider moves: from (1,5) to (2,9)? Condition: c=2,d=9; check c < a < d? 2<1<9 false; c < b < d? 2<5<9 true (2<5<9). So edge 1->3 exists. From (2,9) to (5,11)? c=5,d=11; check 5<2<11 false; 5<9<11 true (5<9<11). So edge 3->2 exists. Thus path 1->3->2 exists. So answer YES. Matches output.
Thus simple BFS works.
Given the length property, maybe connectivity can be determined by some simpler condition, but with n=100 it's unnecessary.
So solution: maintain a list of intervals (x_i, y_i) added. For each query type 2, we run BFS/DFS from node a (index) to see if we can reach node b. Directed graph: for each node i, we can iterate over all nodes j (including i? self? Not needed) and check condition: can we move from i to j? That is, if (x_j < x_i < y_j) OR (x_j < y_i < y_j). Since intervals are distinct? Possibly same coordinates? But input: x < y and lengths are increasing, so intervals cannot be identical. Coordinates may coincide? Possibly boundaries can be equal? For condition, it's strict inequality. So we need to check strictly inside.
Implementation: for each pair (i,j), if (x_j < x_i < y_j) or (x_j < y_i < y_j), then edge from i to j.
Then BFS/DFS.
We must be careful about one-based indexing: when query says "a-th added interval", we map index to node number.
Complexities: O(n^3) worst-case (since BFS per query O(n^2) and up to n queries, but n=100 -> 1e6 operations trivial).
Alternatively, we could precompute transitive closure after each addition, but not needed.
Edge Cases: When a == b? The query says a ≠ b. So no self queries.
Also note that intervals added have increasing length. That property might ensure no cycles? Not needed.
But wait: The condition for move is: from (a,b) to (c,d) if c < a < d OR c < b < d. But note that it's "c < a < d" not "a < c < d"? So it's specifically the first interval's endpoint lies inside the second interval. So it's directed from first to second. So we compute directed edges accordingly.
Now test some more:
If we have interval I: (1,10) and J: (2,3) (but length of J is less than I; but length property ensures new intervals are longer, so later intervals are longer, earlier are shorter). So later intervals are longer. So if I is earlier (shorter) and J is later (longer), can we move from I to J? Condition: x_J < x_I < y_J? Since J is longer, maybe it covers I's endpoints. For example earlier intervals shorter, later longer may cover their endpoints. Similarly, from J to I: later to earlier: may not hold because earlier is shorter and may not cover endpoints of longer. So graph is typically directed from earlier (shorter) to later (longer)? But check: In sample, from interval1 (length 4) to interval3 (length 7) we had edge; from 3 to 2 (length 6) also edge. So direction can be both ways? 3 to 2: 2 is earlier than 3? Actually ordering: interval1 (idx1), interval2 (idx2), interval3 (idx3). So interval2 is earlier than interval3 but interval2 length=6, interval3 length=7, so later longer. Edge from 3 (longer) to 2 (shorter) works: condition x_2 < x_3 < y_2? x_2=5, x_3=2 -> 5<2 false; x_2 < y_3 < y_2? y_3=9 -> 5<9<11 true. So edge from longer to shorter if shorter covers an endpoint of longer. So direction is not simply by length; it depends on inclusion.
Thus we must compute both directions for each pair.
Implementation: For each i, for each j, if i != j, if (x_j < x_i < y_j) or (x_j < y_i < y_j) then add directed edge i->j.
Now, what about multiple intervals? We'll just compute adjacency on the fly.
Since n is small, we can even precompute reachability after each addition using Floyd-Warshall to answer queries quickly. But BFS each query is fine.
But note: There could be up to 100 queries, each type 2 could be up to 100. BFS O(n^2) each => 100*10000 = 1e6 operations, fine.
Now careful: The intervals coordinates are up to 1e9, but we only compare integers, fine.
Now I'd produce solution.
But wait: The problem statement: "You can move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d." This is a directed condition: from (a,b) to (c,d). So we interpret as from interval i to interval j: if x_j < x_i < y_j or x_j < y_i < y_j. That's what we did.
Now we should answer queries.
We'll parse input:
Read n.
Initialize list intervals = [] (store tuples (x,y)).
For each query i from 1..n:
read type.
if type == 1:
read x, y (x<y). Append (x,y). (We don't need to store index separately as order is list index+1).
else:
read a,b (a != b). Convert to zero-based indices: a-1, b-1.
Perform DFS/BFS from node a-1 to see if we can reach node b-1.
But careful: The graph is dynamic; we only have intervals up to current length. The BFS will consider all intervals currently in list (so up to that point). That's correct.
Now BFS: Use stack/queue. For each node i, we can compute outgoing edges on the fly: loop over all j (0..len(intervals)-1) and check condition. That's O(N) per neighbor check. Since N ≤ 100, it's fine.
Alternatively, we could precompute adjacency matrix after each addition to speed up, but not necessary.
Let's test with sample:
After first two intervals: intervals = [(1,5), (5,11)]. BFS from 0 to 1: from 0, edges: check j=1: x1=5, y1=11; condition: 5<1<11? false; 5<5<11? false (5<5 false). So none. Not reachable.
After adding third: intervals = [ (1,5), (5,11), (2,9) ]. BFS from 0: neighbors: j=2: (2,9): 2<1<9? false; 2<5<9 true -> neighbor. So push 2. Then from 2: neighbors: j=1: (5,11): 5<2<11 false; 5<9<11 true -> neighbor 1. So reachable.
Output YES.
So solution works.
Now consider edge cases: intervals could have negative coordinates? "All numbers in the input are integers and don't exceed 10^9 by their absolute value." So could be negative. Works.
Now about the length condition: "The length of the new interval is guaranteed to be strictly greater than all the previous intervals." This ensures that intervals lengths are distinct and increasing. This might help with some more efficient solutions but we ignore.
But we must be careful: When we add a new interval, its length > all previous. That could be used to guarantee something like later intervals are longer. So we have property: for i < j, length_i < length_j. This may affect potential cycles? Not needed for brute force.
Thus solution is straightforward.
But let's double-check interpretation: "move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d." This is unambiguous.
Now, what about the condition for path: "Also there is a path from interval I1 from our set to interval I2 from our set if there is a sequence of successive moves starting from I1 so that we can reach I2." So directed path.
Thus answer "YES" if reachable.
Now let's think about potential pitfalls: Could there be a scenario where a path exists but BFS fails because we need to consider intermediate intervals that were added after the start? But all intervals are present in the set at query time. So fine.
What about multiple queries? Each query type 2 is answered based on current set, which includes all intervals added so far, including those added after the two intervals? But the query refers to indices of intervals that have been added previously. It's guaranteed that a and b are valid indices that have been added before this query? The problem statement: "2 a b (a ≠ b) — answer the question: is there a path from a-th (one-based) added interval to b-th (one-based) added interval?" Since the queries are processed in order, and a,b refer to intervals added earlier, they must have been added before the query. So safe.
Now, is there any possibility that the intervals are not necessarily added in increasing order of length? The problem statement: "The length of the new interval is guaranteed to be strictly greater than all the previous intervals." So yes, each addition yields a longer interval than any previous.
Now, we need to implement.
But wait: There is a nuance: The condition for moving is "c < a < d or c < b < d". It uses letters a,b,c,d. But careful: In the query "1 x y", x<y. So the interval is (x,y). Then when we later compute move from interval i to j, we need to compare i's coordinates (a_i,b_i) with j's coordinates (a_j,b_j). The condition uses the second interval's left as c and right as d. So we need to check if the second interval contains one of the first interval's endpoints strictly.
One potential confusion: The condition is symmetrical? The typical Codeforces problem "The Great Wall" or "Intervals" maybe? Actually, this problem is from Codeforces Round? Possibly problem "Paths in a Graph" but not sure. But anyway.
Now, with n ≤ 100, we can just implement BFS.
Let's write code:
def can_move(i, j, intervals):
xi, yi = intervals[i]
xj, yj = intervals[j]
# from i to j if xj < xi < yj or xj < yi < yj
return (xj < xi < yj) or (xj < yi < yj)
Then BFS: visited = [False]*len(intervals); queue = [a]; visited[a]=True; while queue: u = queue.pop(); for v in range(len(intervals)): if not visited[v] and can_move(u, v, intervals): if v == b: return True; visited[v]=True; queue.append(v). Return False.
Edge Cases: Could there be cycles? Possibly, but BFS will handle.
Now, what about the scenario where intervals are very large? Not an issue.
Now complexity: O(Q * N^2) with Q ≤ 100, N ≤ 100 => up to 1e6 checks.
Thus solution is trivial.
But wait, the constraints: n (number of queries) is up to 100. That's extremely small. But typical Codeforces problems have n up to 100? Possibly the problem is from some older contest or maybe it's simplified. Let's double-check: The problem statement in the text says "1 ≤ n ≤ 100". That's indeed small. So brute-force is fine.
Now, before finalizing, check if there is any trick: The intervals added have strictly increasing length. This might be used to prove that the reachability relation is transitive in some sense? But not needed.
Now, we can also precompute reachability matrix after each addition using Floyd-Warshall, then answer queries in O(1). But BFS is simpler.
Now, we need to output for each query of type 2: "YES" or "NO". Uppercase.
Thus solution is straightforward.
But wait: The problem might be misinterpreted: Could the moves be bidirectional? The phrase "You can move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d." That suggests a directed move. But then they ask "is there a path from a-th added interval to b-th added interval?" So they consider directed path. So we follow that.
But in some problems like this, the condition is symmetric because they consider undirected connectivity, but here it's explicitly directed. We'll keep directed.
Now let's test on some custom cases.
Case 1:
Add (1,3) len=2
Add (2,5) len=3
Add (0,4) len=4 (longer)
Query 2 1 2?
Interval1 (1,3) to interval2 (2,5): check from 1 to 2: 2<1<5? false; 2<3<5? true -> edge. So reachable. Output YES.
Case: Query 2 2 1? from interval2 (2,5) to interval1 (1,3): from 2 to 1: check 1<2<3? true? Actually x1=1,y1=3: 1<2<3 true => edge. So also reachable. So sometimes both directions.
Now, it's possible that there is a directed path from a to b but not from b to a. So our BFS must respect direction.
Now consider more complex: maybe need to handle loops but fine.
Given n=100, we can also compute full transitive closure after each addition: For each new node k, compute edges from/to others; then update closure using Floyd-Warshall incremental? Not needed.
Now, confirm with the sample.
Alright.
Now, the problem statement might be from Codeforces 499B? Actually I recall a problem "J. Intervals" or something. But anyway.
Now, a nuance: The intervals are added, and length increases. This property might cause that later intervals are longer, so they are more likely to contain endpoints of earlier intervals. But also earlier intervals might contain endpoints of later intervals? Possibly if later interval's endpoints lie inside earlier intervals. However, earlier intervals are shorter, so if later interval is longer, it's more likely to cover earlier intervals' endpoints, but it could also have endpoints that fall inside earlier intervals if earlier intervals are large enough? But since later interval is longer, its endpoints are farther apart. Could one endpoint of later interval lie inside an earlier interval? Yes, if earlier interval is large enough? But earlier interval is shorter, so its length is smaller. For an earlier interval (shorter) to contain an endpoint of a later interval (longer), the earlier interval would need to have length large enough to cover that endpoint, but since it's shorter, it's possible if the later interval is not fully covering earlier but just one endpoint falls inside the earlier interval. For example: earlier interval (1,10) length=9; later interval (2,12) length=10>9; endpoint 2 lies inside (1,10) because 1<2<10, and endpoint 12 does not. So from later to earlier, there can be an edge if earlier contains later's endpoint. So edges can go both ways.
But the increasing length property ensures that lengths are strictly increasing. So we can rely on that? Not needed.
Now, code.
Implementation details:
We'll read n. Then loop over n lines.
We'll maintain a list intervals = []. For each line, read as string, split, parse first integer as t. If t==1, then read x,y, append (x,y). If t==2, then read a,b, convert to int, then run BFS. For BFS we can implement with queue.
We need to be careful with input reading: may have extra spaces. Use sys.stdin.
Now, BFS function:
def reachable(start, target, intervals):
n = len(intervals)
visited = [False]*n
q = [start]
visited[start] = True
while q:
u = q.pop() # stack for DFS, or use pop(0) for BFS; we can use collections.deque
for v in range(n):
if not visited[v]:
xi, yi = intervals[u]
xj, yj = intervals[v]
if (xj < xi < yj) or (xj < yi < yj):
if v == target:
return True
visited[v] = True
q.append(v)
return False
We can use deque for efficiency, but n small.
Now, output for each query type 2.
Potential bug: In BFS we might need to consider that we can move from u to v if condition holds. We used condition as described.
Now, are there any self-loops? Condition for moving from interval to itself? a != b in query but not in move? Typically moving to itself is not considered because it's trivial; but even if we consider self, condition would be x_i < x_i < y_i? That's false because x_i < x_i false. So no self-edge. So we can ignore.
Now, test with sample.
Now, we need to consider that the set of intervals may include many. BFS may be called many times; but n=100 so fine.
One more test: Suppose intervals: (1,2) length 1; (0,5) length 5; (3,4) length 1? But length increasing, so can't have decreasing length. So we are fine.
Now, is there any subtlety regarding "c < a < d" meaning the first interval's left endpoint is strictly inside second interval? Yes.
Now, we must also consider that intervals are added in order, and query may ask about intervals that are not yet added? No, because a and b refer to intervals that have been added before. The problem guarantees queries are correct.
Now, what if there are multiple edges? BFS covers all.
Thus solution is ready.
But maybe the problem expects to use the increasing length property to simplify connectivity checking, but we don't need.
Now, let's see if there is any potential misinterpretation: The condition says "c < a < d or c < b < d". This is a move from (a,b) to (c,d). Note that (a,b) is the current interval, (c,d) is the target. So the target interval must contain one of the endpoints of the current interval. So it's directed: from current to target.
So our directed graph is correct.
Now, confirm with sample: edge from (1,5) to (2,9) because 2<5<9. Yes.
Now, what about a scenario where there is a path from a to b but not from b to a? BFS correctly determines directed reachability.
Now, we can code.
But wait, there is a nuance: The condition for moving is "if and only if". So it's exactly that. So our adjacency is correct.
Now, consider scenario where intervals have endpoints that coincide? The problem says x < y, and lengths are increasing. But endpoints could be equal across intervals? Possibly, e.g., interval1 (1,5), interval2 (5,10). Their endpoints coincide at 5. But condition uses strict inequalities, so moving from (1,5) to (5,10) would check 5<1<10? false; 5<5<10? false because 5<5 false. So no edge. Similarly from (5,10) to (1,5) check 1<5<5? false; 1<10<5? false. So no edge. So coincidence yields no moves.
Now, what about intervals that are nested? For example, (2,8) and (3,6). Lengths: (3,6) is shorter than (2,8) if added later? But lengths increasing, so later interval is longer, so (2,8) longer than (3,6) if (2,8) added later? Actually length of (2,8)=6, (3,6)=3, so longer added later. So (2,8) longer; (3,6) shorter. Then edges: from shorter to longer: check (3,6) to (2,8): 2<3<8 true, so edge from short to long. From long to short: (2,8) to (3,6): 3<2<6 false; 3<8<6 false. So directed from shorter to longer only. That's typical. But if longer added first? That can't happen because lengths always increase. So later intervals are always longer. So the graph is acyclic? Possibly not: Could there be a cycle? Consider intervals: I1 (a1,b1) with length L1, I2 (a2,b2) with L2 > L1, I3 (a3,b3) with L3 > L2. Could there be a directed cycle I1 -> I2 -> I3 -> I1? For I3 -> I1, need I1 to contain an endpoint of I3. But I1 is shortest, I3 is longest. It's possible? For I3 -> I1, condition: x1 < x3 < y1 or x1 < y3 < y1. Since I1 is short, and I3 is long, it's unlikely that I1 contains an endpoint of I3 because I1's interval is short while I3's endpoints are far apart. However, if I1 is placed in such a way that one endpoint of I3 falls inside I1, it's possible. Example: I1 = (0,10) length 10; I2 = (5,20) length 15; I3 = (15,30) length 15? Actually L3 must be > L2 > L1. Let's try: I1: (0,10) L=10; I2: (5,21) L=16 >10; I3: (9,30) L=21 >16. Check I1 -> I2? from I1 to I2: x2=5,y2=21; condition: 5<0<21 false; 5<10<21 true -> edge I1->I2. I2->I3: x3=9,y3=30; condition: 9<5<30 false; 9<21<30 true -> edge I2->I3. I3->I1: from I3 to I1: x1=0,y1=10; condition: 0<9<10 true -> edge I3->I1. So cycle exists. All lengths increasing: I1 L=10, I2 L=16, I3 L=21. So cycles possible. So graph may have cycles, BFS still fine.
Thus BFS is needed.
Now, is there any other nuance? The problem asks "is there a path from a-th added interval to b-th added interval?" It's directed path. So we need to check reachability in directed graph.
Now, answer should be YES or NO.
Edge Cases: When a and b are the same? Problem says a ≠ b, so ignore.
Now, multiple type 2 queries: we output each on its own line.
Now, code.
Will use Python.
Pseudo:
import sys
def main():
input_data = sys.stdin.read().strip().split()
if not input_data: return
it = iter(input_data)
n = int(next(it))
intervals = []
out_lines = []
for _ in range(n):
typ = int(next(it))
if typ == 1:
x = int(next(it)); y = int(next(it))
intervals.append((x, y))
else:
a = int(next(it)); b = int(next(it))
a -= 1; b -= 1
# BFS from a to b
m = len(intervals)
visited = [False]*m
from collections import deque
q = deque()
q.append(a)
visited[a] = True
reach = False
while q:
u = q.popleft()
if u == b:
reach = True
break
xu, yu = intervals[u]
for v in range(m):
if not visited[v]:
xv, yv = intervals[v]
if (xv < xu < yv) or (xv < yu < yv):
visited[v] = True
q.append(v)
out_lines.append("YES" if reach else "NO")
sys.stdout.write("\n".join(out_lines))
if name == "main":
main()
That should work.
But wait: In BFS, we check condition from u to v. Our condition is correct: (xv < xu < yv) or (xv < yu < yv). That's the condition to move from u to v.
Now, confirm sample: The code would produce NO and YES.
Now, test with custom:
Input:
3
1 0 10
1 5 20
2 1 2
Expected: from (0,10) to (5,20): 5<0<20 false; 5<10<20 true -> YES. So output YES.
Now, test with reverse query 2 2 1: from (5,20) to (0,10): 0<5<10 true -> YES. So both ways possible.
Now, test with more complex.
Given n <= 100, we can also precompute transitive closure after each addition to answer queries faster, but not needed.
Potential optimization: Since n is small, we could compute adjacency list after each addition and then run BFS. But computing adjacency on the fly per BFS is fine.
But note: For each BFS, we might recompute condition many times. However, with n=100 and BFS O(n^2) per query, worst-case 100 * 100 * 100 = 1e6 condition checks, trivial.
Now, some might think that we need to consider the "set" of intervals: they are added and never removed. So it's fine.
Now, any pitfalls with reading? Use sys.stdin.
Now, but wait: The problem might be from Codeforces and n can be up to 100,000? No, the problem statement here says n <= 100. But I recall a Codeforces problem "D. Intervals" or something with n up to 100,000 and they use union-find with segment tree, etc. However the given constraints here are small. Maybe it's a simplified version? The problem title might be "Problem D. Interval" but I'm not sure. But the given input constraints say 1 ≤ n ≤ 100. So it's small. So brute-force is intended? Possibly yes.
Let me search memory: There is a Codeforces problem "C. The Great Wall" or "B. Intervals". Actually I recall a problem "Intervals" from some contest where you have operations add interval and query if two intervals are connected via overlaps of endpoints, but with directed moves? Might be Codeforces Round #277? Actually there is Codeforces 466C? Not sure. But the small constraints indicate it's probably from an educational contest or something.
But the problem statement says: "Your program should handle the queries ... n up to 100". So it's fine.
Now, what about memory? trivial.
Now, before finalizing, consider if there is any nuance about "move from interval (a,b) to interval (c,d) if and only if c < a < d or c < b < d." It says "c < a < d or c < b < d". The variables a,b,c,d are the coordinates. But careful: In the description, they used (a,b) and (c,d) to denote intervals. Then in query "1 x y", they use x and y. So we need to be consistent.
Now, note: The intervals are added with "1 x y (x < y)". So x is left endpoint, y is right endpoint.
Now, ensure we handle the condition correctly: from i to j if (x_j < x_i < y_j) or (x_j < y_i < y_j). Yes.
Now, check if we need to consider that the intervals are open? The condition uses strict inequality, so endpoints are not considered inside.
Now, edge case: If x_j < x_i and y_i < y_j, then both endpoints of i are inside j, but condition still holds because either holds. So it's fine.
Now, could there be multiple edges? Not relevant.
Now, implement BFS as described.
Double-check BFS termination: we check if v == b when we find it; but we also check at start? In our loop, we pop u and if u == b then we have reached. However we might have started with a == b? But a != b, so not needed. But we can still include early check.
In our BFS, we set visited[a] = True and then while q. Inside, after popping u, if u == b, we set reach = True and break. Alternatively, we could check when we add neighbor. Both fine.
Now, what about if a is not present? It will be present because a-th interval was added earlier.
Now, also we need to consider that the graph may have many nodes, but BFS will traverse all reachable nodes.
Now, answer accordingly.
Now, we can think about testing a scenario with many intervals and a complex graph.
We'll write a simple test:
Input:
6
1 0 10
1 5 15
1 12 20
2 1 3
2 2 3
2 3 1
We need to compute manually:
Intervals: I1 (0,10), I2 (5,15), I3 (12,20). Check edges:
I1->I2? x2=5<0<15? false; 5<10<15 true -> edge.
I1->I3? x3=12<0<20 false; 12<10<20 false -> no.
I2->I1? x1=0<5<10 true -> edge. So bidirectional between I1,I2.
I2->I3? x3=12<5<20 false; 12<15<20 true -> edge.
I3->I2? x2=5<12<15 true -> edge. So bidirectional between I2,I3.
I3->I1? x1=0<12<10 false; 0<20<10 false -> no.
Thus graph: I1 <-> I2 <-> I3, but no direct I1-I3. So reachability:
1->3? 1->2->3, so YES.
2->3? direct, YES.
3->1? 3->2->1, so YES.
So outputs would be YES, YES, YES. But check with code.
Now, BFS works.
Now, the solution seems correct.
Now, consider if the problem expects "YES"/"NO" in uppercase exactly.
Now, think about potential pitfalls: reading input with large numbers? Not a problem.
Now, confirm time complexity: For each type 2, we may have up to M nodes where M is number of intervals added so far (<=100). BFS loops over all nodes for each node we pop: worst-case O(M^2) per query. With M <= 100, and Q <= 100, worst-case ~10^6 operations, fine.
Thus solution is acceptable.
But I'll double-check the condition once more: "You can move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d." This implies that we move from interval (a,b) to (c,d) if either endpoint of the source lies strictly inside the target. Not the other way around. So it's directed. So our code is correct.
One more nuance: When we add new interval, does it affect previous connectivity? It may create new paths. BFS at query time accounts for all intervals added so far.
Thus answer.
Now, I think we have the solution.
But wait: The problem statement says: "You can move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d. Also there is a path from interval I1 from our set to interval I2 from our set if there is a sequence of successive moves starting from I1 so that we can reach I2." The phrase "successive moves starting from I1" means we start at I1 and apply the move rule repeatedly. So directed.
So solution stands.
Now, I'll produce final answer with explanation.
But before final, perhaps there is an interpretation that the moves are undirected because the condition might be symmetric in some sense? But they explicitly say "from (a,b) to (c,d)" so it's directed. However, sometimes in problems they define a symmetric relation but phrase it that way. But they would typically say "you can go from interval I to interval J if ..." which is directed. So we stick with directed.
I'll check sample: They said output NO then YES. That matches our directed interpretation.
Now, what about a case where they might expect undirected? Suppose we had intervals (1,5) and (5,11) with no direct connection. But if we consider undirected, then still no connection because condition is not symmetric? Actually if it were undirected, they might define connection if either interval contains an endpoint of the other. That would yield connection between (1,5) and (5,11) if we consider inclusive boundaries? But condition is strict, so no. So not connected. So our directed yields same. However, if we had intervals (1,5) and (2,3) (but lengths would violate increasing? Not necessarily if (2,3) added later? But length 1 < 4, cannot be later. If (1,5) added first length 4, (2,3) length 1 cannot be added later because length must be greater. So such not possible. So maybe the increasing length property ensures some properties, but not needed.
Now, maybe there is an even simpler solution: because lengths are increasing, the directed graph may have some monotonic properties that make connectivity check equivalent to something like: interval a can reach interval b if and only if the left endpoint of a is within b or something? But not exactly.
Given the constraints, we can skip that.
Now, final solution: We'll implement BFS for each query type 2.
I'll write a more efficient BFS by precomputing adjacency matrix after each addition and updating reachability? But it's not necessary.
But we can also compute transitive closure using Floyd-Warshall after each addition to answer queries O(1). Since n=100, we can also just recompute Floyd-Warshall from scratch after each addition; O(n^4) maybe? Actually after each addition, we have up to M nodes. Floyd-Warshall O(M^3). With M up to 100, and up to 100 additions, if we recompute after each addition, total O(100^4)=1e8, borderline but still okay in Python? 1e8 might be a bit high but maybe okay with optimizations? But BFS per query is simpler and less total operations.
But note: If we have many type 2 queries, say up to 100, each BFS O(M^2) => 100 * 100^2 = 1e6, which is fine.
So BFS is better.
Now, we need to be careful: We might call BFS many times, but we recompute edges each time. Could also precompute adjacency list after each addition to speed up neighbor iteration to O(M) per node, but we already do that.
Actually in BFS, for each node u we iterate over all v to check condition, which is O(M). If M=100 and BFS visits all nodes, that's O(M^2)=10000 per BFS. With up to 100 BFS, that's 1e6. With each condition being simple integer comparisons, it's trivial.
Thus solution is fine.
Now, I'll produce the final answer with explanation.
But the user asks "In this problem ... Your program should handle ... Answer all the queries." They want code? The prompt: "Answer all the queries." It's a problem statement. Usually we need to produce a solution description and code. The user likely expects a solution explanation and code.
Given this is a coding problem, we should output the code. The conversation is: The user provided problem statement, and we need to respond with solution. So we should provide explanation and then code.
Thus final answer: We'll describe approach: maintain list of intervals; for each type 2 query, perform BFS on directed graph defined by condition; output YES/NO.
Now, we should note that because n <= 100, BFS is efficient.
Now, I'll write the final message with explanation and Python code.
But the instruction: "Answer all the queries." might be part of problem statement, not instruction to us. So we just provide solution.
Thus final answer includes code.
Now, ensure code is correct. I'll double-check BFS implementation for directedness.
I'll write:
def can_reach(start, end, intervals):
n = len(intervals)
visited = [False]*n
q = deque([start])
visited[start] = True
while q:
u = q.popleft()
if u == end:
return True
xu, yu = intervals[u]
for v in range(n):
if not visited[v]:
xv, yv = intervals[v]
if (xv < xu < yv) or (xv < yu < yv):
visited[v] = True
q.append(v)
return False
Yes.
Now, note: For BFS, we use deque for O(1) popleft.
Now, read input:
import sys
from collections import deque
def main():
data = sys.stdin.read().strip().split()
if not data: return
it = iter(data)
n = int(next(it))
intervals = []
out = []
for _ in range(n):
t = int(next(it))
if t == 1:
x = int(next(it)); y = int(next(it))
intervals.append((x, y))
else:
a = int(next(it)) - 1
b = int(next(it)) - 1
reachable = can_reach(a, b, intervals)
out.append("YES" if reachable else "NO")
sys.stdout.write("\n".join(out))
if name == "main":
main()
But we need to define can_reach inside main or outside. We'll define as nested or separate.
Now, test with sample:
Input:
5
1 1 5
1 5 11
2 1 2
1 2 9
2 1 2
Our code: after first two adds, intervals = [(1,5), (5,11)]. Query 2 1 2: a=0, b=1. BFS from 0: neighbors? check v=1: xv=5,yv=11: 5<1<11? false; 5<5<11? false -> no neighbor. So not reachable. Output NO. Then add (2,9). Then query 2 1 2: BFS from 0: neighbors: v=2? indices: 0,1,2. v=2: (2,9): 2<1<9? false; 2<5<9 true -> neighbor. visited[2]=True, q=[2]. Then process u=2: check v=0 (visited), v=1: (5,11): 5<2<11 false; 5<9<11 true -> neighbor, v=1 not visited, set visited, add to q. Then process u=1: u==b -> return True. So YES. Output YES. Matches.
Now, edge case: if start == end? Not possible.
Now, confirm reading: The first line is n. Then n lines each with query. In our reading with split, we treat all tokens. That's fine.
Now, check potential bug: When reading, we assume each line has correct number of tokens. It's guaranteed.
Now, final.
But wait: The constraints say n ≤ 100, but numbers can be up to 10^9, our code uses Python int.
Now, potential improvement: We could precompute adjacency matrix once and update after each addition to speed up BFS, but not needed.
Now, note that BFS uses visited list of length m, and we iterate over all v each time. In worst-case BFS visits all nodes, and for each visited node we scan all v, resulting in O(m^2) per BFS. With m=100, that's 10k per BFS. With up to 100 BFS, 1M operations. Should run in milliseconds.
Thus final answer.
Now, I'll produce final response with explanation and code.
But the user might be expecting a C++ solution? The problem statement didn't specify language. Typically in such platforms, they accept multiple languages, but as ChatGPT we can provide Python. The user didn't specify, but we can provide Python solution with explanation.
Alternatively, we could provide both. But I'll provide Python.
Now, the answer: We'll describe the approach:
Maintain a list of intervals.
For query type 2, perform BFS/DFS from interval a to interval b in a directed graph where there is an edge from i to j if x_j < x_i < y_j or x_j < y_i < y_j.
Output YES if reachable, else NO.
We'll also mention that constraints are small, so simple BFS suffices.
Now, deliver final.
```python
import sys
from collections import deque
def can_reach(start: int, target: int, intervals: list) -> bool:
"""BFS in the directed graph defined by the problem."""
n = len(intervals)
visited = [False] * n
q = deque([start])
visited[start] = True
code
while q:
u = q.popleft()
if u == target:
return True
xu, yu = intervals[u]
for v in range(n):
if not visited[v]:
xv, yv = intervals[v]
# move from u to v ?
if (xv < xu < yv) or (xv < yu < yv):
visited[v] = True
q.append(v)
return False
def main() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
intervals = [] # list of (x, y)
answers = []
code
for _ in range(n):
typ = int(next(it))
if typ == 1:
x = int(next(it))
y = int(next(it))
intervals.append((x, y))
else: # typ == 2
a = int(next(it)) - 1 # to 0‑based
b = int(next(it)) - 1
reachable = can_reach(a, b, intervals)
answers.append("YES" if reachable else "NO")
sys.stdout.write("\n".join(answers))