Example 3 Let the terms of the sequence $\left\{a_{n}\right\}$ be
$$
2,5,5,5, \cdots, \underbrace{(3 n-1), \cdots,(3 n-1)}_{(2 n-1) \uparrow}, \cdots \text {. }
$$

Find $a_{n}$ and $S_{n}$.
Solution: By the formula for the sum of an arithmetic sequence, the sum of the first $(m-1)$ segments of the sequence $\left\{a_{n}\right\}$ is
$$
1+3+5+\cdots+(2 m-3)=(m-1)^{2} \text {. }
$$

It is evident that, if and only if
$$
(m-1)^{2}+1 \leqslant n \leqslant m^{2}
$$

then, $a_{n}=3 m-1$. Solving the inequality (3) for $m$, we get
$$
\sqrt{n} \leqslant m \leqslant 1+\sqrt{n-1} .
$$

The number of identical terms is $(2 m-1)(m=1,2, \cdots)$.
$$
\because 0 \leqslant 1+\sqrt{n-1}-\sqrt{n}<1 \text {. }
$$
$\therefore m$ is the integer part of $1+\sqrt{n-1}$, denoted as:
$$
m=[1+\sqrt{n-1}] \text {. }
$$

Thus, $a_{n}=3[1+\sqrt{n-1}]-1$.
Therefore, the sum of the first $n$ segments of the sequence $\left\{a_{n}\right\}$ is
$$
\begin{array}{l}
S_{n \in \mathbf{E}}=1 \times 2+3 \times 5+\cdots+(2 n-1)(3 n-1) \\
=6\left(1^{2}+\cdots+n^{2}\right)-5(1+2+\cdots+n)+n \\
=6 \times \frac{1}{6} n(n+1)(2 n+1)-5 \times \frac{1}{2} n(n+1)+n \\
=\frac{1}{2} n\left(4 n^{2}+n-1\right) .
\end{array}
$$

Substituting $n$ with $m-1$, the sum of the first $(m-1)$ segments is
$$
\begin{array}{l}
S_{(m-1)^{2}}=\frac{1}{2}(m-1)\left(4 m^{2}-7 m+2\right) . \\
\because a_{n}=3 m-1,
\end{array}
$$
$\therefore$ the sum of the first $n$ terms of the sequence $\left\{a_{n}\right\}$ is
$$
\begin{aligned}
S_{n} & =S_{(m-1)^{2}}+\left[n-(m-1)^{2}\right](3 m-1) \\
= & \frac{1}{2}(m-1)\left(4 m^{2}-7 m+2\right)+3 m n-n \\
& -(3 m-1)(m-1)^{2} \\
= & 3 m n-n-\frac{1}{2} m(m-1)(2 m-1),
\end{aligned}
$$

where $m=[1+\sqrt{n-1}]$.