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

Find $a_{n}$ and $S_{n}$.
Solution: This is an interesting sequence, where the number of terms in each segment increases (in an arithmetic sequence).

Let the general term formula of the sequence $\{a_{n}\}$ be $a_{n}=m$ (where $m$ is the sequence number of each segment in the segmented sequence). By the sum formula of an arithmetic sequence, the sum of the number of terms in the first $(m-1)$ segments is
$$
1+2+3+\cdots+(m-1)=\frac{1}{2} m(m-1) \text{. }
$$

It can be seen that, if and only if
$$
\frac{1}{2} m(m-1)+1 \leqslant n \leqslant \frac{1}{2} m(m+1)
$$

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

The same term has $m$ (for $m=1,2, \cdots$) terms.
$$
\because 0 \leqslant \frac{1+\sqrt{8 n-7}}{2}-\frac{-1+\sqrt{8 n+1}}{2}<1 \text{, }
$$
$\therefore m$ is the integer part of $\frac{1+\sqrt{8 n-7}}{2}$, denoted as
$$
m=\left[\frac{1+\sqrt{8 n-7}}{2}\right] \text{. }
$$

Thus, $a_{n}=\left[\frac{1+\sqrt{8 n-7}}{2}\right]$.
Therefore, the sum of the first $(m-1)$ segments of the sequence $\{a_{n}\}$ is
$$
\begin{array}{l}
S_{\left.\left.\frac{1}{2} m \right\rvert\, m-1\right)}=1^{2}+2^{2}+3^{2}+\cdots+(m-1)^{2} \\
=\frac{1}{6} m(m-1)(2 m-1) .
\end{array}
$$

Since $a_{n}=m$, the sum of the first $n$ terms of the sequence $\{a_{n}\}$ is
$$
\begin{aligned}
S_{n}= & S_{\frac{1}{2} m\{m-1}+\left[n-\frac{1}{2} m(m-1)\right] m \\
= & \frac{1}{6} m(m-1)(2 m-1)+m n \\
& -\frac{1}{2} m^{2}(m-1) \\
= & m n-\frac{1}{6} m(m+1)(m-1),
\end{aligned}
$$

where $m=\left[\frac{1+\sqrt{8 n-7}}{2}\right]$.