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

Find $a_{n}$ and $S_{n}$.
Solution: This problem still involves an equal-segment incrementing sequence, where the sum of the first $(m-1)$ segments is
$$
\underbrace{k+k+\cdots+k}_{(m-1) \uparrow}=k(m-1) .
$$

It is clear that when and only when
$$
k(m-1)+1 \leqslant n \leqslant k m
$$

then, $a_{n}=m$. Solving the inequality (5) for $m$, we get
$\frac{n}{k} \leqslant m \leqslant \frac{n+k-1}{k}$, and the number of identical terms is $m$ (for $m=1,2, \cdots$).
$$
\because 0<\frac{n+k-1}{k}-\frac{n}{k}<1 \text {, }
$$
$\therefore m$ is the integer part of $\frac{n+k-1}{k}$, denoted as
$$
m=\left[\frac{n+k-1}{k}\right] \text {. }
$$

Thus, $a_{n}=\left[\frac{n+k-1}{k}\right]$.
Therefore, the sum of the first $n$ segments of the sequence $\left\{a_{n}\right\}$ is
$$
\begin{array}{l}
S_{n \in}=1 \cdot k+2 \cdot k+3 \cdot k+\cdots+n \cdot k \\
=k(1+2+\cdots+n)=\frac{1}{2} k n(n+1) .
\end{array}
$$

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

where $m=\left[\frac{n+k-1}{k}\right]$.