Domain: Algorithms & Data Structures

## Problem

There are `n` communication towers (numbered `1..n`) and `m` bidirectional wires.

Tower `i` accepts all integer frequencies in the interval `[l[i], r[i]]` (inclusive).

A tower `b` is called **accessible** from tower `a` if there exists an integer frequency `x` and a sequence of towers
`a = v1, v2, ..., vk = b`
such that:
- each consecutive pair `(vj, vj+1)` is connected by a wire, and
- **every** tower on the sequence accepts frequency `x` (i.e., `l[vj] ≤ x ≤ r[vj]` for all `j`).

Important: accessibility is **not transitive** because the frequency `x` may differ between different paths.

### Task
Output all towers that are accessible from tower `1`, in increasing order.

### Input
- First line: `n m` (`1 ≤ n ≤ 2⋅10^5`, `0 ≤ m ≤ 4⋅10^5`)
- Next `n` lines: `l[i] r[i]` (`1 ≤ l[i] ≤ r[i] ≤ 2⋅10^5`)
- Next `m` lines: undirected edges `v u` (`1 ≤ v,u ≤ n`, `v ≠ u`), no multi-edges.

### Output
Print the accessible tower indices from `1` in ascending order.

### Required sanity-checks (answer must also satisfy these)
Your approach must produce the following outputs on these additional testcases:

**Sanity Check A**
5 4
2 4
3 3
1 2
2 3
4 5
1 3
3 4
4 2
1 5

Expected output:
1 3 4 5

**Sanity Check B**
6 6
1 3
2 4
3 5
1 1
4 6
2 2
1 2
2 3
3 5
1 4
2 6
6 5

Expected output:
1 2 3 4 6

Grading rubric:
criterion: The answer explicitly restates that accessibility from tower 1 means: there exists a single integer frequency x such that all towers on the path accept x.
weight: 10
criterion: The answer reduces the problem to taking the union over frequencies x of the connected component of node 1 in the subgraph induced by towers active at x.
weight: 10
criterion: The answer defines the active interval of an edge (u,v) as [max(lu,lv), min(ru,rv)] and states the edge is usable exactly for x in that interval.
weight: 10
criterion: The answer uses an offline dynamic connectivity approach over x=1..MAX that adds each edge interval into a segment tree (or equivalent divide-and-conquer) over time.
weight: 10
criterion: The answer explicitly uses a DSU with rollback (persistent/undoable union-find) during the segment-tree traversal.
weight: 10
criterion: The answer states that at a leaf time x where tower 1 is inactive (x not in [l1,r1]), no towers should be marked/reported from that x.
weight: 10
criterion: The answer describes creating a merge-DAG (or DSU merge tree) where each union creates a new node with directed edges to the two merged component-nodes.
weight: 10
criterion: The answer states that a tower v is in the final output iff the singleton node for v is reachable from at least one marked component-node corresponding to find(1) at some x.
weight: 10
criterion: For the explicit input Sanity Check A: \"5 4\\n2 4\\n3 3\\n1 2\\n2 3\\n4 5\\n1 3\\n3 4\\n4 2\\n1 5\\n\" the answer states the output is exactly \"1 3 4 5\".
weight: 10
criterion: For the explicit input Sanity Check B: \"6 6\\n1 3\\n2 4\\n3 5\\n1 1\\n4 6\\n2 2\\n1 2\\n2 3\\n3 5\\n1 4\\n2 6\\n6 5\\n\" the answer states the output is exactly \"1 2 3 4 6\".
weight: 10

Answer:
### Key reformulation: union over frequencies of the component of tower 1

Fix an integer frequency `x`. Let `Active(x) = { i : l[i] ≤ x ≤ r[i] }`, and consider the subgraph induced by `Active(x)`.
For this `x`, the towers accessible from `1` using frequency `x` are exactly the vertices in the connected component of `1`
in that induced subgraph (and if `1 ∉ Active(x)`, then nothing is accessible for that `x`).

So the final answer is the union over all `x` (from 1 to `MAX = 2⋅10^5`) of `Comp1(x)`.

