We study the longitudinal decorrelations of elliptic, triangular and quadrangular flows in heavy-ion collisions at the LHC and RHIC energies. The event-by-event CLVisc (3+1)-dimensional hydrodynamics model, combined with the fully fluctuating AMPT initial conditions, is utilized to simulate the space-time evolution of the strongly-coupled quark-gluon plasma. Detailed analysis is performed for the longitudinal decorrelations of flow vectors, flow magnitudes and flow orientations. We find strong correlations between final-state longitudinal decorrelations of anisotropic flows and initial-state longitudinal structures and collision geometry: while the decorrelation of elliptic flow shows a non- monotonic centrality dependence due to initial elliptic geometry, typically the longitudinal flow decorrelations are larger in lower energy and less central collisions where the mean lengths of the string structure are shorter in the initial states.