Step 1: Introduce a substitution.
Given,
\[
\frac{dy}{dx}=\cos^2(3x+y)
\]
Let
\[
u=3x+y
\]
Then,
\[
\frac{du}{dx}=3+\frac{dy}{dx}
\]
Using the differential equation,
\[
\frac{du}{dx}=3+\cos^2 u
\]
Hence,
\[
\frac{du}{3+\cos^2 u}=dx
\]
Step 2: Simplify the denominator.
Using
\[
\cos^2 u=\frac{1}{1+\tan^2 u},
\]
let
\[
t=\tan u
\]
Then,
\[
du=\frac{dt}{1+t^2}
\]
Now,
\[
3+\cos^2 u
=
3+\frac{1}{1+t^2}
\]
\[
=
\frac{3(1+t^2)+1}{1+t^2}
\]
\[
=
\frac{4+3t^2}{1+t^2}
\]
Therefore,
\[
\frac{du}{3+\cos^2 u}
=
\frac{\frac{dt}{1+t^2}}{\frac{4+3t^2}{1+t^2}}
\]
\[
=
\frac{dt}{4+3t^2}
\]
Hence,
\[
\int \frac{dt}{4+3t^2}=\int dx
\]
Step 3: Integrate both sides.
We know that
\[
\int \frac{dt}{a^2+b^2t^2}
=
\frac{1}{ab}\tan^{-1}\left(\frac{bt}{a}\right)+C
\]
Here,
\[
a=2,
\qquad
b=\sqrt{3}
\]
Thus,
\[
\int \frac{dt}{4+3t^2}
=
\frac{1}{2\sqrt{3}}
\tan^{-1}\left(\frac{\sqrt{3}}{2}t\right)
\]
Therefore,
\[
\frac{1}{2\sqrt{3}}
\tan^{-1}\left(\frac{\sqrt{3}}{2}\tan u\right)
=
x+C
\]
Multiplying by \(2\sqrt{3}\),
\[
\tan^{-1}\left(\frac{\sqrt{3}}{2}\tan u\right)
=
2\sqrt{3}(x+C)
\]
Substituting
\[
u=3x+y,
\]
we get
\[
\tan^{-1}\left(\frac{\sqrt{3}}{2}\tan(3x+y)\right)
=
2\sqrt{3}(x+C)
\]
Thus,
\[
f(x)=2\sqrt{3}(x+C)
\]
Step 4: Final conclusion.
Therefore,
\[
\boxed{2\sqrt{3}(x+C)}
\]