This is an offline dynamic connectivity problem over time `x` where edges are active on intervals.

---

### Step 1: Convert vertex activity to edge activity intervals

An undirected wire `(u,v)` can be used at frequency `x` iff both endpoints accept `x`, i.e.:
- `x ∈ [l[u], r[u]] ∩ [l[v], r[v]]`

So the edge `(u,v)` is active on the (possibly empty) integer interval:
\[
I_{uv} = [ \max(l[u],l[v]),\ \min(r[u],r[v]) ].
\]
If `I_{uv}` is non-empty, then across all `x ∈ I_{uv}`, union-find should have `u` and `v` connected.

We also only care about times `x` where tower `1` is active, i.e. `x ∈ [l[1], r[1]]`, but it’s simplest to compute everything and ignore leaves where `1` is inactive.

---

### Step 2: Offline dynamic connectivity by divide-and-conquer + DSU rollback

Use a segment tree (or recursive divide-and-conquer) over the time axis `x ∈ [1..MAX]`.

For each edge `(u,v)` with active interval `[L,R] = I_{uv}`, add it to the segment tree nodes that fully cover `[L,R]`
(standard “add interval to segment tree” technique). Total inserts are `O(m log MAX)`.

Traverse the segment tree with a DSU that supports rollback:
- At each node, apply (union) all edges stored in that node.
- Recurse to children.
- Roll back DSU to the previous snapshot when returning.

At a leaf corresponding to a single frequency `x`, DSU represents connectivity of the active-edge graph at that `x`.

---

### Step 3: Record all components containing vertex 1 without enumerating vertices per leaf

Directly listing all vertices in the DSU component of `1` at each `x` is too slow.

Instead, build a **component-merge DAG** during DSU unions:

- Create a “component node” for every original vertex `v` (a leaf node representing the singleton component `{v}`).
- Whenever DSU unions two components with roots `ra` and `rb`, create a new DAG node `new`,
  add directed edges `new -> node(ra)` and `new -> node(rb)`,
  and set `node(newRoot) = new` as the representative DAG node for the merged DSU component.

This is a standard way to persist a history of merges; the DAG is a forest of merge trees (a “DSU merge tree”).

Now, whenever we are at a leaf time `x` such that tower `1` is active (`l[1] ≤ x ≤ r[1]`), we record the current DAG representative:
- `mark node( find(1) )`.

A tower `v` is accessible from `1` iff the singleton node for `v` is reachable from at least one marked node in this merge DAG.

So after processing all leaves, run a DFS/BFS from all marked nodes in the merge DAG and mark all reachable singleton nodes.
Output the corresponding vertex indices.

---

### Correctness sketch

- For any `x`, DSU at leaf `x` equals the connected components of the graph containing exactly edges whose `I_{uv}` covers `x`,
  which is exactly the induced subgraph on towers that accept `x` (edges exist iff both endpoints accept `x`).
- Thus the DSU component of `1` at leaf `x` is exactly `Comp1(x)`.
- Marking the DSU-component DAG node at each such `x` and then taking reachability down the merge DAG recovers the union of all
  vertices that ever belonged to `Comp1(x)` for some `x`.

---

### Complexity

- Building edge intervals: `O(n + m)`
- Segment tree interval insertion: `O(m log MAX)` with `MAX = 2⋅10^5`
- DSU rollback traversal: `O((m log MAX) α(n))` amortized (each inserted edge causes one union on each visited node)
- Merge DAG size: `O(n + number_of_unions)` = `O(n + m log MAX)`
- Final DFS on merge DAG: linear in DAG size.

This fits the constraints.

---

### Sanity checks (why the expected outputs match)

- Sanity A:
  - Frequency `x=2` connects 1–3–4, so {1,3,4} reachable.
  - Frequency `x=4` connects 1–5, so {1,5} reachable.
  - Union is {1,3,4,5}.

- Sanity B:
  - `x=1` gives {1,4}.
  - `x=2` gives {1,2,6}.
  - `x=3` gives {1,2,3}.
  - Union is {1,2,3,4,6}.